这个函数如何改变我的只读变量?
我在 C# 中使用 jabber.net,有一个怪癖让我感到困惑,所以希望有人能解释它是如何做到的。我对 C++ 背景的 C# 还很陌生,所以这可能是一个误解,
我正在创建一个只读 JID 来保存我需要进入的房间:
private readonly JID messagingRoomJID;
public MyClass
{
// Reads from database but for purpose of this question
messagingRoomJID = new JID("user@myserver");
}
稍后我想加入一个房间,这就是我得到我所在的东西的地方困惑
conferenceManager = new ConferenceManager();
conferenceManager.Stream = xmppClient;
// Before entering this room messagingRoomJID.Resource == null
theRoom = conferenceManager.GetRoom(messagingRoomJID);
// But now messagingRoomJID.Resource contains my username
但是资源是如何改变的?该变量是只读的,但也不应该通过 const 引用传递,那么它是如何更新的呢?
看起来我可以这样做,但我不确定这是否明智:
theRoom = conferenceManager.GetRoom(new JID(messagingRoomJID));
I am using jabber.net in C# and there is a quirk that has confused me so hopefully someone can explain how it is doing it. I am still quite new to C# from a C++ background so it may be a misunderstanding there
I am creating a readonly JID to hold a room that I need to enter:
private readonly JID messagingRoomJID;
public MyClass
{
// Reads from database but for purpose of this question
messagingRoomJID = new JID("user@myserver");
}
Later I want to join a room which is where I get the thing I am confused by
conferenceManager = new ConferenceManager();
conferenceManager.Stream = xmppClient;
// Before entering this room messagingRoomJID.Resource == null
theRoom = conferenceManager.GetRoom(messagingRoomJID);
// But now messagingRoomJID.Resource contains my username
But how is Resource being changed? The variable is readonly but also shouldn't it be passed by const reference, so how is it being updated anyway?
It looks like I can do it by this but I am not sure it is sensible:
theRoom = conferenceManager.GetRoom(new JID(messagingRoomJID));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
它不会改变变量的值。变量的值只是一个参考。该参考与以前保持不变。
发生变化的是引用所引用的对象内的数据。
readonly
修饰符 only 适用于您的变量 - 它没有说明对象本身的可变性。区分引用和对象非常重要 - 请参阅我关于该主题的文章了解更多信息信息。
It's not changing the value of your variable. The value of your variable is just a reference. That reference is staying the same as it was before.
What's changing is the data within the object that the reference refers to. The
readonly
modifier only applies to your variable - it doesn't say anything about the mutability of the object itself.It's very important to distinguish between references and objects - see my article on the topic for more information.
因为它正在更改只读变量的属性,而只有根元素本身是只读的。
例如,
我们可以在构造函数中更改它一次;但对于包含属性、公开字段等的复杂类型,我们可以在以后更改它们。这就是为什么(对于一个常见的例子)拥有一个只读数组通常是一个坏主意,或者至少可以想象它通常不是预期的 - 因为数组的元素可以更改。
Because it is changing a property of your
readonly
variable, whereas only the root element is readonly per se.For instance,
So, we can change it once, in the constructor; but in the case of a complex type containing properties, exposed fields and such and so, we can alter those ever after. This is why (for a common example) having a
readonly
array is generally a bad idea, or at least conceivable that it often isn't what is intended - since the elements of the array can be changed.所以,我认为乔恩·斯基特的回答是正确的。
例如,您的类变量 messagingRoomJID 是只读的且是引用,因此您无法将其分配给新值,但您当然可以更改其所有属性的值,因为属性“Resource”不是只读的。
GetRoom() 方法调用不会更改内存中的 messagesRoomJID 变量引用,它只会更改其允许的属性之一的值。
So, I consider Jon Skeet's answer as correct.
As an example, your class variable messagingRoomJID is readonly and a reference, so you can't assign it to a new value, but you can certainly change the values of all its properties because the property "Resource" is not readonly.
The GetRoom() method call doesnt change the messagingRoomJID variable reference in memory, it only changes the value of one of its properties which is allowed.
readonly
表示对实例的引用不会更改。但您可以更改该对象的任何字段。当传递给方法GetRoom
时,它是通过引用传递的。因此,GetRoom
可以更改任何对象的字段。readonly
means that reference to the instance will not be changed. But you can change any field of this object. When is passed to methodGetRoom
, its passed by reference. SoGetRoom
can change any object's field.根据 MSDN ,可以在声明期间或使用构造函数。
但是,正如在您的代码中一样,您在初始化期间或使用构造函数时都不会更改,但属性
Resource
仍然包含该值。正如 Jon Skeet 所指出的,我会尝试添加它,引用类型变量的值要么是 null 要么是内存地址。
因此,当您声明:
当时
messagingRoomJID
是只读的,但该内存位置几乎没有属性,在这种情况下Resource
可以更改为另一个值。As per MSDN , readonly fields can be initialized during declaration or by using a constructor.
However, as in your code you are not changing either during the initialization or using a constructor but still the property
Resource
contains the value.As pointed out by Jon Skeet, I would attempt to add to it, the value of a reference type variable is either
null
or amemorry address
.So ,when you declare :
At that time
messagingRoomJID
is readonly but there are few property present at that memory location , in this caseResource
can be changed to another value.