lenelex.com
Home
Quizzes
Games
Register
Log in
Question 1 of 20
What is the output of the following program?
class Program { static void Main() { var a = new int[] {1, 2, 3}; var b = a; ByRef(ref a); ByVal(b); Console.Write($"{a.Length}, {b.Length}"); } static void ByRef(ref int[] p) { Array.Resize(ref p, p.Length-1); } static void ByVal(int[] p) { Array.Resize(ref p, p.Length - 1); } }
A. 2, 3
B. 2, 0
C. 2, 2
D. 3, 3
Passing a reference-type variable by value
A variable of a reference type does not contain its data directly; it contains a reference to its data. When you pass a reference-type parameter by value, it is possible to change the data pointed to by the reference, such as the value of a class member. However, you cannot change the value of the reference itself.
Passing a reference-type variable by reference
When passing a reference-type variable to a method by reference, any changes that take place in the method affect the original variable in the calling program.
Links:
Passing Arrays as Arguments
The [ref] Keyword