ProAnswers.org

What is a parameter? Explain the new types of parameters introduced in C# 4.0 ?

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");