C# 4.0 - Named Arguments

No.of Views822
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:10
Tag : CSharp , Miscellaneous
C# 4.0 - Named Arguments
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

For methods having numerous arguments we tend to get confused. And this is also pain for others while reviewing the code. C# 4.0 gives us an opportunity to pass parameters with name.

How it works, let’s check it. Suppose you have a method like below,

C# Code:

public static class TestClass

{

public static void ParamMethod(string Name, double Salary,

string Department, int Age = 30)

{

Console.WriteLine("Name = {0}", Name);

Console.WriteLine("Age = {0}", Age);

Console.WriteLine("Salary = {0}", Salary);

Console.WriteLine("Department = {0}", Department);

}

}

Now the calling would look like,

TestClass.ParamMethod(Name:"Amal", Salary:1000.00,

Department:"Software",Age:31);

You can alter the sequence of the parameters too,

TestClass.ParamMethod("Tupur", 2000.00,

Department: "IT", Age: 26);

No compiler overhead is associated with it.

Rule, Non-optional parameters has to be specified either sequentially or by name.

Thank you

Amal

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top