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

C method parameter: ref

2022-06-13 03:07:00 cathy18c

class Program
    {
        static void Add(int num1)
        {
            num1++;
            Console.WriteLine("num1 The value of is :{0}", num1);
        }

        static void Main(string[] args)
        {            
            int num1 = 10;
            Add(num1);
            Console.WriteLine("num1 The value of is :{0}",num1);
        }

        
    }

The final output :

Add In the way num1 The value of is :11
Main In the way num1 The value of is :10 
stay Add The method is internal to num1 The modification of does not affect the num1 

When a value type is passed as an argument , You pass a copy of the value , Parameter received this value , Changing this value does not change the argument itself . But if we have a requirement to change this argument , And you can't use reference types , That is to say, we should pass the value type as an argument and change the value , Then use ref keyword .

void Method(ref int refArgument)
{
    refArgument = refArgument + 44;
}

int number = 1;
Method(ref number);
Console.WriteLine(number);
// Output: 45

  Just look at this example , The argument passes the value type to Method Method ,Method This value has been modified internally in the method , There is no need to return sentence , To it number Also changed. . This is it. ref Credit .ref The value type is passed by reference , Instead of changing a value type into a reference type

If you use ref keyword , The definition and call of that method should use ref, For example, the definition of the above example method void Method(ref int refArgument)  Method call Method(ref number)  With the ref

Pass on to ref Arguments to must initialize values , Take the example above , If number Not initialized to 1, It just defines int number; That's wrong , I can't compile , Tips “ An unassigned local variable is used number”

On overloading methods , Look at the code below

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) { }
}

Class members Cannot have only in ref、in or out Different signatures . If the only difference between two members of a type is that one of them has ref Parameters , And the other has out or in Parameters , A compiler error occurs . Everything else is the same , just ref out in Different , Not enough to prove that there are two different approaches .

The following is right

class RefOverloadExample
{
    public void SampleMethod(int i) { }
    public void SampleMethod(ref int i) { }
}

ref keyword , Initialization is required when variables are passed in , The value can be updated inside the method , And automatically return to the arguments that call it , Update the value of the argument  

ref Address / reference , This parameter must have been initialized when calling

Reference resources : Reference type and value type , And reference passing and value passing  

Method parameter (C# Reference resources )


 

 

原网站

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