“参考”关键字和引用类型

发布于 2024-10-11 09:06:37 字数 216 浏览 5 评论 0原文

我团队中的某人偶然发现了引用类型上 ref 关键字的特殊用法,

class A { /* ... */ } 

class B
{    
    public void DoSomething(ref A myObject)
    {
       // ...
    }
}

有什么理由让理智的人会做这样的事情吗?我在 C# 中找不到这个用途

someone in my team stumbled upon a peculiar use of the ref keyword on a reference type

class A { /* ... */ } 

class B
{    
    public void DoSomething(ref A myObject)
    {
       // ...
    }
}

Is there any reason someone sane would do such a thing? I can't find a use for this in C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

尸血腥色 2024-10-18 09:06:37

仅当他们想要将作为 myObject 传入的对象的引用更改为其他对象时。

public void DoSomething(ref A myObject)
{
   myObject = new A(); // The object in the calling function is now the new one 
}

这很可能不是他们想要做的,并且不需要 ref

Only if they want to change the reference to the object passed in as myObject to a different one.

public void DoSomething(ref A myObject)
{
   myObject = new A(); // The object in the calling function is now the new one 
}

Chances are this is not what they want to do and ref is not needed.

你丑哭了我 2024-10-18 09:06:37

class A
{
    public string Blah { get; set; }
}

void Do (ref A a)
{
    a = new A { Blah = "Bar" };
}

那么

A a = new A { Blah = "Foo" };
Console.WriteLine(a.Blah); // Foo
Do (ref a);
Console.WriteLine(a.Blah); // Bar

但是如果就

void Do (A a)
{
    a = new A { Blah = "Bar" };
}

那么

A a = new A { Blah = "Foo" };
Console.WriteLine(a.Blah); // Foo
Do (a);
Console.WriteLine(a.Blah); // Foo

Let

class A
{
    public string Blah { get; set; }
}

void Do (ref A a)
{
    a = new A { Blah = "Bar" };
}

then

A a = new A { Blah = "Foo" };
Console.WriteLine(a.Blah); // Foo
Do (ref a);
Console.WriteLine(a.Blah); // Bar

But if just

void Do (A a)
{
    a = new A { Blah = "Bar" };
}

then

A a = new A { Blah = "Foo" };
Console.WriteLine(a.Blah); // Foo
Do (a);
Console.WriteLine(a.Blah); // Foo
温柔戏命师 2024-10-18 09:06:37

如果该方法应该更改存储在传递给该方法的变量中的引用,则 ref 关键字非常有用。如果不使用ref,则无法更改引用,只能更改对象本身在方法外部可见。

this.DoSomething(myObject);
// myObject will always point to the same instance here

this.DoSomething(ref myObject);
// myObject could potentially point to a completely new instance here

The ref keyword is usefull if the method is supposed to change the reference stored in the variable passed to the method. If you do not use ref you can not change the reference only changes the object itself will be visible outside the method.

this.DoSomething(myObject);
// myObject will always point to the same instance here

this.DoSomething(ref myObject);
// myObject could potentially point to a completely new instance here
风筝在阴天搁浅。 2024-10-18 09:06:37

这并没有什么奇怪的。如果您想要从方法返回多个值或者只是不想将返回值重新分配给作为参数传入的对象,则可以引用变量。

像这样:

int bar = 4;
foo(ref bar);

而不是:

int bar = 4;
bar = foo(bar);

或者如果您想检索多个值:

int bar = 0;
string foobar = "";
foo(ref bar, ref foobar);

There's nothing peculiar with this. You reference variables if you want to return several values from a method or just don't want to reassign the return value to the object you've passed in as an argument.

Like this:

int bar = 4;
foo(ref bar);

instead of:

int bar = 4;
bar = foo(bar);

Or if you want to retrieve several values:

int bar = 0;
string foobar = "";
foo(ref bar, ref foobar);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文