gpt4 book ai didi

C# 检查我是否理解正确的赋值

转载 作者:行者123 更新时间:2023-11-30 17:41:19 26 4
gpt4 key购买 nike

所以我正在学习 C#,但我在编写程序时遇到了一些麻烦。我只是想检查一下我是否正确理解变量赋值。以下行为是否像我想的那样?

SomeObject someObject; // declares a SomeObject object called someObject
SomeObject someReference; // declares a SomeObject object called someReference
SomeObject someOtherObject; // declares a SomeObject object called someOtherObject

someObject = new SomeObject(); // initialises a new SomeObject object into someObject using SomeObject's contructor
someOtherObject = new SomeObject(); // initialises a new SomeObject object into someOtherObject using SomeObject's constructor
someReference = someObject; // someReference is now a reference pointing to the same place as someObject

someReference.attribute = value; // sets someReference's attribute attribute to value. someObject.attribute is also now value

someReference = someOtherObject; // someReference now points to someOtherObject instead of someObject
someReference.attribute = value2; // someOtherObject.attribute is now value2. someObject.attribute is unaffected

someReference = null; // sets someReference to be a null reference. someObject and someOtherObject are unaffected.

最佳答案

我会略微更改您的一些至少对我来说似乎有点不准确的评论(其他评论没问题):

// declares a field (if it's in class) or a variable (if it's inside method) 
// of type SomeObject that will later work with SomeObject instance

SomeObject someObject;

...

// instantiates a new SomeObject instance (including creation of object on heap,
// pointer to this object and running object constructor).
// It also makes someObject point to this newly created object.

someObject = new SomeObject();
...

// sets new value for someReference's 'attribute' field (or property)

someReference.attribute = value;

...

关于C# 检查我是否理解正确的赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33210102/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com