Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4265804
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:40:26+00:00 2026-05-21T06:40:26+00:00

I was recently looking into internals of CancellationToken structure and discovered a bit of

  • 0

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 );
    }
}

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.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:40:27+00:00Added an answer on May 21, 2026 at 6:40 am

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

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

    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.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been looking into SQL recently and exploring a bit. in regards to Temp
I am not an expert on VBA as I've recently started looking into this.
(Is this the right site for this question?) I've recently been looking into circuits,
I recently started looking into building web applications using .NET MVC and I stumbled
I have been looking into CruiseControl configuration recently (I'm a complete CC noob) and
recently, i am looking into assembly codes for #define, const and enum: C codes(#define):
I've been looking into setting up a Selenium test suite recently but all of
I've recently been looking through my warnings in Eclipse and come across this one:
Recently I have been looking into OpenGL, and I've got up to the stage
I've been looking into memory management a lot recently and have been looking at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.