tags:

views:

1484

answers:

10

I know VB.Net and am trying to brush up on my C#. Is there a With block equivalent in C#?

Thanks

+3  A: 

No there is not.

Gavin Miller
-1: there is already an accepted answer with same response. This seems to be polluting the answer...
Sung Meister
+4  A: 

No, there is not.

Guulh
Jinx! Buy me a coke! :P
Gavin Miller
+1 to even out the score with LFSR Consulting ;)
Jeff Yates
-1: there is already an accepted answer, this seems to be polluting the answer...
Sung Meister
+21  A: 

No, and thank goodness there isn't.

Ray
+1 for the additional sentiment.
Jeff Yates
Amen to that. +1
Otávio Décio
+1, I'd rather have code readability.
RexM
With does not reduce readability IMHO
ggf31416
@ggf... Coming from Delphi I'd have to say it does. Especially when you have nested Withs
Ray
coming from JS I'd have to say it doesn't, even with neste withs
annakata
-1 for the snobbery
RS Conley
+1 for the witty answer and the funny comment by @RS Conley
Sung Meister
+3  A: 

You could use the argument accumulator pattern.

Big discussion about this here:

http://blogs.msdn.com/csharpfaq/archive/2004/03/11/87817.aspx

BBetances
This is a good alternative, and the pattern is nice for lots of reasons other than replacing "with". Another option is a utility method, like I describe here: http://stackoverflow.com/questions/601153/missing-the-with-keyword-in-c/601237#601237
Gabe Moothart
A: 

hmm. I have never used VB.net in any depth, so I'm making an assumption here, but I think the 'using' block might be close to what you want.

using defines a block scope for a variable, see the example below

using ( int temp = someFunction(param1) ) {
   temp++;  // this works fine
}

temp++; // this blows up as temp is out of scope here and has been disposed

Here is an article from Microsoft that explains a bit more


EDIT: yeah, this answer is wrong - the original assumption was incorrect. VB's 'WITH' is more like the new C# object initialisers:

var yourVariable = new yourObject { param1 = 20, param2 = "some string" };
mlennox
No, a using statement is very different - the point of a using statement is to dispose of a resource at the end of the block. It doesn't make referring to the value any shorter.
Jon Skeet
Thanks Jon, always good to learn something new about another language, I suppose I should have taken heed of the old statement "Assume makes an ass out of u and me" - but I guess its only me looking bad in this case ;)
mlennox
nope ... the initializer only works on initialization ... have a look at fluent interfaces!
Andreas Niedermair
+2  A: 

About 3/4 down the page in the "Using Objects" section:

VB:

With hero 
  .Name = "SpamMan" 
  .PowerLevel = 3 
End With

C#:

//No "With" construct
hero.Name = "SpamMan"; 
hero.PowerLevel = 3;
Timothy Carter
+5  A: 

Although C# doesn't have any direct equivalent for the general case, C# 3 gain object initializer syntax for constructor calls:

var foo = new Foo { Property1 = value1, Property2 = value2, etc };

See chapter 8 of C# in Depth for more details - you can download it for free from Manning's web site.

(Disclaimer - yes, it's in my interests to get the book into more people's hands. But hey, it's a free chapter which gives you more information on a related topic...)

Jon Skeet
A: 

ever heard of a fluent-interface? ... that is really near to that ...

Andreas Niedermair
+3  A: 

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

Gulzar
+1  A: 

As the Visual C# Program Manager linked above says, there are limited situations where the With statement is more efficient, the example he gives when it is being used as a shorthand to repeatedly access a complex expression.

Using an extension method and generics you can create something that is vaguely equivalent to a With statement, by adding something like this:

    public static T With<T>(this T item, Action<T> action)
    {
        action(item);
        return item;
    }

Taking a simple example of how it could be used, using lambda syntax you can then use it to change something like this:

    updateRoleFamily.RoleFamilyDescription = roleFamilyDescription;
    updateRoleFamily.RoleFamilyCode = roleFamilyCode;

To this:

    updateRoleFamily.With(rf =>
          {
              rf.RoleFamilyDescription = roleFamilyDescription;
              rf.RoleFamilyCode = roleFamilyCode;
          });

On an example like this the only advantage is perhaps a nicer layout, but with a more complex reference and more properties it could well give you more readable code.

RTPeat
I don't really see the advantage of what your example shows. The original code (pre-lambda) is the [objectinstance].[property] = [value]. The lambda code is basically just changing the updateRoleFamily with rf.
Dan Appleyard
Try it with a longer reference to the object instance and many more properties. In the above example you are simplifying updateRoleFamily to rf and setting two properties, which you are correct, isn't a big gain. If however your object instance is something like myDataStructure.GetButton(44), and you have to set ten properties it could make it more readable to use a lambda or set a local variable.Like the original VB With statement, it is only a little bit of syntactic sugar, that you can take or leave.
RTPeat