Basically in C# , there are four types of Parameters -
Value Parameter-
Create a copy of parameter passed, so modification will not affect each other.
Reference Parameter-
The ref method parameter keyword on method parameter causes a method to refer to same variable that was passed into the method. Any changes made to the variable will be reflected in that variable when control passes back to the calling method.
Out Parameter-
Use when you want a method to return more than one value
Parameter Arrays -
The Param keyword lets you specify a method parameter that takes a variable number of arguments. Param keyword should be the last one in a method declaration.
Here is the Example-
int[] numbers = new int[4];
numbers[0] = 137;
numbers[1] = 138;
numbers[2] = 139;
numbers[3] = 140;
parammethod();
parammethod(numbers);
parammethod(1, 2, 3, 4, 5);
int j = 20;
int k = 0;
simplemethod(j);
Console.WriteLine(j);
simplemethod(ref k);
Console.WriteLine(k);
int total = 0;
int product = 0;
calculate(10, 20, out total, out product);
Console.WriteLine("sum={0} && Product={1}", total, product);
}
public static void calculate(int a, int b, out int sum, out int product)
{
sum = a + b;
product = a * b;
}
public static void simplemethod(ref int i)
{
i = 10;
}
//you can not have two param and it should be at the end
public static void parammethod(params int[] numbers)
{
Console.WriteLine("the total numbers are {0}", numbers.Length);
foreach (int i in numbers)
{
Console.WriteLine(i);
}
No comments:
Post a Comment
Please let me know if you have any doubts.