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;
}
}
It returns an independent copy of the value. In other words:
is like
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:Please don’t do that though 🙂