Declaring Method And Call Method With Name Argument

With the help of this method, we can manage our code in a proper way. Sometimes, we need to write some set of code more than one time in our program and it increases the size of a program and is not easy to manage the code to solve these problems. We used the method given below:

Syntax of declaring the method is given bellow,

  1. returnType methodeName (parameterList)  
  2. {  
  3. //method body  
  4. }  
returnType tells about return type of method. This can be any type int, string, float, void etc.

methodName is used to call the method. We should follow camelCase convention for the method name.

parameterList is an option. We provide parameterList or not, according to our need

Example
  1. void myFirstMethod()  
  2. {  
  3. // Do somthings  
  4. }  
  5. void mySecondMethod(int i)  
  6. {  
  7. }  
We must specify the type of the parameter and return type of a method. We can not use “var” keyword

How to return data from Method

If we want to return data from the method, first, define the return type of the method but it is not a void type. When we use void, it means the method should not return any data. We used the return keyword to achieve this target.

Example
  1. int addTwoValue (int first, int second)  
  2. {  
  3. return first + second;  
  4. }  
We remember that the return keyword should be used in the end of the method because when the control find returns the keyword it means that the function is finished and no single line will execute that method after the return keyword.

If you want the method, not return data, you should use void keyword.
  1. void displayInfo( int dataDisplay)  
  2. {  
  3. Console.WriteLine(dataDisplay);  
  4. }  
How to call method with name argument

This is best the feature of C#. C# uses the position of each parameter to determine which parameter is passed. We can change the position of the parameters, when we call the method.

Example
  1. void myMethod(int first, int second=10,string third=”hi”)  
  2. {  
  3. // do somthing  
  4. }  
Now, we call the method:
  1. myMethod(1,20,”C#”);  
  2. myMethod(first:10,second:10,third:”Name”);  
  3. myMethod(second:20,first:10,third:”C#”);  
The all calling method is correct. Name argument provides a facility to pass the parameter in any sequence. We can mix the position with name argument.
  1. myMethod(1,second:60,”C#”);  
How to resolve ambiguities with optional parameters and name arguments?

There is some possibility of ambiguity, when we used optional parameter and the name argument. How does compiler understand this ambiguity?

Let’s see an example, 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. namespace Method  
  7. {  
  8.     class Program   
  9.     {  
  10.         static void Main(string[] args) {  
  11.             myMethod(1, 0.0, "gg");  
  12.             myMethod(1, fourth: 300);  
  13.             Console.ReadKey();  
  14.         }  
  15.         public static void myMethod(int first, double second = 0.0, string third = "Hi") {  
  16.             Console.WriteLine("hi i am first");  
  17.         }  
  18.         public static void myMethod(int first, double second = 0.0, string third = "Hi"int fourth = 100) {  
  19.             Console.WriteLine("hi i am Scond");  
  20.         }  
  21.     }  
  22. }  
  23. void myMethod(int first, int second = 0, string third = ”Method”) {  
  24.     // Do something  
  25. }  
  26. void myMethod(int first, int second = 0, string third = ”Method”, int forth = 1) {  
  27.     //Do something  
  28. }  
When we call a method this way

myMethod (1,2,”Hello”);

Do you know which method is called? Don’t worry, i's  called first method, which has three parameters because the compiler checks closely match the method.

myMethod (1,forth:6);


Now, this is called a second method, which has a fourth argument.