3 ms·
C# doesn't allow you to destructure a single property and as far as I can tell based on a quick test it doesn't allow you to combine a using declaration with de
by Merad 3y ago
C# doesn't allow you to destructure a single property and as far as I can tell based on a quick test it doesn't allow you to combine a using declaration with destructuring. I agree that this is surprising syntax... as a C# dev my first interpretation of that code would be that connection is the thing being disposed since it's the variable being declared. I wonder if js/ts destructuring has always silently held a reference to the containing object or if they added that behavior to make this use case work?
- metaltyphoon 3y ago> C# doesn't allow you to destructure a single property and as far as I can tell https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRj2NwDcBDMAAgAoB9GK8gcwFMBKKgXiqlYHcqABVZgAzgHsoNAEQBbVjJgFM7ANwlUBAJw0WHDbjxhWAYwlg4w0ZOlbMVAHLkFjAJZRgVAIJt1QA= https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRj2Nw...
- Merad 3y agoYour code is destructuring two properties and discarding one of them. It doesn't work with a single property: https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRj2NwDcBDMAAgAoBbAUwEoqBeKqBgdyoAUGwAZwD2UGgCJG4pgG4SqAgE56zObjxgGAY2Fg4fASLELMVAHLlGsoA= https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRj2Nw... I think that records don't generate a deconstruct method when they only have one property, but even if you manually define one you'll get an error on `var (varName) = ...`