Complete the code to pass the variable by reference using the correct keyword.
int number = 5; ModifyNumber([1] number); void ModifyNumber(ref int num) { num = 10; }
The ref keyword is used to pass a variable by reference, allowing the method to modify the original variable.
Complete the code to correctly declare an out parameter in the method signature.
void GetValues(int input, [1] out int result) { result = input * 2; }
The out keyword declares an output parameter that the method must assign before it returns.
Fix the error in the method call by adding the correct keyword for the out parameter.
int output; CalculateSum(3, 4, [1] output); void CalculateSum(int a, int b, out int sum) { sum = a + b; }
The out keyword must be used in both the method signature and the call when passing an out parameter.
Fill both blanks to correctly declare and call a method using ref parameters.
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);
The ref keyword must be used in both the method parameters and the call to pass variables by reference.
Fill all three blanks to create a method that uses an out parameter and call it correctly.
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); }
int.TryParse.The method declares an out parameter, and the call to int.TryParse also uses out with the variable number. The call to TryParseInt uses out as well.