NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Awesome .NET 4: Named and Optional Parameters for Classes and Methods

Monday, November 16th, 2009

.NET 4.0 packs a plethora of new awesome features for developers, but my favorite is by far named and optional parameters for both functions and classes. This addition in .NET 4.0 allows developers to drastically shorten the coding time on classes, default constructors, method overloads, and overall coding while calling these methods and classes.

I would like to jump right into an example, so for my purposes, consider this .NET 3.5 class:

public class Dog
{
private string m_Name;
private int m_Age;

public Dog() {
Dog("", 1);
}

public Dog(string Name) {
Dog(Name, 1);
}

public Dog(string Name, int Age)
{
m_Name = Name;
m_Age = Age;
}

public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}

public int Age
{
get
{
return m_Age;
}
set
{
m_Age = value;
}
}
}

You could than instantiate this class as any of the following:

Dog mydog = new Dog();
Dog mydog = new Dog("Zoe");
Dog mydog = new Dog("Scooter", 5);

There are 2 major problems for developers (now that we know of better options) with this above:
1. You have to continually overload the constructor, which means a lot of redundant coding
2. You must pay attention to the ordering of these methods while coding using the class. For example, you cannot have Dog(5, "Scooter") as the types are wrong, and even if the types matched you would have the wrong data displayed for each member.

.NET 4.0 resolves both of these problems, so let's start by declaring my class in .NET 4.0:

public class Dog
{
private string m_Name;
private int m_Age;

public Dog(string Name = "", int Age = 1)
{
m_Name = Name;
m_Age = Age;
}

public string Name
{
get
{
return m_Name;
}
set
{
m_Name = value;
}
}

public int Age
{
get
{
return m_Age;
}
set
{
m_Age = value;
}
}
}

Notice that this, first of all and very obviously, eliminates all the other overloaded methods for the constructor. This is a huge win for me already.

Second of all, defaults are now defined for both values, and it is immediately obvious what those defaults are.

We can also (which I will show below) now call this method with any combination and ordering of these parameters.

We can now instantiate this class in any of the following manners:

Dog mydog = new Dog("Zoe", 3);
Dog mydog = new Dog("Scooter");
Dog mydog = new Dog(Name:"Zoe", Age:5)
Dog mydog = new Dog(Age:5, Name:"Zoe");

Or, an even more elegant instantiation:
Dog mydog = new Dog() { Age = 5, Name = "Scooter" }

What this last use means, is no more of your common .NET 3.5 declarations such as:
Dog mydog = new Dog();
mydog.Name = "Scooter";
mydog.Age = 5;

This simple innovation in .NET 4.0 will drastically speed up coding time, clarity of development, and ease of comprehension while looking at code. I cannot wait until .NET 4.0 leaves beta for this reason alone.

.NET 4in' Tom Out.

Tags

Howto CSharp

Related Blogs

Fedora Core 20 and Apache Virtual Host on mounted share
Fixing MOSS/WSS SharePoint errors with Alternate Access Mappings
Determining if MOSS is Standard or Enterprise
Resolving ASP.NET Web.Config Inheritance
Disabling Outlook restricted extensions

Comments

mstortz said on Friday, November 20th, 2009 @ 11:59 AM

Yes, I made a typo providing the value of "Dog". What I meant is, you can't call a constructor like a method in 3.5. You get a "'Dog' is a 'type' but is used like a 'variable'" error.

I take you =can= do this in C#4.0? I would love to be able to compute parameters for the base constructor!

Tom said on Friday, November 20th, 2009 @ 7:55 AM

Hi mstortz,

Those 2 are completely different.

My example yields:
mydog->Name = ""
mydog->Age = 1

Your example yields:
mydog->Name = "Dog"
mydog->Age = 1

I think what you were trying to imply is that you could just reference through the This variable and bypass the method call within an overloaded method. That is an option yes, but just a different style of coding.

T

mstortz said on Friday, November 20th, 2009 @ 7:46 AM

You can do what now?

public Dog() {
Dog("", 1);
}

Really?

Not...

public Dog() : this("Dog", 1) { }

?

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations