C# 中的等效语法是什么(如果有)?
C# 是否有 VB6 的等效项
With
End With
Does C# have any equivalent for VB6
With
End With
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
C# 是否有 VB6 的等效项
With
End With
Does C# have any equivalent for VB6
With
End With
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
没有什么完全等效的东西,但 C# 3 获得了在构造时设置属性的能力:
和集合:
它绝对不能替代所有使用
With
,但其中一些值得了解。There's nothing quite equivalent, but C# 3 gained the ability to set properties on construction:
And collections:
It's definitely not a replacement for all uses of
With
, but worth knowing for some of them.C# 没有任何等效语法。最接近的是对象初始值设定项,但它们并不相同:
C# does not have any equivalent syntax. The closest are object initializers, but they are not the same:
不。
最接近的是对象和列表初始值设定项。
No.
What comes close are object and list initializers.
它绝不是等价的,但是,如果您想减少打字量,您可以这样做。
It is by no means an equivalent, however, if it is the typing you're trying to reduce, you can do.
没有与 Visual Basic 的
With
关键字等效的 C#。There is no C# equivalent to Visual Basic's
With
keyword.c# 中没有等效项 ->在评论中阅读更多信息 http://blogs.msdn.com/b/csharpfaq/archive/2004/03/11/why-doesn-tc-have-vb-net-s-with-operator.aspx
There is no equivalent in c# -> read more here in the comments http://blogs.msdn.com/b/csharpfaq/archive/2004/03/11/why-doesn-t-c-have-vb-net-s-with-operator.aspx
一种近乎等效的方法是调用类成员的方法。您不必在类成员中重复命名拥有对象 - 它隐含在以下事实中:该函数是为给定实例调用的成员。
出于这个原因,我怀疑在 C# 中直接等效于 With/End With 是否是一个好主意。如果您发现自己在给定范围内一遍又一遍地键入对象的名称,则很好地表明所讨论的代码将成为该对象的类的良好方法。
One near-equivalent would be calling a method that is a member of a class. You don't have to repeatedly name the owning object inside class members - it's implicit in the fact that the function is a member, called for a given instance.
I doubt a direct equivalent of With/End With is a good idea in C# for this reason. If you found yourself typing an object's name over and over in a given scope, it's a good indication that the code in question would make a good method on that object's class.
没有直接的等价物。您可以在构造时设置属性,正如其他人所解释的那样,或者您可以将表达式分配给具有短名称的变量。以下内容在语义上应该是等效的:
There is no direct equivalent. You can set properties on construction, as others explained, or you can assign your expression to a variable with a short name. The following should be semantically equivalent: