本文介绍了C#与VB中的With语句等效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我真的很喜欢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中:
var bar = Stuff.Elements.Foo
{
Name = "Bob Dylan",
Age = 68,
Location = "On Tour",
IsCool = True
};
这篇关于C#与VB中的With语句等效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!