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

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

Choose your learning style9 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.