brainbench test with no answer?
-
I took the Brain Bench test and was asked this question. I thin the answer should be 0,30,30 but it wasn't listed. What am I doing wrong? public static void Main() { Coordinates c1 = new Coordinates(); Coordinates c2 = new Coordinates(); int x = 30; c1.X = 30; c2.X = 30; Test(ref c1, c2, x); Console.WriteLine("C1.X=" + c1.X.ToString() + ", C2.X=" + c2.X.ToString() + ", X=" + x.ToString()); Console.Read(); } public static void Test(ref Coordinates Coord1, Coordinates Coord2, int X) { Coord1 = new Coordinates(); Coord2 = new Coordinates(); Coord1.X = 0; Coord2.X = 0; X = 0; } What is the console output for the above sample code? Choice 1 C1.X=30, C2.X=30, X=30 Choice 2 C1.X=30, C2.X=0, X=00 Choice 3 C1.X=0, C2.X=0, X=30 Choice 4 C1.X=0, C2.X=0, X=0 Thanks in advance for you help
-
I took the Brain Bench test and was asked this question. I thin the answer should be 0,30,30 but it wasn't listed. What am I doing wrong? public static void Main() { Coordinates c1 = new Coordinates(); Coordinates c2 = new Coordinates(); int x = 30; c1.X = 30; c2.X = 30; Test(ref c1, c2, x); Console.WriteLine("C1.X=" + c1.X.ToString() + ", C2.X=" + c2.X.ToString() + ", X=" + x.ToString()); Console.Read(); } public static void Test(ref Coordinates Coord1, Coordinates Coord2, int X) { Coord1 = new Coordinates(); Coord2 = new Coordinates(); Coord1.X = 0; Coord2.X = 0; X = 0; } What is the console output for the above sample code? Choice 1 C1.X=30, C2.X=30, X=30 Choice 2 C1.X=30, C2.X=0, X=00 Choice 3 C1.X=0, C2.X=0, X=30 Choice 4 C1.X=0, C2.X=0, X=0 Thanks in advance for you help
brsecu wrote:
What am I doing wrong?
Nothing. You are right, whether Coordinates is a class or a struct, it is 0,30,30 :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
I took the Brain Bench test and was asked this question. I thin the answer should be 0,30,30 but it wasn't listed. What am I doing wrong? public static void Main() { Coordinates c1 = new Coordinates(); Coordinates c2 = new Coordinates(); int x = 30; c1.X = 30; c2.X = 30; Test(ref c1, c2, x); Console.WriteLine("C1.X=" + c1.X.ToString() + ", C2.X=" + c2.X.ToString() + ", X=" + x.ToString()); Console.Read(); } public static void Test(ref Coordinates Coord1, Coordinates Coord2, int X) { Coord1 = new Coordinates(); Coord2 = new Coordinates(); Coord1.X = 0; Coord2.X = 0; X = 0; } What is the console output for the above sample code? Choice 1 C1.X=30, C2.X=30, X=30 Choice 2 C1.X=30, C2.X=0, X=00 Choice 3 C1.X=0, C2.X=0, X=30 Choice 4 C1.X=0, C2.X=0, X=0 Thanks in advance for you help
The answer is Choice 3 (0,0,30) if Coordinates is a class, or your answer if Coordinates is a Struct ALL classes in C# are reference types so regardless if you use the ref keyword or not, they are passed by reference. If Coordinates is a struct instead of a Class then the first parameter is passed by reference and the other 2 are passed by value.
-
The answer is Choice 3 (0,0,30) if Coordinates is a class, or your answer if Coordinates is a Struct ALL classes in C# are reference types so regardless if you use the ref keyword or not, they are passed by reference. If Coordinates is a struct instead of a Class then the first parameter is passed by reference and the other 2 are passed by value.
Nope. Just run the code, and you will see the result is 0,30,30 whether Coordinates is a struct or a class. The Test() method replaces both Coordinates by new ones, however only the first one makes it back to the caller, thanks to the ref keyword; the second Coordinates is just local to Test(). :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.