473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Named parameters

Is it possible to make use of named parameters outside of Attribute?

If so how :)

Thanks in advance.

Jun 5 '07 #1
8 1499
Paul <pa*********@corpoflondon.gov.ukwrote:
Is it possible to make use of named parameters outside of Attribute?

If so how :)
What exactly are you after? C# only supports positional parameters for
methods, if that's what you mean.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 5 '07 #2
"Paul" <pa*********@corpoflondon.gov.ukwrote:
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/...tializers.aspx

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

Jun 5 '07 #3
Thanks,

I have seen a demo of that at the last DevWeek. It does look good.

Paul

Jun 5 '07 #4
This isn't named parameters though. It allows you to set accessible
properties/fields after using new to construct the object. Very different
things.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris Mullins [MVP]" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Paul" <pa*********@corpoflondon.gov.ukwrote:
>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/...tializers.aspx

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
Jun 5 '07 #5
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]" <cm******@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
| "Paul" <pa*********@corpoflondon.gov.ukwrote:
| 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/...tializers.aspx
|
| 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
|
|
|
Jun 5 '07 #6
It saves some typing when adding it to a List :).

List<Clst = 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"} );

Jun 6 '07 #7
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" <hu*******@gmail.comwrote in message
news:11*********************@q66g2000hsg.googlegro ups.com...
It saves some typing when adding it to a List :).

List<Clst = 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"} );

Jun 6 '07 #8
oh thanks

its very nice :)

"Chris Mullins [MVP]" wrote:
"Paul" <pa*********@corpoflondon.gov.ukwrote:
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/...tializers.aspx

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

Jun 6 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
48
by: Adam Ruth | last post by:
Hello, Has there ever been any talk to adding named parameters to C? I enjoy using them in my Python and Ada code and can see their usefulness in C. I can envision an implementation where the...
9
by: Wiktor Zychla | last post by:
Hello, I wonder why the delegate declaration needs named parameters? public delegate void MyDelegate( int a, int b ); // ok public delegate void MyDelegate( int, int ); // compiler error...
18
by: Peter Hardy | last post by:
Hi Guys, Can I use named parameters in C# Methods. i.e doFoo(fname="do", lname="Foo"); Cheers, Pete
8
by: cody | last post by:
Why doesn't C# allow default parameters for methods? An argument against I hear often is that the default parameters would have to be hardbaken into the assembly, but why? The Jit can take care of...
17
by: Ben R. | last post by:
I'm reading about attribute classes and specifically, named versus positional parameters. Was this implimented instead of multiple constructors for flexibility or is there another reason I'm...
3
by: Adam Hartshorne | last post by:
What is named parameter mechanism? Any ideas? I am looking through some code and there is a comment saying "VC++ has trouble with the named parameters mechanism", which i have no idea what this...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.