Enhanced var
pattern
#7181
-
Today C# allows us using if (expr is { Property: var p, _field: var f })
// ... Sometimes, a property or field may be a ref return rather than a value return. Here may exist a need for defining inlining variables with ref using // properties
if (expr is
{
RefProperty: ref var r,
RefProperty2: ref readonly var s,
RefReadOnlyProperty: ref readonly var t
})
// ...
// indexers
if (span is [ref var a, ref readonly var b, ..])
// ...
// methods
if (Method() is ref var r)
// ... Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Does C# support ref properties now? Think I missed that one 😊 |
Beta Was this translation helpful? Give feedback.
-
Since every line in your example should unconditionally succeed, I'd turn this around and say that having nominal deconstruction with var {
RefProperty: ref r,
RefProperty2: ref readonly s,
RefReadOnlyProperty: ref readonly t,
} = expr; The next would be list destructuring which hasn't been discussed yet that I'm aware of, but which keeps coming to my mind as we work on collection literals (@CyrusNajmabadi): var [ref a, ref readonly b, ..] = span; The last is already possible in the ideal (unconditional) form today: ref var r = ref Method(); |
Beta Was this translation helpful? Give feedback.
Since every line in your example should unconditionally succeed, I'd turn this around and say that having nominal deconstruction with
var
would be the ideal way to approach the first:The next would be list destructuring which hasn't been discussed yet that I'm aware of, but which keeps coming to my mind as we work on collection literals (@CyrusNajmabadi):
The last is already possible in the ideal (unconditional) form today: