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

Passing value types to methods in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to pass the integer value to the method.

C Sharp (C#)
int number = 5;
ChangeValue([1]);

void ChangeValue(int num) {
    num = 10;
}
Console.WriteLine(number);
Drag options to blanks, or click blank then click option'
Aref number
Bnumber
Cout number
Din number
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ref' or 'out' without declaring the method parameter accordingly.
Expecting the original variable to change when passing by value.
2fill in blank
medium

Complete the method call to pass the integer by reference.

C Sharp (C#)
int value = 3;
ModifyValue([1]);

void ModifyValue(ref int val) {
    val = 7;
}
Console.WriteLine(value);
Drag options to blanks, or click blank then click option'
Aref value
Bin value
Cout value
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the ref keyword in the method call.
Using out instead of ref when the method expects ref.
3fill in blank
hard

Fix the error in the method call to correctly pass the value by output parameter.

C Sharp (C#)
int result;
Calculate(out [1]);

void Calculate(out int res) {
    res = 20;
}
Console.WriteLine(result);
Drag options to blanks, or click blank then click option'
Aout result
Bref result
Cresult
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using ref instead of out in the call.
Not initializing the variable before passing it as out (which is allowed).
4fill in blank
hard

Fill both blanks to correctly pass and modify the value type using ref.

C Sharp (C#)
int count = 1;
UpdateValue([1]);

void UpdateValue([2] int num) {
    num = 100;
}
Console.WriteLine(count);
Drag options to blanks, or click blank then click option'
Aref count
Bcount
Cref
Dout
Attempts:
3 left
💡 Hint
Common Mistakes
Using out keyword in method or call instead of ref.
Passing variable without ref when method expects ref.
5fill in blank
hard

Fill all three blanks to create a method that takes a value type by in parameter and call it correctly.

C Sharp (C#)
int data = 50;
DisplayValue([1]);

void DisplayValue([2] int val) {
    Console.WriteLine([3]);
}
Drag options to blanks, or click blank then click option'
Ain data
Bval
Cdata
Din
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting in keyword in method or call.
Trying to modify the parameter inside the method (not allowed with in).