简体   繁体   English

将ref关键字与引用类型参数一起使用有什么好处?

[英]What is the advantage of using the ref keyword with a reference type parameter?

What do I gain, as the developer, by passing a reference type argument to a method using the ref keyword if you are not changing what the reference points to ? 如果您不更改引用指向的内容 ,作为开发人员,通过使用ref关键字将引用类型参数传递给方法,我将获得什么

I have read both, ref keyword with reference type parameter and What is the use of "ref" for reference-type variables in C#? 我已经阅读了带有引用类型参数的ref关键字C#中引用类型变量的“ ref”的用法? and a number of others. 和其他一些。 I am aware this subject has been asked about and answered a great number of times. 我知道这个问题已经被问了很多遍了。 I believe I am asking a unique question. 我相信我在问一个独特的问题。 Feel free to mark down my question if you know other wize. 如果您知道其他Wize,请随意写下我的问题。

Simple class: 简单类:

public class Person
{
    public Person(string first, string last)
    {
        FirstName = first;
        LastName = last;
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public override string ToString()
    {
        return string.Format("{0} {1}", FirstName, LastName);
    }
}

Form passing using keyword 'ref': 使用关键字“ ref”传递表单:

public partial class Form2 : Form
{
    private Person _person;

    public Form2()
    {
        _person = new Person("John", "Doe");
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        textBox1.Text = _person.ToString();

        base.OnLoad(e);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ChangeLastName(ref _person);

        textBox2.Text = _person.ToString();
    }

    private static void ChangeLastName(ref Person person)
    {
        person.LastName = "Smith";
    }
}

Form passing parameter without using keyword 'ref': 不使用关键字“ ref”的形式传递参数:

public partial class Form2 : Form
{
    private Person _person;

    public Form2()
    {
        _person = new Person("John", "Doe");
        InitializeComponent();
    }

    protected override void OnLoad(EventArgs e)
    {
        textBox1.Text = _person.ToString();

        base.OnLoad(e);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ChangeLastName(_person);

        textBox2.Text = _person.ToString();
    }

    private static void ChangeLastName(Person person)
    {
        person.LastName = "Smith";
    }
}

What is the difference if any or what as a developer do I gain from the former (other than the ability to new up the passed parameter inside my method)? 我可以从前者那里得到什么(如果有的话)或从开发者那里得到什么(除了可以在我的方法中更新传递的参数的能力)?

Thanks in advance, 提前致谢,

What do I gain, as the developer, by passing a reference type argument to a method using the ref keyword if you are not changing what the reference points to? 如果您不更改引用指向的内容,作为开发人员,通过使用ref关键字将引用类型参数传递给方法,我将获得什么?

None whatsoever - that's the only purpose of passing a reference type by reference is if you are going to change what the reference refers to. 无任何问题-通过引用传递引用类型的唯一目的是,如果您要更改引用所引用的内容。

To be fair - it gives you the ability to change that behavior in the future, which would be sadistic and cruel... 公平地说-它使您能够在将来改变这种行为,这将是虐待和残忍的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM