0

Example code:

public MyStruct Struct => new MyStruct(this);

Does this create a new MyStruct (or class) every time? Basically, what does this code compile to? Also how is this property shortening officially called? Was a bit tough trying to google this one.

Paprik
  • 159
  • 8
  • 2
    There's no need for you to ask us what you can test for yourself. – jmcilhinney May 16 '23 at 14:48
  • You can check everything pretty simple using [sharplab.io](https://sharplab.io/#v2:C4LglgNgNAJiDUAfAAgJgIwFgBQPkGYACAZ2ACcBXAY2EIFkBPAZXOuAG8dDvCueD6zVjQAUAewBGAKwCmNQmGAyAtgEpO2Hlt6aeAXxwHcxgWkIBhQhq0DGLSvPttCAXgB8hAHYyA7oKeiwAAWYMSqANx83KYALPQiqlZRhEZ6QA===). You can see the IL (or jit asm) code on the right and you can hover on some parts of this code to see the description. – Sergey Sosunov May 16 '23 at 14:53
  • 1
    See [what-does-the-operator-mean-in-a-property-or-method](https://stackoverflow.com/questions/31764532/what-does-the-operator-mean-in-a-property-or-method) on the `=>` operator ( you can probably use "C# fat arrow" for google searches). – topsail May 16 '23 at 14:53
  • 1
    Spoiler alert: This is _not_ an Auto-Property. – Fildor May 16 '23 at 14:57
  • @jmcilhinney Then what even is the point of this website if not to act as an archive of questions-answers. We could all test everything ourselves, find out what code decompiles to, etc. and maybe some people here can't or don't know how to – Paprik May 16 '23 at 14:59
  • Let's not pretend that this would be difficult to test or that you would even have to do any research to work out how to do it. – jmcilhinney May 16 '23 at 15:36
  • @Paprik SO is a place for question-answer threads of *novel* problems and solutions. Otherwise the entire website would be flooded by people that want to be spoonfed the things their textbooks or the languages tools can tell them in two seconds. Please follow the debugger tutorials from the MS docs for [visual studio code](https://learn.microsoft.com/en-us/dotnet/core/tutorials/debugging-with-visual-studio-code?pivots=dotnet-7-0) or [visual studio](https://learn.microsoft.com/en-us/dotnet/core/tutorials/debugging-with-visual-studio?pivots=dotnet-7-0) – Narish May 16 '23 at 15:37
  • @Narish This site is already full of such questions though. I search for a 2 second answer, usually there's a SO link. This time there wasn't, so I wanted to ask so someone can get their 2 second answer in the future. I'm sorry if you guys disagree, feel free to delete this question, I don't even know how to. – Paprik May 16 '23 at 16:02
  • `This time there wasn't` because people generally don't use a readonly property as a simplified factory method. They create a factory method, and ask questions about that – Narish May 16 '23 at 17:32

1 Answers1

3

The answer is two-part. First, you're returning a new instance on every call (=>), rather than returning a stored instance (=). In other words, what you seem to want is this:

// caveat: you can't use "this" before the constructor is called
// you'll have to initialize this property inside the constructor instead
public MyStruct Struct { get; } = new MyStruct(this);

However, structs are copied by value, so even if you write it correctly, you'll still get a new instance every time. Either use a class, or use ref struct instead, like this:

MyStruct myStruct;
public ref MyStruct Struct => ref myStruct;

The above will let you modify the internal structure as you would expect.

Blindy
  • 65,249
  • 10
  • 91
  • 131