Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 49: Named Arguments

Section 49.1: Argument order is not necessary

You can place named arguments in any order you want.

Sample Method:

public static string Sample(string left, string right)

{

return string.Join("-",left,right);

}

Call Sample:

Console.WriteLine (Sample(left:"A",right:"B"));

Console.WriteLine (Sample(right:"A",left:"B"));

Results:

A-B

B-A

Section 49.2: Named arguments and optional parameters

You can combine named arguments with optional parameters.

Let see this method:

public sealed class SmsUtil

{

public static bool SendMessage(string from, string to, string message, int retryCount = 5, object attachment = null)

{

// Some code

}

}

When you want to call this method without set retryCount argument :

var result = SmsUtil.SendMessage(

 

from

: "Cihan",

to

: "Yakar",

message

: "Hello there!",

attachment

: new object());

 

 

Section 49.3: Named Arguments can make your code more clear

Consider this simple class:

class SmsUtil

{

public bool SendMessage(string from, string to, string message, int retryCount, object attachment)

GoalKicker.com – C# Notes for Professionals

211

{

// Some code

}

}

Before C# 3.0 it was:

var result = SmsUtil.SendMessage("Mehran", "Maryam", "Hello there!", 12, null);

you can make this method call even more clear with named arguments:

var result = SmsUtil.SendMessage( from: "Mehran",

to: "Maryam",

message "Hello there!", retryCount: 12, attachment: null);

GoalKicker.com – C# Notes for Professionals

212

Chapter 50: Named and Optional

Arguments

Section 50.1: Optional Arguments

Consider preceding is our function definition with optional arguments.

private static double FindAreaWithOptional(int length, int width=56)

{

try

{

return (length * width);

}

catch (Exception)

{

throw new NotImplementedException();

}

}

Here we have set the value for width as optional and gave value as 56. If you note, the IntelliSense itself shows you the optional argument as shown in the below image.

Console.WriteLine("Area with Optional Argument : "); area = FindAreaWithOptional(120); Console.WriteLine(area);

Console.Read();

Note that we did not get any error while compiling and it will give you an output as follows.

Using Optional Attribute.

Another way of implementing the optional argument is by using the [Optional] keyword. If you do not pass the

GoalKicker.com – C# Notes for Professionals

213

value for the optional argument, the default value of that datatype is assigned to that argument. The Optional keyword is present in “Runtime.InteropServices” namespace.

using System.Runtime.InteropServices;

private static double FindAreaWithOptional(int length, [Optional]int width)

{

try

{

return (length * width);

}

catch (Exception)

{

throw new NotImplementedException();

}

}

area = FindAreaWithOptional(120); //area=0

And when we call the function, we get 0 because the second argument is not passed and the default value of int is 0 and so the product is 0.

Section 50.2: Named Arguments

Consider following is our function call.

FindArea(120, 56);

In this our first argument is length (ie 120) and second argument is width (ie 56). And we are calculating the area by that function. And following is the function definition.

private static double FindArea(int length, int width)

{

try

{

return (length* width);

}

catch (Exception)

{

throw new NotImplementedException();

}

}

So in the first function call, we just passed the arguments by its position. Right?

double area;

Console.WriteLine("Area with positioned argument is: "); area = FindArea(120, 56);

Console.WriteLine(area); Console.Read();

If you run this, you will get an output as follows.

GoalKicker.com – C# Notes for Professionals

214

Now here it comes the features of a named arguments. Please see the preceding function call.

Console.WriteLine("Area with Named argument is: "); area = FindArea(length: 120, width: 56); Console.WriteLine(area);

Console.Read();

Here we are giving the named arguments in the method call.

area = FindArea(length: 120, width: 56);

Now if you run this program, you will get the same result. We can give the names vice versa in the method call if we are using the named arguments.

Console.WriteLine("Area with Named argument vice versa is: "); area = FindArea(width: 120, length: 56); Console.WriteLine(area);

Console.Read();

One of the important use of a named argument is, when you use this in your program it improves the readability of your code. It simply says what your argument is meant to be, or what it is?.

You can give the positional arguments too. That means, a combination of both positional argument and named argument.

Console.WriteLine("Area with Named argument Positional Argument : "); area = FindArea(120, width: 56); Console.WriteLine(area);

Console.Read();

In the above example we passed 120 as the length and 56 as a named argument for the parameter width.

There are some limitations too. We will discuss the limitation of a named arguments now.

Limitation of using a Named Argument

Named argument specification must appear after all fixed arguments have been specified.

GoalKicker.com – C# Notes for Professionals

215

If you use a named argument before a fixed argument you will get a compile time error as follows.

Named argument specification must appear after all fixed arguments have been specified

GoalKicker.com – C# Notes for Professionals

216