With Block Equivalent in C#

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.

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

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

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;

VBA to C# with statement

As HighCore said, there's no C# equivalent to VB's With block. Here's the equivalent C# code:

myRow = ProductRangeA.Rows.Count;
myValue = ProductRangeA.Value;

myRow = ProductRangeB.Columns.Count;
myValue = ProductRangeB.Value;

Since you can't avoid typing ProductRangeA and ProductRangeB twice each, you can reduce typing by using shorter variable names. That can make the code more difficult to understand, of course.

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.

Objective-C code blocks equivalent in C#

Something like this:

void Foo(Action<int> m)
{
int a = 5;
m(a);
}

void RegularFoo()
{
Foo(val => // Or: Foo(delegate(int val)
{
Console.WriteLine(val);
});
}

Action<T> is a delegate that takes exactly one argument of a type you specify (in this case, int), that executes without returning anything. Also see the general C# delegate reference.

For a simple case like this, it's pretty straightforward. However, I believe there are some semantic/technical differences between blocks in Objective-C and delegates in C#, which are probably beyond the scope of this question.

What is the VB.NET equivalent to C#'s 'using' block

It's the same, it's just:

Using conn As New SqlConnection
....
End Using

What is the C# Using block and why should I use it?

If the type implements IDisposable, it automatically disposes that type.

Given:

public class SomeDisposableType : IDisposable
{
...implmentation details...
}

These are equivalent:

SomeDisposableType t = new SomeDisposableType();
try {
OperateOnType(t);
}
finally {
if (t != null) {
((IDisposable)t).Dispose();
}
}

using (SomeDisposableType u = new SomeDisposableType()) {
OperateOnType(u);
}

The second is easier to read and maintain.


Since C# 8 there is a new syntax for using that may make for more readable code:

using var x = new SomeDisposableType();

It doesn't have a { } block of its own and the scope of the using is from the point of declaration to the end of the block it is declared in. It means you can avoid stuff like:

string x = null;
using(var someReader = ...)
{
x = someReader.Read();
}

And have this:

using var someReader = ...;
string x = someReader.Read();


Related Topics



Leave a reply



Submit