繁体   English   中英

C# 使用对象(正确的 byRef Byval 问题)

[英]C# Working with objects (properly a byRef Byval problem)

我有我的压缩机类:

public class Compressors
{

    public Refrigerant Output { get; set; }
    public Refrigerant Input { get; set; }


// Constructor
    public Compressors(Refrigerant input)
    {
        Input = input; 
    }

}

在我的主要代码中,我有:

var Compressor1 = new Compressors(BeforeComp);
var Compressor2 = new Compressors(Compressor1.Output);

看着就好

Compressor1.Input
Compressor1.Output

但是看着

Compressor2.Input //Should be the same as Compressor1.Output!

返回空错误

总结一下,我希望 Compressor1.Output 和 Compressor2.Input 引用同一个对象。 我究竟做错了什么?

代码按预期工作。
您永远不会初始化Compressor1.Output 因此它是null 您将此值用作Compressor2构造函数的输入,然后您将在Compressor2.Input看到该值。

换句话说:

Assert.Equal(Compressor1.Output, Compressor2.Input);
Assert.Null(Compressor1.Output);
Assert.Null(Compressor2.Input);

感谢您的反馈! 它让我看向别处

我用过这个:

public Refrigerant ShallowCopy()
{
return (Refrigerant)this.MemberwiseClone();
}

复制存储在我的制冷剂类中的值。 我认为这只会复制值,所以我不必

Output.x = Input.x
Output.y = Input.y
Output.z = Input.z

但它没有按预期工作

暂无
暂无

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

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