0
0
C Sharp (C#)programming~5 mins

Passing reference types to methods in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What happens when you pass a reference type to a method in C#?
The method receives a copy of the reference, so it can modify the object's data, but reassigning the parameter inside the method won't affect the original reference outside.
Click to reveal answer
intermediate
How can you make a method change the reference itself (not just the object's data) in C#?
Use the ref keyword in the method parameter and when calling the method. This passes the reference by reference, allowing the method to reassign the original reference.
Click to reveal answer
beginner
True or False: Passing a reference type to a method always allows the method to change the original object.
True. The method can change the object's data because it has a reference to the same object in memory.
Click to reveal answer
intermediate
What is the difference between passing a reference type normally and passing it with ref in C#?
Normally, the method gets a copy of the reference and can change the object's data but not the reference itself. With ref, the method can change the reference to point to a new object, affecting the caller's variable.
Click to reveal answer
beginner
Consider this code snippet:<br><pre>class Person { public string Name; }<br>void ChangeName(Person p) { p.Name = "Alice"; }<br>Person person = new Person { Name = "Bob" };<br>ChangeName(person);<br>What is the value of <code>person.Name</code> after calling <code>ChangeName</code>?</pre>
The value is "Alice" because the method changed the data inside the object that person references.
Click to reveal answer
When you pass a reference type to a method without ref, what can the method change?
ANeither the object nor the reference
BThe reference to point to a new object
CThe object's data but not the reference itself
DBoth the object and the reference
How do you allow a method to change the reference variable itself in C#?
AUse the <code>out</code> keyword
BUse the <code>ref</code> keyword
CPass the object normally
DUse the <code>in</code> keyword
If a method changes a property of a passed reference type, will the change be visible outside the method?
AOnly if <code>ref</code> is used
BNo, because the method has a copy
COnly if <code>out</code> is used
DYes, because the object is shared
What does the ref keyword do when used with a reference type parameter?
AAllows the method to reassign the reference itself
BMakes the parameter read-only
CCopies the object before passing
DPrevents the method from modifying the object
Which statement is true about passing reference types to methods?
APassing a reference type copies the reference to the object
BPassing a reference type copies the entire object
CPassing a reference type always requires <code>ref</code>
DPassing a reference type prevents modification of the object
Explain what happens when you pass a reference type to a method in C# and how changes inside the method affect the original object.
Think about the difference between the reference and the object it points to.
You got /4 concepts.
    Describe how the ref keyword changes the behavior of passing reference types to methods in C#.
    Consider what happens if the method assigns a new object to the parameter.
    You got /3 concepts.