1.

What Is A Parameter? Explain The New Types Of Parameters Introduced In C# 4.0.?

Answer»

A parameter is a special kind of variable, which is used in a function to provide a piece of information or input to a caller function. These inputs are called arguments.

In C#, the different types of parameters are as follows:

  • Value type - Refers that you do not NEED to provide any keyword with a parameter.
  • Reference type - Refers that you need to mention the ref keyword with a parameter.
  • Output type - Refers that you need to mention the out keyword with a parameter.
  • Optional parameter - Refers to the NEW parameter introduced in C# 4.0. It ALLOWS you to neglect the parameters that have some predefined default values.

The example of optional parameter is as follows:

public int Sum(int a, int b, int c = 0, int d = 0); /* c and d is optional */
Sum(10, 20); //10 + 20 + 0 + 0
Sum(10, 20, 30); //10 + 20 + 30 + 0
Sum(10, 20, 30, 40); //10 + 20 + 30 + 40

NAMED parameter - Refers to the new parameter introduced in C# 4.0. Now you can provide arguments by name rather than position.

The example of the named parameter is as follows:

public void CreateAccount(string name, string address = "unknown", int age = 0);
CreateAccount("Sara", age: 30);
CreateAccount(address: "India", name: "Sara");

A parameter is a special kind of variable, which is used in a function to provide a piece of information or input to a caller function. These inputs are called arguments.

In C#, the different types of parameters are as follows:

The example of optional parameter is as follows:

public int Sum(int a, int b, int c = 0, int d = 0); /* c and d is optional */
Sum(10, 20); //10 + 20 + 0 + 0
Sum(10, 20, 30); //10 + 20 + 30 + 0
Sum(10, 20, 30, 40); //10 + 20 + 30 + 40

• Named parameter - Refers to the new parameter introduced in C# 4.0. Now you can provide arguments by name rather than position.

The example of the named parameter is as follows:

public void CreateAccount(string name, string address = "unknown", int age = 0);
CreateAccount("Sara", age: 30);
CreateAccount(address: "India", name: "Sara");



Discussion

No Comment Found