When can you write ref this?

Usually, you cannot pass ref this as a parameter, since this is not a writable field.  However, that’s not true for value types.  The this field of a value type is a writable value.

To quote the spec (§5.1.5)

Within an instance method or instance accessor of a struct type, the this keyword behaves exactly as a reference parameter of the struct type (§7.6.7).

Therefore, the following code prints 1:

static void Main() {
    Mutable m = new Mutable();
    m.Mutate();
    Console.WriteLine(m.Value);
}

struct Mutable {
    public int Value;
    
    public void Mutate() {
        this = new Mutable(); 
        MutateStruct(ref this); 
    }
}
static void MutateStruct(ref Mutable m) { 
    m.Value++; 
}

In practice, this should never come up, since mutable structs are evil and should be avoided at all costs.

Next time: This doesn’t always work.

12 comments:

This publication is excellent. Rose

I am sure that advices from https://resumecvwriter.com/blog/how-to-write-resume-job-description will help you to write your resume job description. I had such experience so I know what I am talking about

Thanks for your article! I have been looking for quite a long time and fortunately I read this article! I wish you would continue to have valuable articles like this or more to share with everyone
Joaca acum gratis jocuri friv!

Our myassignmenthelp administration goes about as a simple and advantageous technique to choose for specialists' help for finishing any scholastic undertaking inside the characterized time. Thus, in the event that you have pressure for your assignment, enlist our expert journalists

Hello friends, my name is Rahul, who has brought something special for you. I am a resident of Jalandhar city. I have brought something for you, you will find it on my website. You can visit my website.
Jalandhar Escorts
Jalandhar Escorts

Ethiopia, a country in East Africa, We will continue to track the finest Tender for your firm, and you can sign up to receive notifications about Ethiopia Tenders from multi-funding agencies and government departments. All information about Ethiopia Tenders We are becoming one of the largest bidding contractors and Tenders information service providers. To learn more about the most recent Ethiopia Tenders information online from Ethiopia, all Ethiopia Tender come from government departments, international finance organisations, and private firms in Ethiopia. Bid Detail is a website that Provides all of Ethiopia Tenders info online

shopify app development company We do the whole shopify site maintenance based on your business’s health check.

Very good blog post, it is very well written and your points are well-expressed. Thanks for sharing cross rivers-state-college-of-health-tech.edu.ng_admission-list, visit Best Rated Educational Update Portal in the World; Examination and Academic Guide, High Paying Jobs & Scholarship  Website.

"Ref This" is a comprehensive guide for mastering reference management, providing clear guidance on creating, organizing, and citing references. It is accessible to both beginners and seasoned researchers, offering practical tips and examples to enhance academic or professional writing quality. This indispensable tool is suitable for students, researchers, and writers. ¿Quién Recibe el Anillo de Compromiso en un Divorcio Nueva York?

สล็อต ทดลอง pg ทั้งหมด เว็บไซต์ของเราเป็นประสบการณ์ที่น่าตื่นเต้น pg และสนุกสนานที่จะพาคุณสำรวจโลกของเกมสล็อตที่ไม่ธรรมดา ด้วยความหลากหลายที่เต็มไปด้วยความสนุก

Post a Comment