Part 69 Making method parameters optional by specifying parameter defaults

preview_player
Показать описание
Tags
c# 4.0 named and optional parameters
c# optional parameters named
c# named parameters example
c# method named parameters
c# function named parameters
named parameters c# example
c# method default parameters
c# optional parameters default value
optional parameters must specify a default value
c# named parameters method

C#, SQL Server, WCF, MVC and ASP .NET video tutorials for beginners

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video, we will discuss making method parameters optional by specifying parameter defaults.

This method allows us to add any number of integers.
public static void AddNumbers(int firstNumber, int secondNumber,
int[] restOfTheNumbers)
{
int result = firstNumber + secondNumber;
foreach (int i in restOfTheNumbers)
{
result += i;
}

Console.WriteLine("Total = " + result.ToString());
}

If we want to add 5 integers - 10, 20, 30, 40 and 50. We call the method as shown below.
AddNumbers(10, 20, new int[]{30, 40, 50});

At the moment all the 3 parameters are mandatory. If I want to add just 2 numbers, then I can invoke the method as shown below. Notice that, I am passing an empty integer array as the argument for the 3rd parameter.
AddNumbers(10, 20, new int[]{});

We can make the 3rd parameter optional by specify a default value of null for the 3rd parameter.
public static void AddNumbers(int firstNumber, int secondNumber,
int[] restOfTheNumbers = null)
{
int result = firstNumber + secondNumber;

// loop thru restOfTheNumbers only if it is not null
// otherwise you will get a null reference exception
if (restOfTheNumbers != null)
{
foreach (int i in restOfTheNumbers)
{
result += i;
}
}
Console.WriteLine("Total = " + result.ToString());
}

Since we have specified a default value for the 3rd parameter, it is optional. So, if we want to add just 2 numbers, we can use the function as shown below.
AddNumbers(10, 20);

Optional parameters must appear after all required parameters
The following method will not comiple. This is because, we are making parameter "a" optional, but it appears before the required parameters "b" and "c".
public static void Test(int a = 10, int b, int c)
{
// Do something
}

The following method will compile, as optional parameter "a" is specified after all the required parameters ("b" & "c").
public static void Test(int b, int c, int a = 10)
{
// Do something
}

Named Parameters
In the following method, parameters "b" & "c" are optional.
public static void Test(int a, int b = 10, int c = 20)
{
Console.WriteLine("a = " + a);
Console.WriteLine("b = " + b);
Console.WriteLine("c = " + c);
}

When we invoke this method as shown below, "1" is paased as the argument for parameter "a" and "2" is passed as the argument for parameter "b" by default.
Test(1, 2);

My intention is to pass "2" as the argument for parameter "c". To achieve this we can make use of named parameters, as shown below. Notice that, I have specified the name of the parameter for which value "2" is being passed.
Test(1, c: 2);
Рекомендации по теме
Комментарии
Автор

You're a great teacher. Hats off. Respect Sir.

GreeeenEagle
Автор

You are "aaaabsoluutely" good teacher. :)

merkurunbabasi
Автор

"Is it possible? Aaaabsolutely!" xD at 5:56

georgeatester
Автор

Some best lines kudvenkat has ever said: 1."Is it possible?", venkat : Aaabsolutely. 2. "I strongly encourage you to watch that part of my video ".

rahulk
Автор

I thought, how sad is it, only 51 000 people viewed this video. After it I realized, this is the number of inhabitants of 20th biggest town in my country, and that is awesome! :-)

Trzbne
Автор

Its actually Named Arguments Venkat....you have specified as Named Parameter.
Thanks a lot for your valuable contribution to the world.

praveenajayaraman
Автор

You should say "thank you for watching" and not "for listening" :)

qqbamba
visit shbcf.ru