当前位置:网站首页>C method parameter: out

C method parameter: out

2022-06-13 03:07:00 cathy18c

out keyword , Only modify but not read , in other words , When an argument is passed to a formal parameter , Arguments cannot be initialized , Pay attention to whether it is necessary or not

int initializeInMethod;
OutArgExample(out initializeInMethod);
Console.WriteLine(initializeInMethod);     // value is now 44

void OutArgExample(out int number)
{
    number = 44;
}

You can't give  initializeInMethod Assign initial value to , Modify it within the method , After the method is over , This variable initializeInMethod Automatic value , no need return

Follow ref in equally , It is also used for both defining methods and invoking methods out

On method overloading and ref in equally , If everything else is the same , Only the signature of the method has ref in out distinguish , It is impossible to judge that they are not a method , as follows :

class CS0663_Example
{
    // Compiler error CS0663: "Cannot define overloaded
    // methods that differ only on ref and out".
    public void SampleMethod(out int i) { }
    public void SampleMethod(ref int i) { }
}

SampleMethod Method return value type , Method name , Parameter type , Number , The order , All the same , Only different modifiers , The system still recognizes them as a method , So there's a compilation error .

The following is true :
 

class OutOverloadExample
{
    public void SampleMethod(int i) { }
    public void SampleMethod(out int i) => i = 5;
}

Statement out Parameters  

Use out The parameter declaration method is a classic solution for returning multiple values

void Method(out int answer, out string message, out string stillNull)
{
    answer = 44;
    message = "I've been returned";
    stillNull = null;
}

int argNumber;
string argMessage, argDefault;
Method(out argNumber, out argMessage, out argDefault);
Console.WriteLine(argNumber);
Console.WriteLine(argMessage);
Console.WriteLine(argDefault == null);

// The example displays the following output:
//      44
//      I've been returned
//      True

Call with out Parameter method  

The original way of writing

string numberAsString = "1640";

int number;
if (Int32.TryParse(numberAsString, out number))
    Console.WriteLine($"Converted '{numberAsString}' to {number}");
else
    Console.WriteLine($"Unable to convert '{numberAsString}'");
// The example displays the following output:
//       Converted '1640' to 1640

from C# 7.0 Start , It can be declared in the parameter list of the method call rather than in a separate variable declaration out Variable . This makes the code more concise and readable , It also prevents inadvertent assignment of values to the variable before the method call . The following example is basically the same as the previous one , The difference is that it is right Int32.TryParse Method is defined number Variable .

string numberAsString = "1640";

if (Int32.TryParse(numberAsString, out int number))
    Console.WriteLine($"Converted '{numberAsString}' to {number}");
else
    Console.WriteLine($"Unable to convert '{numberAsString}'");
// The example displays the following output:
//       Converted '1640' to 1640

In the previous example ,number Variables are strongly typed to int. You can also declare an implicitly typed local variable , This is shown in the following example .

string numberAsString = "1640";

if (Int32.TryParse(numberAsString, out var number))
    Console.WriteLine($"Converted '{numberAsString}' to {number}");
else
    Console.WriteLine($"Unable to convert '{numberAsString}'");
// The example displays the following output:
//       Converted '1640' to 1640

 

Reference resources :out Parameter modifiers  

 

原网站

版权声明
本文为[cathy18c]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280534095267.html