相关疑难解决方法(0)

为什么C#不支持引用的返回?

我已经读过.NET支持返回引用,但C#没有.有特殊原因吗?为什么我不能做这样的事情:

static ref int Max(ref int x, ref int y) 
{ 
  if (x > y) 
    return ref x; 
  else 
    return ref y; 
} 
Run Code Online (Sandbox Code Playgroud)

.net c# reference return-type

139
推荐指数
2
解决办法
2万
查看次数

是否可以在C#中返回对变量的引用?

我可以返回对double值的引用吗?

这就是我想要做的:

ref double GetElement()
{
   ......
   // Calculate x,y,z
   return ref doubleArray[x,y,z];
}
Run Code Online (Sandbox Code Playgroud)

要像这样使用它

void func()
{
   GetElement()=5.0;
}
Run Code Online (Sandbox Code Playgroud)

这就像在C++中返回一个双指针......我知道我写它的方式是错误的......但是有没有正确的方法呢?

c# reference return-value

29
推荐指数
4
解决办法
3万
查看次数

如何通过"引用"将其分配给c#中的类字段?

我试图了解如何通过"引用"分配给c#中的类字段.

我有以下示例要考虑:

 public class X
 {

  public X()
  {

   string example = "X";

   new Y( ref example );

   new Z( ref example );

   System.Diagnostics.Debug.WriteLine( example );

  }

 }

 public class Y
 {

  public Y( ref string example )
  {
   example += " (Updated By Y)";
  }

 }

 public class Z
 {

  private string _Example;

  public Z( ref string example )
  {

   this._Example = example;

   this._Example += " (Updated By Z)";

  }

 }

 var x = new X();
Run Code Online (Sandbox Code Playgroud)

运行上面的代码时,输​​出是:

X(由Y更新)

并不是: …

c# parameters field reference

27
推荐指数
2
解决办法
3万
查看次数

C#引用赋值运算符?

例如:

        int x = 1;
        int y = x;
        y = 3;
        Debug.WriteLine(x.ToString());
Run Code Online (Sandbox Code Playgroud)

是否为任何引用运算符而不是"="在行:3,使x等于3,如果我指定y = 3.

c# variables reference

4
推荐指数
1
解决办法
7135
查看次数

ref关键字在C#中返回类型之前的含义是什么

在下面的代码是什么的意义refGetAge()方法签名?

public class Person
{
    private int age;
    public ref int GetAge()
    {
        return ref this.age;
    }
}
Run Code Online (Sandbox Code Playgroud)

c#

4
推荐指数
1
解决办法
328
查看次数

标签 统计

c# ×5

reference ×4

.net ×1

field ×1

parameters ×1

return-type ×1

return-value ×1

variables ×1