This question already has an answer here:
这个问题在这里已有答案:
- With block equivalent in C#? 14 answers
使用C#中的块等效? 14个答案
I know that C# has the using
keyword, but using
disposes of the object automatically.
我知道C#有using关键字,但是自动使用对象处理。
Is there the equivalence of With...End With
in Visual Basic 6.0?
在Visual Basic 6.0中是否存在With ... End With的等价性?
6 个解决方案
#1
C# doesn't have an equivalent language construct for that.
C#没有相应的语言结构。
#2
It's not equivalent, but would this syntax work for you?
它不等同,但这种语法对你有用吗?
Animal a = new Animal()
{
SpeciesName = "Lion",
IsHairy = true,
NumberOfLegs = 4
};
#3
There is no equivalent, but I think discussing a syntax might be interesting!
没有等价物,但我认为讨论语法可能很有趣!
I quite like;
我相当喜欢;
NameSpace.MyObject.
{
active = true;
bgcol = Color.Red;
}
Any other suggestions?
还有其他建议吗?
I cant imagine that adding this language feature would be difficult, essentially just a preprocessed.
我无法想象添加这种语言功能会很困难,基本上只是预处理。
EDIT:
I was sick of waiting for this feature, so here is and extension that achieves a similar behavior.
我厌倦了等待这个功能,所以这里和扩展实现了类似的行为。
/// <summary>
/// C# implementation of Visual Basics With statement
/// </summary>
public static void With<T>(this T _object, Action<T> _action)
{
_action(_object);
}
Usage;
LongInstanceOfPersonVariableName.With(x => {
x.AgeIntVar = 21;
x.NameStrVar = "John";
x.NameStrVar += " Smith";
//etc..
});
EDIT: Interestingly it seems someone beat me to the punch, again, with this "solution". Oh well..
编辑:有趣的是,似乎有人用这个“解决方案”再次打败了我。那好吧..
#4
I think the equivalent of the following VB:
我认为相当于以下VB:
With SomeObjectExpression()
.SomeProperty = 5
.SomeOtherProperty = "Hello"
End With
Would be this is C#:
这将是C#:
{
Var q=SomeOtherExpression();
q.SomeProperty = 5;
q.SomeOtherProperty = "Hello";
}
The only real difference is that in vb, the identifier doesn't have a name "q", but is simply a default identifier used when a period is encountered without any other identifier before it.
唯一真正的区别在于,在vb中,标识符没有名称“q”,而只是在遇到句点而没有任何其他标识符之前使用的默认标识符。
#5
There's no equivalent to With ... End With in C#.
没有相当于With ... End With in C#。
Here's a comparison chart for you that illustrates differences between Visual Basic and C#.
这是一个比较图表,说明了Visual Basic和C#之间的差异。
#6
There is no equivalent structure in C#. This is a Visual Basic 6.0 / VB.NET feature.
C#中没有等效的结构。这是Visual Basic 6.0 / VB.NET功能。
#1
C# doesn't have an equivalent language construct for that.
C#没有相应的语言结构。
#2
It's not equivalent, but would this syntax work for you?
它不等同,但这种语法对你有用吗?
Animal a = new Animal()
{
SpeciesName = "Lion",
IsHairy = true,
NumberOfLegs = 4
};
#3
There is no equivalent, but I think discussing a syntax might be interesting!
没有等价物,但我认为讨论语法可能很有趣!
I quite like;
我相当喜欢;
NameSpace.MyObject.
{
active = true;
bgcol = Color.Red;
}
Any other suggestions?
还有其他建议吗?
I cant imagine that adding this language feature would be difficult, essentially just a preprocessed.
我无法想象添加这种语言功能会很困难,基本上只是预处理。
EDIT:
I was sick of waiting for this feature, so here is and extension that achieves a similar behavior.
我厌倦了等待这个功能,所以这里和扩展实现了类似的行为。
/// <summary>
/// C# implementation of Visual Basics With statement
/// </summary>
public static void With<T>(this T _object, Action<T> _action)
{
_action(_object);
}
Usage;
LongInstanceOfPersonVariableName.With(x => {
x.AgeIntVar = 21;
x.NameStrVar = "John";
x.NameStrVar += " Smith";
//etc..
});
EDIT: Interestingly it seems someone beat me to the punch, again, with this "solution". Oh well..
编辑:有趣的是,似乎有人用这个“解决方案”再次打败了我。那好吧..
#4
I think the equivalent of the following VB:
我认为相当于以下VB:
With SomeObjectExpression()
.SomeProperty = 5
.SomeOtherProperty = "Hello"
End With
Would be this is C#:
这将是C#:
{
Var q=SomeOtherExpression();
q.SomeProperty = 5;
q.SomeOtherProperty = "Hello";
}
The only real difference is that in vb, the identifier doesn't have a name "q", but is simply a default identifier used when a period is encountered without any other identifier before it.
唯一真正的区别在于,在vb中,标识符没有名称“q”,而只是在遇到句点而没有任何其他标识符之前使用的默认标识符。
#5
There's no equivalent to With ... End With in C#.
没有相当于With ... End With in C#。
Here's a comparison chart for you that illustrates differences between Visual Basic and C#.
这是一个比较图表,说明了Visual Basic和C#之间的差异。
#6
There is no equivalent structure in C#. This is a Visual Basic 6.0 / VB.NET feature.
C#中没有等效的结构。这是Visual Basic 6.0 / VB.NET功能。