ref模式和没有返回类型的常规函数​​之间的区别? [英] Difference between ref mode and general function with no return type ?

查看:91
本文介绍了ref模式和没有返回类型的常规函数​​之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

函数参数中的ref模式和
有什么区别

What is the difference between ref mode in a function argument and
The function having no return type?

推荐答案

很多.

函数返回类型与ref参数几乎没有关系或无关:以后要做的就是允许该方法访问调用方法的值并对其进行更改.如果需要,它可以用于提供其他返回值",但是这两项无关.

Lots.

A function return type has little or nothing to do with a ref parameter: all the later does is allow the method to access the value of, and make changes to, a value from the calling method. It can be used to provide additional "return values" if needed, but the two items are not related.

void A(string s)
    {
    Console.WriteLine(s);
    s = "Hello";
    }
void B(out string s)
    {
    //Console.WriteLine(s); Attempting to include this would cause a compiler error
    s = "Goodbye";
    }
void C(ref string s)
    {
    Console.WriteLine(s);
    s = "Whoops";
    }
void D()
    {
    string a = "123";
    A(a);
    Console.WriteLine(a);   // Prints 123, 123
    string b = "123";
    B(out b);
    Console.WriteLine(b);   // Prints Goodbye
    string c = "123";
    C(ref c);
    Console.WriteLine(c);   // Prints 123, Whoops
    }


在ref模式下,作为ref传入的值将改变,而不管函数是否返回任何内容.
In ref mode, the value passed in as ref will change irrespective of whether a function returns anything or not.


这篇关于ref模式和没有返回类型的常规函数​​之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆