ref vs out方法? [英] ref vs out methods ?

查看:108
本文介绍了ref vs out方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再次问好。



所以我读过,唯一不同的是,在ref方法中,你必须在调用方法之前初始化var或类似的东西。我试图在网络和其他资源上找到解决方案,但我脑子里的概念仍然不明确。



1)我必须知道的主要区别是什么一个bigginer / starter?

2)这些方法在哪里以及如何(即使这样相互模拟)可以帮助我完成任务?

您使用的2个问题的示例ref和out会很棒。



提前致谢

Hello again.

So i have read that the only diference is that at the ref method you must intialize the var before you call the method or something like that. I tried to find a solution at net and other resources but still the concept is not clear in my head.

1)Whats the main differences that i must know atm as a bigginer/starter?
2) Where and How these methods (even so simmilar to each other) can help me with tasks?
An example of 2 problems that you use ref and out would be amazing.

Thanks in advance

推荐答案

输出参数:

out parameters:
string s;
MyMethod(out s);
...
private void MyMethod(out string par)
   {
   par = "hello";
   }

在将参数传递给方法之前,您不需要为参数赋值,因为该方法必须在退出之前为参数赋值(如果参数退出,则会出现错误它没有) - 值将传回到调用方法 - 所以s将获得值hello

在MyMethod中分配值之前使用par将给你一个错误。



ref参数:

You do not need to assign a value to the parameter before you pass it to the method, as the method must assign value to the parameter before it exits (you will get an error if it doesn't) - the value will be "passed back" into the calling method - so "s" will get the value "hello"
Using "par" before you assign a value in MyMethod will give you an error.

ref parameters:

string s = "";
    MyMethod(ref s);
    }
private void MyMethod(ref string par)
    {
    Console.WriteLine(par);
    par = "hello";
    }

在将参数传递给方法之前,必须为参数赋值,因为该方法可以使用它传递的值。在退出之前没有必要为参数赋值(但是如果你愿意的话你也可以) - 该值将传回到调用方法中 - 所以s将获得值hello

You must assign a value to the parameter before you pass it to the method, as the method is able to use the value it is passed. It is not necessary to assign value to the parameter before it exits (but you can if you want) - the value will be "passed back" into the calling method - so "s" will get the value "hello"


public static void NormalTest(int x)
{
    x++;
    Console.WriteLine(x);
}

public static void OutTest(out int x)
{
    x = 5;
    Console.WriteLine(x);
}

public static void RefTest(ref int x)
{
    x++;
    Console.WriteLine(x);
}

static void Main(string[] args)
{
    int y = 1;
    NormalTest(y); // "2"
    Console.WriteLine(y); // "1"
    // y's value hasn't changed despite the param incrementing in the NormalTest function

    y = 1;
    OutTest(out y); // "5"
    Console.WriteLine(y); // "5"
    // y's initial value is irrelevant to the OutTest function and after the function
    // y's value has changed

    y = 1;
    RefTest(ref y); // "2"
    Console.WriteLine(y); // "2"
    // the "y" parameter keeps the value it was changed to inside the RefTest function.
    // compare this to the NormalTest where it wasn't changed

    // a normal param is an input to the function
    // an out param is an output from the function
    // a ref param is input and output

    Console.ReadKey();
}


这篇关于ref vs out方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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