Method Parameters Flashcards
What is “out” parameter modifier used for?
The “out” keyword causes arguments to be passed by reference.
What are 2 differences between “out” and “ref” parameter modifiers?
When using “ref” you must initialize the variable that you will be passing. When using “out” you do not have to initialize the variable you will be passing.
What will be shown in Console?
class MyClass { private void MyMethod(out int myVar) { string aWord = "Hello"; Console.Write(aWord); }
int myInt = 5; MyMethod(out myInt); Console.Write(myInt); }
This code will not compile. The method parameter marked with “out” keyword must be assigned the value before control leaves that method.
This will result in compile time error:
“The out parameter ‘myVar’ must be assigned to before control leaves the current method”
Will this compile?
class ExampleClass { public void SampleMethod(out int i) { } public void SampleMethod(ref int i) { } }
No, this will not compile. “ref” and “out” modifiers are not considered part of the method signature at compile time.
Like “ref”, “out” parameter is passed by reference. True or false?
True.