一个结构值赋值给这个关键字 [英] Assignment of a struct value to this keyword

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

问题描述

我最近正在研究的CancellationToken 结构的内部,发现有点不可思议结构的(更精确的值赋值为此< 。它的构造之一/ code>关键字)

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).

代码如下所示:

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

什么是行的意义上的分配这个关键字出现?

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

请注意,分配给这个关键词是不可能的班级 - 误差无法分配到'<这>',因为它是只读发生

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

推荐答案

这是C#的一个鲜为人知的功能 - 这允许一个结构覆盖自己的数据。我不知道这是否仅适用于blittable值类型与否(即时猜测没有)。

This is a very little known feature of C# - this allows a struct to overwrite its own data. I'm not sure if this is only available for blittable value types or not (im guessing not).

至于实际应用的推移,你不会发现许多用途的..

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...
}

有关它的更多思考,有什么本当你在处理值类型VS引用类型是指一个根本的区别。

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

当调用本上引用类型 - 你得到的是一种生活在栈上的指针,你实际上并没有得到对象本身。指针隐含提领回堆,它抽象了间接的对象。现在,如果你是这样说的此=新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 you said something like this = new MyReferenceType(), you've changed the pointer to point to a different heap object in the current scope - you have not changed the original object itself in the heap, nor do any other references/pointers now refer to the new heap object. Its very likely that as soon as your mutated pointer went out of scope - the new heap object you created would be subject to garbage collection.

当你调用本上。值类型 - 你得到实际的对象,而不是引用或指针。有没有间接所以你可以自由在这个存储位置覆盖原始比特(这是默认的构造函数不正是)。

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天全站免登陆