与 VB 中的 With 语句等效的 C# 是什么? [英] What's the C# equivalent to the With statement in VB?
本文介绍了与 VB 中的 With 语句等效的 C# 是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
With"与End With"的等价性在 C# 中?
我非常喜欢 VB 的一个特性……With
语句.C# 有什么等价的吗?我知道您可以使用 using
来不必键入名称空间,但仅限于此.在 VB 中你可以这样做:
There was one feature of VB that I really like...the With
statement. Does C# have any equivalent to it? I know you can use using
to not have to type a namespace, but it is limited to just that. In VB you could do this:
With Stuff.Elements.Foo
.Name = "Bob Dylan"
.Age = 68
.Location = "On Tour"
.IsCool = True
End With
C# 中的相同代码是:
The same code in C# would be:
Stuff.Elements.Foo.Name = "Bob Dylan";
Stuff.Elements.Foo.Age = 68;
Stuff.Elements.Foo.Location = "On Tour";
Stuff.Elements.Foo.IsCool = true;
推荐答案
不是真的,您必须分配一个变量.所以
Not really, you have to assign a variable. So
var bar = Stuff.Elements.Foo;
bar.Name = "Bob Dylan";
bar.Age = 68;
bar.Location = "On Tour";
bar.IsCool = True;
或在 C# 3.0 及更高版本中:
Or in C# 3.0 and above:
var bar = new FooType
{
Name = "Bob Dylan",
Age = 68,
Location = "On Tour",
IsCool = True
};
Stuff.Elements.Foo = bar;
这篇关于与 VB 中的 With 语句等效的 C# 是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文