This isn't named parameters though. It allows you to set accessible
properties/fields after using new to construct the object. Very different
things.
I do like the feature, but don't see at the moment that it actually saves
much typing other then a newline or so and in some ways can be a bit harder
to look at. Being able to stick more on one line does not equal clearer
code imo:
c = new C()
c.Name1 = "name";
-vs-
c= new C(){Name1="name"}
What are all the advantages?
--
William Stacey [C# MVP]
"Chris Mullins [MVP]" <cmull@yahoo.com> wrote in message
news:%23kgzP25pHHA.1148@TK2MSFTNGP06.phx.gbl...
| "Paul" <paul.tho
@corpoflondon.gov.uk> wrote:
| > Is it possible to make use of named parameters outside of Attribute?
| >
| > If so how :)
|
| C# 3.0, which ships this year, brings what you're looking for - they're
| called Object Initializers. You can read all about them at:
|
http://blogs.msdn.com/wriju/archive/2007/04/12/c-3-0-enhancements-obj... |
| This is something that I've missed for a while too, as I quite like doing
it
| with Attributes.
|
| --
| Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
|
http://www.coversant.com/blogs/cmullins |
|
|
-----------------------------------------------Reply-----------------------------------------------
It saves some typing when adding it to a List :).
List<C> lst = new List<C>();
lst.Add(new C(){Name1="name0"} );
lst.Add(new C(){Name1="name1"} );
lst.Add(new C(){Name1="name2"} );
lst.Add(new C(){Name1="name3"} );
-----------------------------------------------Reply-----------------------------------------------
Maybe... personally I've never had any problem with the 2 existing
patterns (below). It is a little more succinct, but not a lot...
lst.Add(new C("name0")); // if a suitable ctor exists
// or
lst.Add(CreateC("name0")); // if some (locally-specific-set-of)
properties must be used
// ... (the following implementation-(rather than type-) specific
method is scoped to the class using it)
private static C CreateC(string name) {
C c = new C();
c.Name = name;
return c;
}
"Michael Huber" <huberm
@gmail.com> wrote in message
news:1181118062.937094.55230@q66g2000hsg.googlegroups.com...
> It saves some typing when adding it to a List :).
> List<C> lst = new List<C>();
> lst.Add(new C(){Name1="name0"} );
> lst.Add(new C(){Name1="name1"} );
> lst.Add(new C(){Name1="name2"} );
> lst.Add(new C(){Name1="name3"} );
oh thanks
its very nice :)
"Chris Mullins [MVP]" wrote:
> "Paul" <paul.tho
@corpoflondon.gov.uk> wrote:
> > Is it possible to make use of named parameters outside of Attribute?
> > If so how :)
> C# 3.0, which ships this year, brings what you're looking for - they're
> called Object Initializers. You can read all about them at:
> http://blogs.msdn.com/wriju/archive/2007/04/12/c-3-0-enhancements-obj...
> This is something that I've missed for a while too, as I quite like doing it
> with Attributes.
> --
> Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
> http://www.coversant.com/blogs/cmullins