Set of arguments that must
be provided for that method are parameters
Different ways of
passing parameters in C#
Pass by Value (Value Of
Parameter Passed)
Pass by ref (Memory Location or Ref of Parameter is passed)
Out ( variable is assigned a value before returning from a method)
Params ( any number of parameters of a particular type)
Pass by ref (Memory Location or Ref of Parameter is passed)
Out ( variable is assigned a value before returning from a method)
Params ( any number of parameters of a particular type)
See the below code
example
using System; using System.Text; class Test { static void DisplayUsingValue(int p) { ++p; } static void DisplayUsingRef(ref int p) { ++p; } static void DisplayUsingOut(int p , out int y) { y=p+2; } static int Add(params int[] iarr) { int sum = 0; foreach (int i in iarr) sum += i; return sum; } static void Main() { int x = 8; DisplayUsingValue(x); // make a copy of the value-type x Console.WriteLine("output From Pass By Value Method Demo "); Console.WriteLine(x); // x will still be 8 DisplayUsingRef(ref x); Console.WriteLine("output From Pass By Ref Method Demo "); Console.WriteLine(x); int a; DisplayUsingOut(x, out a); Console.WriteLine("output From out parameter Demo "); Console.WriteLine(a); int result = Add(1, 2,7); Console.WriteLine("output From params[] Demo "); Console.WriteLine(result); } }
And See the output
"output From Pass By Value Method Demo
8
output From Pass By Ref Method Demo
9
output From out parameter Demo
11
output From params[] Demo
10
No comments:
Post a Comment