为什么 with() 结构在 VB.NET 中非常酷,但没有包含在 C# 中?
我是 C# 开发人员。 我真的很喜欢花括号,因为我有 C、C++ 和 Java 背景。 不过,我也喜欢 .NET 系列的其他编程语言,例如 VB.NET。 如果您已经在 .NET 中编程了一段时间,那么在 C# 和 VB.NET 之间来回切换并不是什么大问题。 这是我工作的公司中非常常见的做法。 作为 C# 人员,我非常喜欢 VB.NET 编译器提供的 XML 文字和 with
关键字。 我希望 Microsoft 也将这些功能包含在 C# 中。
我只是好奇其他开发者对此有何评论!
I am C# developer. I really love the curly brace because I came from C, C++ and Java background. However, I also like the other programming languages of the .NET Family such as VB.NET. Switching back and forth between C# and VB.NET is not really that big of deal if you have been programming for a while in .NET. That is very common approach in the company where I work. As C# guy, I really like the XML literal and with
keywords provided by the VB.NET compiler. I wish Microsoft had included those features in C# also.
I am just curious , what other developer has to say about it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
就我个人而言,我不喜欢在构造后使用WITH - 如果您需要在初始化后对对象执行多项操作,通常该行为应该封装在类型本身中。 如果您确实想做类似WITH 的事情,只需声明一个短变量并可选择引入一个新作用域即可。
然而,能够紧凑地初始化具有多个属性的对象是很有用的 - 这正是 C# 3 允许您编写的原因:
对此有一些限制(C# 4 中的可选参数和命名参数将有助于克服),但它比以前更好,而且不会导致潜在的混乱/歧义。
(在 XML 文字方面,我再次支持 C# 团队 - XML 是一种非常具体的技术,可以放入语言中。如果他们能够提出一种通用形式来创建 XML,但是也可以用来创建其他树,那就太好了 - 就像查询表达式不直接绑定到 IEnumerable 或 IQueryable 一样。)
Personally I don't like WITH when it's used after construction - if you need to do several things with an object after it's initialized, usually that behaviour should be encapsulated in the type itself. If you really want to do something like WITH, it's only a matter of declaring a short variable and optionally introducing a new scope.
However, it is useful to be able to compactly initialize an object with several properties - which is precisely why C# 3 allows you to write:
There are limitations to this (which the optional and named parameters in C# 4 will help to overcome) but it's better than it was without leading to potential messes/ambiguities.
(On the XML literal front, I'm again with the C# team - XML is a very specific technology to put into a language. If they could come up with a generalised form which happened to create XML but could be used to create other trees too, that would be nice - just as query expressions aren't directly tied to IEnumerable or IQueryable.)
您可以通过创建快速的单字母变量名称来替换 VB.Net 的
With
。 实际上代码更少,因为With
稍后还需要End With
。例如,我过去经常需要做的一件事是迭代控制/中断样式报告的数据表中的行。
在 vb.net 中,可能看起来像这样:
C# 看起来像这样:
这里需要注意的是,C# 版本实际上需要更少的输入,因为 VB 无法将
WITH
与数组索引,并且必须通过.Item
属性来处理某些事情。 这里没什么大不了的,但想象一下,如果报表有 20 个字段而不是 2 个,并且必须在 3 个项目而不是 1 个项目上中断。当然,您也可以使用 C# 中演示的用于 VB 的技术。 但要注意的主要事情是
WITH
并不能真正给你带来太多。You can replace VB.Net's
With
by creating a quick single-letter variable name. It's actually less code, sinceWith
also requires anEnd With
later on.For example, one thing I used to need to do fairly often was iterate over the rows in a datatable for a control/break style report.
In vb.net, that might look like this:
The C# would look like this:
The thing to notice here is that the C# version actually needed less typing, because the VB couldn't combine
WITH
with the array index, and had to go through the.Item
property for certain things. It's not a big deal here, but imagine if the report had 20 fields instead of 2 and had to break on 3 items instead of 1.Of course, you could use the technique demonstrated in C# for VB as well. But the main thing to note is that
WITH
doesn't really give you much.这是关于开发人员偏好的问题,但我同意你的意见。 我的偏好是尽量减少发挥作用的变量数量以及它们存在的范围。 C# 的哲学似乎很相似。 但在这种情况下,这里的反应似乎表明,与对我来说与 lambda 非常相似的构造相比,添加(并让你自己负责)变量是一件好事。
It's about developer preferences, but I'm with you about WITH. My preference is to minimize the number of variables in play, and the scope within which they live. The philosophy of C# seems to be much the same. But in this case the responses here seem to suggest that adding (and making yourself responsible for) a variable is a Good Thing compared to a construct which, to me, is very similar to lambdas.
我觉得在初始化期间只允许“质量”属性设置是相当任意的。 我真的不明白为什么这会“糟糕”:
我觉得这个示例代码干净简洁。
I feel it is rather arbitrary to only allow 'mass' property setting during initialization. I really don't get why this would be 'bad':
I feel that this example code is clean and concise.
通常,当我看到对 C# 中的
with
功能的请求时,我会看到可以从重构中受益的代码。 通常,当重构完成后,对with
的假设需求就消失了。当我在对象中构建流畅的接口时,我喜欢代码的运行方式; 它与
with
有一些相似之处。 如果我正在设计MessageBox.Show()
,我可能会这样写:你也可以看到与 Linq 类似的东西:
感觉有点像
with
但似乎很自然地适合我已经拥有的语言。Usually when I see a request for a
with
feature in C#, I see code that would benefit from refactoring. Usually when the refactoring is done, the supposed need forwith
is gone.I like the way my code works out when I build a fluent interface in to an object; it has some similarities to
with
. If I was designingMessageBox.Show()
, I might write:You can also see something similar with Linq:
It feels a bit like
with
but seems to fit naturally in to the language I already have.如果“With”或类似的功能最终与其他迄今为止流行的 VB 专用功能一起添加到 C# 中,我不会感到惊讶。 最近在 PDC 和其他地方的 C# 4.0 预览演示中,有很多关于从 C# 4.0 和 VB 10 开始增加“对语言平等的关注”的讨论。
I wouldn't be suprised if "With" or a similar feature is added to C# eventually along with other heretofore popular exclusively VB features. There was a lot of talk in the C# 4.0 preview presentations recently at PDC and elsewhere of the increased "focus on language parity" starting with C# 4.0 and VB 10.