Equivalence of "With...End With" in C#

Equivalence of With...End With in C#?

C# doesn't have an equivalent language construct for that.

C# equivalent for Visual Basic keyword: 'With' ... 'End With'?

You cannot do this in C#.

This feature is specific to VB and the closest you can come in C# is the object initializer like you describe.

With block equivalent in C#?

This is what Visual C# program manager has to say:
Why doesn't C# have a 'with' statement?

Many people, including the C# language designers, believe that 'with'
often harms readability, and is more of a curse than a blessing. It is
clearer to declare a local variable with a meaningful name, and use
that variable to perform multiple operations on a single object, than
it is to have a block with a sort of implicit context.

What's the C# equivalent to the With statement in VB?

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;

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;

What is the equivalent of End (VB6/VBA) in order to end in C# for Windows applications?

The marked answer is not correct. Application.Exit() is a graceful shutdown, it can be blocked by a form's FormClosing event handler setting e.Cancel = true. The exact equivalent to the VB End statement is Environment.Exit(0);

Is there a VB.NET With statement in C#?

No, there isn't.

This was intentionally left out of C#, as it doesn't add much convenience, and because it can easily be used to write confusing code.

Blog post from Scott Wiltamuth, Group Program Manager for Visual C#, about the with keyword: https://web.archive.org/web/20111217005440/http://msdn.microsoft.com/en-us/vstudio/aa336816.aspx

For the special case of a StringBuilder, you can chain the calls:

StringBuilder sb = new StringBuilder()
.Append("foo")
.Append("bar")
.Append("zap");

C# & VB6: How to convert 'with' statement to C#?

You haven't shown the EventThief code, which makes it impossible to tell, really. But in general:

With expression
.Foo = a
.Bar = b
End With

would translate to

var x = expression;
x.Foo = a;
x.Bar = b;

(Of course you can specify the type explicitly...)

The commonality here is that expression is only evaluated once. In the particular code you showed, there's no need for an extra variable of course, as the expression is only the local variable in the first place.

Your actual error looks like it's just to do with the types of EventThief.RIGHT_DOWN etc rather than with the WITH statement.

EDIT: Okay, you've now shown the original EventThief code which does use Booleans... but you haven't shown your ported EventThief code. You wrote:

It says et.LEFT_UP is a short

... but it shouldn't be. In the original it's a Boolean, so why is it a short in your port?

Java equivalent to #region in C#

There's no such standard equivalent. Some IDEs - Intellij, for instance, or Eclipse - can fold depending on the code types involved (constructors, imports etc.), but there's nothing quite like #region.

What is the equivalent syntax in C#, if any?

There's nothing quite equivalent, but C# 3 gained the ability to set properties on construction:

var person = new Person { Name = "Jon", Age = 34 };

And collections:

var people = new List<Person>
{
new Person { Name = "Jon" },
new Person { Name = "Holly"}
};

It's definitely not a replacement for all uses of With, but worth knowing for some of them.



Related Topics



Leave a reply



Submit