Method Parameters Flashcards

1
Q

What is “out” parameter modifier used for?

A

The “out” keyword causes arguments to be passed by reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are 2 differences between “out” and “ref” parameter modifiers?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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); }
A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Will this compile?

class ExampleClass
{
    public void SampleMethod(out int i) { }
    public void SampleMethod(ref int i) { }
}
A

No, this will not compile. “ref” and “out” modifiers are not considered part of the method signature at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Like “ref”, “out” parameter is passed by reference. True or false?

A

True.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly