Modifier | Remarks |
---|---|
(None) | value type: passed by value (a copy of data) reference type: passed by reference Note: string is by default passed by value. |
out | out parameters are passed by reference. Must be assigned in the method. |
in | to indicates that the ref parameters are read-only for the method. |
ref | passed by reference. The method may modify the intial value. |
params | only one params modifier is allowed in a method. It is a parameter with a variable number of arguments |
1. out parameters
out parameters could be discarded when not needed.
// long multi;
// long add;
// new in C# 7.0, out parameters could be directly declared inside the method call.
Calculator(2, 100, out long multi, out long add);
// Discarding out paramters (new 7.0) when not needed for better reading and perfomance.
Calculator(100, 200, out long multi2, out _);
Console.WriteLine($"The multiplication result is {multi}");
Console.WriteLine(($"the add result is {add}"));
Console.ReadLine();
static void Calculator(int x, int y, out long multi, out long add)
{
multi = x * y;
add = x + y;
}
2. ref parameters
int x = 100;
int y = 200;
Double(ref x, ref y);
Console.WriteLine($"The result of double is {x}, {y}");
Console.ReadLine();
static void Double(ref int x, ref int y)
{
x = x * 2;
y = y * 2;
}
// output:
// The result of double is 200, 400
3. in paramters
The in paramters are passed by reference byut prevents the called method to modify the values (read-only).
int x = 100;
int y = 2;
Console.WriteLine($"The result of double is {Double(x, y)}");
Console.ReadLine();
static int Double(in int x, in int y)
{
//x = x * 2; // CS8331: Cannot assign to variable 'in int' because it is readonly.
//y = y * 2; // CS8331: Cannot assign to variable 'in int' because it is readonly.
return x * y;
}
4. params paramters
// directly a list of doubles
var maximum1 = FindMaximum(1, 2, 40, 4);
// directly an array
double[] list = { 1, 20, 88, 102, 12 };
var maximum2 = FindMaximum(list);
Console.WriteLine($"The result is {maximum1}");
Console.WriteLine($"The result is {maximum2}");
Console.ReadLine();
static double FindMaximum(params double[] values)
{
double maximum = 0;
if (values.Length == 0) return maximum;
maximum = values[0];
for (int i = 1; i < values.Length; i++)
{
if (values[i] > maximum) maximum = values[i];
}
return maximum;
}
5. Optional Parameters and Named Arguments
The value for an optional paramter must be given at complie time. Error if given at runtime.
// using default optional value
int result1 = Double(10);
// specify an optional value
int result2 = Double(10, 10);
// using named arguments
int result3= Double(10, coefficient: 3);
Console.WriteLine($"The result is {result1}");
Console.WriteLine($"The result is {result2}");
Console.WriteLine($"The result is {result3}");
Console.ReadLine();
static int Double(int x, int coefficient = 2)
{
int result = x * coefficient;
return result;
}
6. Method Overloading
namespace ConsoleApp11
{
public static class Calculation
{
public static int Calculator(int x, int y)
{
int result = x * y;
return result;
}
public static double Calculator(double x, double y)
{
double result = y * x;
return result;
}
public static long Calculator(long x, long y)
{
long result = x * y;
return result;
}
}
}
using static ConsoleApp11.Calculation;
int result1 = Calculator(10, 10);
double result2 = Calculator(2.1, 2.3);
Console.WriteLine($"The result is {result1}");
Console.WriteLine($"The result is {result2}");
Console.ReadLine();