Another variation of using new operator in C#
I've just learned about another variation we can use in C# to instantiate a class and set its member fields or properties. It's good to know in case you haven't known about it yet.
For example, let's say we have a class Foo with following code:
class Foo
{
public int A;
public int B;
public int C;
}
If I want to create the Foo object and set its properties, I always write it like this way for example:
Foo foo = new Foo();
foo.A = 1;
foo.B = 2;
foo.C = 3;
Apparently I also could write the code slightly different, but still achieving the same objective as:
Foo foo = new Foo()
{
A = 1, B = 2, C = 3
};
With this variation, we don't gain anything other than less typing, for instance if we use a long variable name :-)
Comments
- Anonymous
September 05, 2008
PingBack from http://housesfunnywallpaper.cn/?p=4375