out var Property #748
Replies: 18 comments
-
You can already do that with any ref-returning method or property, class C {
ref int M(out int i)
=> ref M(out M(out i));
} I'm not sure if it makes sense for it to work with non-refs, though. |
Beta Was this translation helpful? Give feedback.
-
You can't use |
Beta Was this translation helpful? Give feedback.
-
I've wanted this on a fair number of occasions. @Joe4evr He didn't ask to pass the property as the |
Beta Was this translation helpful? Give feedback.
-
The compiler could certainly handle the syntax and behave as described using a temporary local, for both There is one place where the behavior wouldn't be consistent, though. If the method called modifies the reference the location is changed immediately and would be reflected on further inspection of that local/field. But that couldn't be the case with properties since you're relying on assignment after the method returns. At best the compiler could wrap the method call in a |
Beta Was this translation helpful? Give feedback.
-
If I can throw a wrench into the mix here: Concurrency. Consider e.g. Anything using special visibility/ordering guarantees, like |
Beta Was this translation helpful? Give feedback.
-
I think the one benefit here would be with being able to pass the backing field of an auto-prop for a read-write property (this is the primary blocker of auto-props in a lot of my personal projects, that and wanting to ensure field layout is exactly how I want it for interop). The only other benefit is saving 1 line of code (the actual assignment from the temporary to the property) and I'm not sure that is enough of a reason to add this as a feature. |
Beta Was this translation helpful? Give feedback.
-
VB already supports this. C# definitely coudl as well. There's no good reason, afaict, for C# to not support this. |
Beta Was this translation helpful? Give feedback.
-
Wait for it... ... ...analyzers. 😁 |
Beta Was this translation helpful? Give feedback.
-
How would analyzers help here? |
Beta Was this translation helpful? Give feedback.
-
@DavidArno I meant it as a joke, but if you're serious, the analyzer would create an error if you ref something for |
Beta Was this translation helpful? Give feedback.
-
I think there is difference between With On the other hand, with Or is there some real-world code where it would make a difference that the value is written only once after the call for |
Beta Was this translation helpful? Give feedback.
-
@svick Even if we ignore
In this case,
If we go even further, the |
Beta Was this translation helpful? Give feedback.
-
I would really like to |
Beta Was this translation helpful? Give feedback.
-
Inability to pass properties as a ref or an out variable greatly complicates code, and often makes one regret auto-properties. But auto-properties are greatly needed for sanity and cleanness of code's sake. I wish this had been added at the same time as auto-properties. This goes beyond just a little thing of inconvenience.
YES! I can't think of a bigger way of saying: we desperately need this.
But ah-hem, auto-properties are what, 90% of our properties?! Yes, without auto-properties, it wouldn't be a big deal, just use the backing variable, but auto-properties are the standard we always want to use when there is no reason not to otherwise (except for this very limitation!). For those mentioning technical limitations to doing this... could limiting this to auto-properties solve those problems? Please let's get this done. |
Beta Was this translation helpful? Give feedback.
-
@copernicus365 limiting it to auto-properties could allow the compiler to use the backing field directly, and solve those problems. but, it creates another problem in that auto-properties are no longer just a syntax sugar but a distinct member type -- I don't think that inconsistency is worth it. |
Beta Was this translation helpful? Give feedback.
-
Auto-implemented properties are an internal implementation detail. The consumer of such a type has no idea as to whether or not the property is an auto-implemented property or not, and certainly doesn't have direct access to any underlying backing field. There really isn't any serious hurdles to supporting |
Beta Was this translation helpful? Give feedback.
-
Bravo!
This doesn't seem like much of a problem to me, in fact, the case could be made that this would be performing more consistently, in a way that is consistent with the nature of properties (a good thing). In the case of a field, it is true that the field could change at the top of the method that sets it as an out or ref parameter, and then that method might take some time before returning. It might even throw an exception, in which case the state of the |
Beta Was this translation helpful? Give feedback.
-
Allow use property as described below:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi All,
It would be really nice if I could pass a property to an outvar call. For example, currently I have to do this:
int.TryParse(txtAge.Text, out var temp);
Person.Age = temp;
It would be great if I could do this:
int.TryParse(txtAge.Tex, out Person.Age);
and when doing that, the compiler was smart enough to create a temporary variable and then assign it to the property.
Beta Was this translation helpful? Give feedback.
All reactions