将结构值分配给此关键字 [英] Assignment of a struct value to this keyword

查看:20
本文介绍了将结构值分配给此关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在研究 CancellationToken 结构的内部结构,发现了一些奇怪的结构(更准确地说,是为 this 关键字赋值).

I was recently looking into internals of CancellationToken structure and discovered a bit of weird construct (to be more precise, assignment of value to this keyword).

其中一个构造函数的代码如下:

Code of one of its constructors is as following:

public CancellationToken( bool canceled )
{
    this = new CancellationToken();
    if ( canceled )
    {
        this.m_source = CancellationTokenSource.InternalGetStaticSource( canceled );
    }
}

this关键字赋值的那一行是什么意思?

What is the meaning of line on which the assignment to this keyword occurs?

请注意,类不能分配给 this 关键字 - 错误 Cannotassign to '<this>'因为它是只读的发生.

Please note that assignment to this keyword is not possible for classes - error Cannot assign to '<this>' because it is read-only occurs.

推荐答案

这是 C# 的一个鲜为人知的特性 - 它允许结构覆盖它自己的数据.

This is a very little known feature of C# - this allows a struct to overwrite its own data.

就实际应用而言,您不会发现它有很多用途..

As far as practical application goes, you're not going to find many uses for this..

struct MyStruct
{
    int a = 1;
    int b = 2;
    int c = 3;

    public void Mutate()
    {
        a = 10;
        b = 20;
        c = 30;
    }

    public void Reset()
    {
        a = 1;
        b = 2;
        c = 3;
    }

    public void Reset2()
    {
        this = new MyStruct();
    }

    // The two Reset methods are equivilent...
}

仔细想想,当您处理值类型与引用类型时,this"的含义存在根本区别.

Thinking about it more, there's a fundamental difference in what "this" means when you're dealing with value types vs reference types.

当你在引用类型上调用this"时——你得到的是一个存在于堆栈中的指针,你实际上并没有得到对象本身.指针隐式地取消引用回堆上的对象,这抽象了间接性.现在,如果在类中分配给 this 是可能的,并且您已经说过类似 this = new MyReferenceType() 的内容,您就会将指针更改为指向一个当前范围中的不同堆对象 - 您不会改变堆中的原始对象本身,也不会导致任何其他引用/指针引用新对象堆对象.很可能一旦您的变异指针超出范围 - 您创建的新堆对象就会受到垃圾收集.

When you call "this" on a reference type - what you get is a pointer that lives on the stack, you don't actually get the object itself. The pointer implicitly dereferences back to the object on the heap, which abstracts the indirection. Now if assigning to this in classes were possible, and you'd have said something like this = new MyReferenceType(), you'd have changed the pointer to point to a different heap object in the current scope - you wouldn't have changed the original object itself in the heap, nor would it have caused any other references/pointers to refer the new heap object. Its very likely that as soon as your mutated pointer would have gone out of scope - the new heap object you'd have created would have been subject to garbage collection.

当您在值类型上调用this"时 - 您获得的是实际对象,而不是引用或指针.没有间接性,因此您可以自由地覆盖此内存位置的原始位(这正是默认构造函数所做的).

When you call "this" on a value type - you are getting the actual object, not a reference or pointer. There is no indirection so you are free to overwrite the raw bits at this memory location (which is exactly what the default constructor does).

这篇关于将结构值分配给此关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆