这个函数如何改变我的只读变量?

发布于 2024-12-10 17:17:20 字数 816 浏览 0 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(5

雪化雨蝶 2024-12-17 17:17:20

它不会改变变量的值。变量的值只是一个参考。该参考与以前保持不变。

发生变化的是引用所引用的对象内的数据。 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.

南城旧梦 2024-12-17 17:17:20

因为它正在更改只读变量的属性,而只有根元素本身是只读的。

例如,

private readonly int MyReadOnlyVariable;
private readonly ComplexType MyReadOnlyComplexType;

public MyClass()
{
    MyReadOnlyVariable = 2;            //valid
    MyReadOnlyComplexType = something; //valid
}

public void NotAConstructor()
{
    MyReadOnlyVariable = 2;                          //invalid
    MyReadOnlyComplexType = somethingElse;           //invalid
    MyReadOnlyComplexType.Something = somethingElse; //valid
}

我们可以在构造函数中更改一次;但对于包含属性、公开字段等的复杂类型,我们可以在以后更改它们。这就是为什么(对于一个常见的例子)拥有一个只读数组通常是一个坏主意,或者至少可以想象它通常不是预期的 - 因为数组的元素可以更改。

Because it is changing a property of your readonly variable, whereas only the root element is readonly per se.

For instance,

private readonly int MyReadOnlyVariable;
private readonly ComplexType MyReadOnlyComplexType;

public MyClass()
{
    MyReadOnlyVariable = 2;            //valid
    MyReadOnlyComplexType = something; //valid
}

public void NotAConstructor()
{
    MyReadOnlyVariable = 2;                          //invalid
    MyReadOnlyComplexType = somethingElse;           //invalid
    MyReadOnlyComplexType.Something = somethingElse; //valid
}

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.

寻梦旅人 2024-12-17 17:17:20

所以,我认为乔恩·斯基特的回答是正确的。
例如,您的类变量 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.

梦言归人 2024-12-17 17:17:20

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 method GetRoom, its passed by reference. So GetRoom can change any object's field.

萌梦深 2024-12-17 17:17:20

根据 MSDN ,可以在声明期间或使用构造函数。

只读字段可以在声明处或构造函数中初始化。因此,只读字段可以具有不同的值,具体取决于所使用的构造函数。

但是,正如在您的代码中一样,您在初始化期间或使用构造函数时都不会更改,但属性 Resource 仍然包含该值。

正如 Jon Skeet 所指出的,我会尝试添加它,引用类型变量的值要么是 null 要么是内存地址。

因此,当您声明:

private readonly JID messagingRoomJID;

当时 messagingRoomJID 是只读的,但该内存位置几乎没有属性,在这种情况下 Resource 可以更改为另一个值。

As per MSDN , readonly fields can be initialized during declaration or by using a constructor.

A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.

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 a memorry address.

So ,when you declare :

private readonly JID messagingRoomJID;

At that time messagingRoomJID is readonly but there are few property present at that memory location , in this case Resource can be changed to another value.

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