What happens when you return "this" from a struct in C#?

.net, c#, struct

Solution

It returns an independent copy of the value. In other words:

MyStruct x = new MyStruct();
y = x;

is like

MyStruct x = new MyStruct();
y = x.This();

It's pretty pointless really.

Bear in mind that "the value" is basically "the bits making up everything to do with the fields in the struct". If those fields are references, the reference values will be copied (not the objects they refer to). If they're other value types, those values will just be copied.

Curious factoid: in structs, you can reassign `this`:

this = new MyStruct();

Please don't do that though :)

Problem

Curious, what happens when you return keyword this from a struct in C#? For example: ``` public struct MyStruct { // ... some constructors and properties 1-3 public MyStruct Copy() { return MyStruct(Property1, Property2, Property3); } // vs public MyStruct This() { return this; } } ```

Original source