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

Ref and out parameters 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 variable by reference using the correct keyword.

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

void ModifyNumber(ref int num) {
    num = 10;
}
Drag options to blanks, or click blank then click option'
Aref
Bout
Cin
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'out' instead of 'ref' when the variable is already initialized.
Forgetting to use the keyword in the method call.
2fill in blank
medium

Complete the code to correctly declare an out parameter in the method signature.

C Sharp (C#)
void GetValues(int input, [1] out int result) {
    result = input * 2;
}
Drag options to blanks, or click blank then click option'
Aout
Bref
Cin
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ref' instead of 'out' when the variable is not initialized before the call.
Not assigning a value to the out parameter inside the method.
3fill in blank
hard

Fix the error in the method call by adding the correct keyword for the out parameter.

C Sharp (C#)
int output;
CalculateSum(3, 4, [1] output);

void CalculateSum(int a, int b, out int sum) {
    sum = a + b;
}
Drag options to blanks, or click blank then click option'
Aref
Bin
Cout
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the keyword in the method call.
Using 'ref' instead of 'out' in the call.
4fill in blank
hard

Fill both blanks to correctly declare and call a method using ref parameters.

C Sharp (C#)
void Swap([1] int a, [2] int b) {
    int temp = a;
    a = b;
    b = temp;
}

int x = 1, y = 2;
Swap([1] x, [2] y);
Drag options to blanks, or click blank then click option'
Aref
Bout
Cin
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'out' instead of 'ref' when variables are already initialized.
Forgetting to use the keyword in the method call.
5fill in blank
hard

Fill all three blanks to create a method that uses an out parameter and call it correctly.

C Sharp (C#)
bool TryParseInt(string input, [1] out int number) {
    return int.TryParse(input, out [2] number);
}

string text = "123";
if (TryParseInt(text, [3] number)) {
    Console.WriteLine(number);
}
Drag options to blanks, or click blank then click option'
Aref
Bout
Cnumber
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ref' instead of 'out' in any of the places.
Using a different variable name in the call to int.TryParse.