'with' function?

Does C# have anything similar to Visual Basic's 'with' function?
Who is Participating?
_TAD_Connect With a Mentor Commented:
There are, however, a lot of other shortcuts you can use.

What code are you trying to shorten?  If you post it, we may be able to tidy it up for you.

The great thing about .Net is that many of its properties are really just references to other objects.

So instead of typing:
object1.child1.subchild2.subchild3.value1 = "something1";
object1.child1.subchild2.subchild3.value2 = "something2";
object1.child1.subchild2.subchild3.value3 = "something3";

many times you can say:

object1.child1.subchild2.subchild3 myObj;
myObj.value1 = "something1";
myObj.value2 = "something2";
myObj.value3 = "something3";


most people think that using the "using" statement is the same thing, but it is not.

- It makes the code hard to read.
- Things like "myObject.MyOtherObject.TheThridObject.AIndexer[myObject.MyOtherObject.AValue].Name" are nothing but very bad programming style. So there's no reason to make it easier to do bad programming...
- Everything more than one, max. two steps away should not be accessed directly.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

derekthorntonAuthor Commented:
I'm just trying to define the fileds of a struct, I can type them all in, it would just be easier with a 'with' like block. Thanks anyway!
Define a constructor on the struct.

public struct MyStruct
    int a;
    int b;
    int c;

    public MyStruct(int a, int b, int c)
        this.a = a;
        this.b = b;
        this.b = b;

Then call new MyStruct(1, 2, 3);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.