Parameters of methods
In the last topic, a SayHello method was defined that outputs some message to the console:
void SayHello()
{
Console.WriteLine(“Hello”);
}
SayHello(); // Hello
But the disadvantage of such a method is that it outputs the same message. And it would be nice if we could dynamically determine what message the method will display on the screen, i.e. pass this message from outside to the method. For this purpose in C# language we can use parameters.
Parameters allow us to pass some input data to the method. Parameters are defined via a parenthesized hyphen after the name of the method in the form:
type_method_name_method (type_parameter1 parameter1, type_parameter2 parameter2, ...)
{
// method actions
}
The parameter definition has two parts: first comes the parameter type and then the parameter name.
For example, let's define the PrintMessage method, which receives the output message from outside:
void PrintMessage(string message)
{
Console.WriteLine(message);
}
PrintMessage(“Hello work”); // Hello work
PrintMessage(“Hello METANIT.COM”); // Hello METANIT.COM
PrintMessage(“Hello C#”); // Hello C#
Here, the PrintMessage() method takes one parameter, which is called message and is of type string.
To execute a method that has parameters, when the method is called, the values for its parameters are passed to it in parentheses after the method name, for example:
PrintMessage(“Hello work”);
Here the string “Hello work” is passed to the message parameter. The values passed to the parameters are also called arguments. That is, the passed string “Hello work” in this case is an argument.
Sometimes you can meet such definitions as formal parameters and actual parameters. Formal parameters are the actual parameters of the method (in this case message), and actual parameters are the values that are passed to the formal parameters. That is, the actual parameters are the arguments of the method.
Let's define another method that adds two numbers:
void Sum(int x, int y)
{
int result = x + y;
Console.WriteLine($"{x} + {y} = {result}”);s
}
Sum(10, 15); // 10 + 15 = 25
The Sum method has two parameters: x and y. Both parameters represent the int type. Therefore, when calling this method, we must pass two numbers in place of these parameters. Inside the method, the sum of the passed numbers is calculated and displayed on the console.
When calling the Sum method, values are passed to the parameters by position. For example, in the Sum(10, 15) call, the number 10 is passed to the x parameter, and the number 15 is passed to the y parameter.
Parameters can also be used in the abbreviated version of the method:
void Sum(int x, int y) => Console.WriteLine($“{x} + {y} = { x + y }”);
Sum(10, 15); // 10 + 15 = 25
The values passed to the parameter can represent the values of variables or the result of complex expressions that return some value:
void Sum(int x, int y) => Console.WriteLine($“{x} + {y} = { x + y }”);
int a = 10, b = 15, c = 6;
Sum(a, b); // 10 + 15 = 25
Sum(3, c); // 3 + 6 = 9
Sum(14, 4 + c); // 14 + 10 = 24
If variable values are passed as method parameters, such variables must be assigned a value. For example, the following program will not compile:
void Sum(int x, int y)
{
Console.WriteLine($"{x} + {y} = { x + y }”);
}
int a;
int b = 15;
Sum(a, b); // ! Error
Matching of parameters and arguments by data type
When passing values to parameters, it is important to consider the type of parameters: there must be a type match between arguments and parameters. For example:
void PrintPerson(string name, int age)
{
Console.WriteLine($"Name: {name} Age: {age}”);
}
PrintPerson(“Tom”, 24); // Name: Tom Age: 24
In this case, the first parameter of the PrintPerson() method represents the string type, so when calling the method, we must pass a string value, i.e. a string, to this parameter. The second parameter represents the int type, so we must pass it an integer, which corresponds to the int type.
PrintPerson(“Tom”, 24);
We can also pass to the parameters values of types that can be automatically converted to the parameter type. For example:
void PrintPerson(string name, int age)
{
Console.WriteLine($"Name: {name} Age: {age}”);
}
byte b = 37;
PrintPerson(“Tom”, b); // Name: Tom Age: 37
Here the int type parameter is passed a value of the byte type, but the comilator can automatically convert a value of the byte type to int type. That's why no error will occur here. What type conversions can be performed automatically was discussed in one of the previous topics: Conversions of basic data types.
We cannot pass data of other types to parameters. For example, the following call of the PrintPerson method will be incorrect:
PrintPerson(45, “Bob”); // Error! value mismatch with parameter types
Optional parameters
By default, when calling a method, you must provide values for all its parameters. But C# also allows you to use optional parameters. For such parameters we need to declare a default value. Also note that after optional parameters, all subsequent parameters must also be optional:
void PrintPerson(string name, int age = 1, string company = “Undefined”)
{
Console.WriteLine($"Name: {name} Age: {age} Company: {company}”
}
Here, the age and company parameters are optional because they are assigned values. Therefore, when calling the method, we may not pass data for them:
void PrintPerson(string name, int age = 1, string company = “Undefined”)
{
Console.WriteLine($"Name: {name} Age: {age} Company: {company}”
}
PrintPerson(“Tom”, 37, “Microsoft”); // Name: Tom Age: 37 Company: Microsoft
PrintPerson(“Tom”, 37); // Name: Tom Age: 37 Company: Undefined
PrintPerson(“Tom”); // Name: Tom Age: 1 Company: Undefined
Console output of the program:
Name: Tom Age: 37 Company: Microsoft
Name: Tom Age: 37 Company: Undefined
Name: Tom Age: 1 Company: Undefined
Named parameters
In the previous examples, when calling methods, the values for the parameters were passed in the order in which the parameters were declared in the method. That is, arguments were passed to parameters by position. But we can break this order by using named parameters:
void PrintPerson(string name, int age = 1, string company = “Undefined”)
{
Console.WriteLine($"Name: {name} Age: {age} Company: {company}”);
}
PrintPerson(“Tom”, company: “Microsoft”, age: 37); // Name: Tom Age: 37 Company: Microsoft
PrintPerson(age:41, name: “Bob”); // Name: Bob Age: 41 Company: Undefined
PrintPerson(company: “Google”, name: “Sam”); // Name: Sam Age: 1 Company: Google
To pass values to the parameters about the name, when calling the method, the name of the parameter and its value through a colon are specified: name: “Tom”.
Console output of the program:
Name: Tom Age: 37 Company: Microsoft
Name: Bob Age: 41 Company: Undefined
Name: Sam Age: 1 Company: Google