Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Passing reference types to methods in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Reference Type Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code when passing a reference type to a method?

Consider the following C# code:

class Box { public int Value; }

void ChangeBox(Box b) {
    b.Value = 10;
}

Box myBox = new Box { Value = 5 };
ChangeBox(myBox);
Console.WriteLine(myBox.Value);

What will be printed?

C Sharp (C#)
class Box { public int Value; }

void ChangeBox(Box b) {
    b.Value = 10;
}

Box myBox = new Box { Value = 5 };
ChangeBox(myBox);
Console.WriteLine(myBox.Value);
A5
B10
C0
DCompilation error
Attempts:
2 left
💡 Hint

Remember that classes are reference types in C#. Changing a property inside the method affects the original object.

❓ Predict Output
intermediate
2:00remaining
What happens when you assign a new object to a reference parameter inside a method?

Look at this code:

class Container { public int Number; }

void ResetContainer(Container c) {
    c = new Container { Number = 0 };
}

Container cont = new Container { Number = 5 };
ResetContainer(cont);
Console.WriteLine(cont.Number);

What will be printed?

C Sharp (C#)
class Container { public int Number; }

void ResetContainer(Container c) {
    c = new Container { Number = 0 };
}

Container cont = new Container { Number = 5 };
ResetContainer(cont);
Console.WriteLine(cont.Number);
A5
B0
CCompilation error
DNullReferenceException
Attempts:
2 left
💡 Hint

Assigning a new object to the parameter inside the method does not change the original reference outside.

🔧 Debug
advanced
2:00remaining
Why does this code throw a NullReferenceException?

Examine this code snippet:

class Data {
    public string Text;
}

void ClearText(Data d) {
    d.Text = null;
}

Data data = null;
ClearText(data);
Console.WriteLine(data.Text);

Why does this code throw a NullReferenceException?

C Sharp (C#)
class Data {
    public string Text;
}

void ClearText(Data d) {
    d.Text = null;
}

Data data = null;
ClearText(data);
Console.WriteLine(data.Text);
ABecause Text is null and cannot be assigned
BBecause ClearText method is static but called on instance
CBecause data is null and accessing d.Text causes an exception
DBecause Console.WriteLine cannot print null values
Attempts:
2 left
💡 Hint

Think about what happens when you try to access a member of a null object.

📝 Syntax
advanced
2:00remaining
Which option correctly passes a reference type by reference to modify the original reference?

Given this code:

class Item { public int Id; }

void ReplaceItem(??? item) {
    item = new Item { Id = 99 };
}

Item myItem = new Item { Id = 1 };
ReplaceItem(myItem);
Console.WriteLine(myItem.Id);

Which method signature for ReplaceItem will make myItem.Id print 99?

C Sharp (C#)
class Item { public int Id; }

void ReplaceItem(??? item) {
    item = new Item { Id = 99 };
}

Item myItem = new Item { Id = 1 };
ReplaceItem(myItem);
Console.WriteLine(myItem.Id);
Avoid ReplaceItem(ref Item item)
Bvoid ReplaceItem(Item item)
Cvoid ReplaceItem(out Item item)
Dvoid ReplaceItem(in Item item)
Attempts:
2 left
💡 Hint

To change the original reference, you must pass the parameter by reference.

🚀 Application
expert
3:00remaining
How many items are in the list after this method call modifies it?

Analyze this code:

void AddItems(List list) {
    list.Add("A");
    list = new List();
    list.Add("B");
}

var myList = new List();
AddItems(myList);
Console.WriteLine(myList.Count);

What number will be printed?

C Sharp (C#)
void AddItems(List<string> list) {
    list.Add("A");
    list = new List<string>();
    list.Add("B");
}

var myList = new List<string>();
AddItems(myList);
Console.WriteLine(myList.Count);
ACompilation error
B2
C0
D1
Attempts:
2 left
💡 Hint

Remember that assigning a new list inside the method does not affect the original list reference.

Practice

(1/5)
1. What happens when you pass a reference type object to a method in C# without using ref keyword?
easy
A. The method can modify the object's properties, affecting the original object.
B. The method cannot modify the object's properties at all.
C. The method receives a copy of the object and changes do not affect the original.
D. The method can reassign the original reference to a new object automatically.

Solution

  1. Step 1: Understand passing reference types

    When a reference type is passed to a method, the method receives a copy of the reference, pointing to the same object.
  2. Step 2: Effect on object properties

    Modifying the object's properties inside the method changes the original object because both references point to the same memory.
  3. Final Answer:

    The method can modify the object's properties, affecting the original object. -> Option A
  4. Quick Check:

    Reference type properties change original [OK]
Hint: Reference types allow property changes to affect original object [OK]
Common Mistakes:
  • Thinking the object is copied entirely
  • Confusing reference and value types
  • Assuming reassignment changes original reference
2. Which of the following method signatures correctly allows a method to reassign a reference type parameter so the caller sees the new object?
easy
A. void UpdateObject(MyClass obj) { obj = new MyClass(); }
B. void UpdateObject(ref MyClass obj) { obj = new MyClass(); }
C. void UpdateObject(out MyClass obj) { obj = new MyClass(); }
D. void UpdateObject(in MyClass obj) { obj = new MyClass(); }

Solution

  1. Step 1: Understand parameter passing keywords

    The ref keyword allows the method to reassign the caller's reference variable.
  2. Step 2: Analyze each signature

    void UpdateObject(ref MyClass obj) { obj = new MyClass(); } uses ref and assigns a new object, so the caller's reference changes. void UpdateObject(MyClass obj) { obj = new MyClass(); } reassigns local copy only. void UpdateObject(out MyClass obj) { obj = new MyClass(); } requires assignment but is for output parameters. void UpdateObject(in MyClass obj) { obj = new MyClass(); } is readonly and cannot reassign.
  3. Final Answer:

    void UpdateObject(ref MyClass obj) { obj = new MyClass(); } -> Option B
  4. Quick Check:

    Use ref to reassign reference type parameter [OK]
Hint: Use ref keyword to let method reassign caller's reference [OK]
Common Mistakes:
  • Using no keyword and expecting reassignment to affect caller
  • Confusing out and ref keywords
  • Trying to reassign with in parameter
3. What is the output of the following C# code?
class Person {
  public string Name;
}

void ChangeName(Person p) {
  p.Name = "Alice";
  p = new Person();
  p.Name = "Bob";
}

Person person = new Person();
person.Name = "John";
ChangeName(person);
Console.WriteLine(person.Name);
medium
A. John
B. null
C. Bob
D. Alice

Solution

  1. Step 1: Trace property change before reassignment

    The method sets p.Name = "Alice", changing the original object's Name to "Alice".
  2. Step 2: Reassignment inside method does not affect caller

    Then p is assigned a new Person object with Name "Bob", but this does not affect the original reference outside the method.
  3. Final Answer:

    Alice -> Option D
  4. Quick Check:

    Property change affects original; reassignment does not [OK]
Hint: Property changes affect original; reassignment inside method does not [OK]
Common Mistakes:
  • Thinking reassignment changes original reference
  • Expecting output to be 'Bob'
  • Ignoring property change before reassignment
4. Identify the error in the following code that tries to update a reference type parameter so the caller sees the new object:
class Data {
  public int Value;
}

void ResetData(Data d) {
  d = new Data();
  d.Value = 0;
}

Data data = new Data();
data.Value = 5;
ResetData(data);
Console.WriteLine(data.Value);
medium
A. The method should use ref keyword to reassign the caller's reference.
B. The method should not create a new object inside.
C. The method should assign d.Value = 5 instead of 0.
D. The method should return the new object instead of void.

Solution

  1. Step 1: Understand reassignment effect without ref

    Assigning d = new Data() inside the method only changes the local copy of the reference, not the caller's variable.
  2. Step 2: Fix by using ref keyword

    Using ref in the method parameter and call allows the method to reassign the caller's reference to the new object.
  3. Final Answer:

    The method should use ref keyword to reassign the caller's reference. -> Option A
  4. Quick Check:

    Use ref to reassign reference type parameter [OK]
Hint: Use ref keyword to let method reassign caller's reference [OK]
Common Mistakes:
  • Expecting reassignment without ref to affect caller
  • Thinking returning new object is required
  • Confusing property assignment with reference reassignment
5. You want a method to update a reference type parameter's properties and also optionally replace the whole object with a new one, so the caller sees all changes. Which approach correctly achieves this in C#?
hard
A. Pass the object with out keyword; assign new object inside method but do not modify properties.
B. Pass the object normally and modify properties; reassigning inside method updates caller automatically.
C. Pass the object with ref keyword; modify properties and reassign new object inside method.
D. Pass the object with in keyword; modify properties but cannot reassign the object.

Solution

  1. Step 1: Understand property modification vs reassignment

    Modifying properties affects original object without ref, but reassignment does not affect caller unless ref is used.
  2. Step 2: Use ref to allow both property changes and reassignment

    Passing with ref lets method modify properties and replace the object so caller sees all changes.
  3. Final Answer:

    Pass the object with ref keyword; modify properties and reassign new object inside method. -> Option C
  4. Quick Check:

    Use ref to modify and reassign reference type parameter [OK]
Hint: Use ref to modify properties and reassign object in method [OK]
Common Mistakes:
  • Assuming normal pass allows reassignment to affect caller
  • Using out when input object is needed
  • Using in which forbids reassignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard