-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: Expression bodied get and set methods #7881
Comments
In #8364 I suggested to use another approach for this, because the |
public string AutoProp { // "standard, old fashioned" auto-property
get;
set => "Hello " + value;
} Together with #8364 (further down) it can be used to populate the backing field: public string UnfancyProp { get; set; }
public string FancyProp {
value UnfancyProp;
get;
set => "Fance me up " + value;
} If UPDATE: #8364 has already evolved and futher defines the wanted behaviour of |
I don't think changing the meaning of |
@MgSam public string Foo {
get => "Bar"; // using expression body here
set { xyz = value; }
} is the same as the already available public string Foo => "Bar"; // using also expression body here ..., but with an available setter also. And as public int Foo { get; set => (value<0) ? 0 : value; } or private object CheckNullObject(object arg) {
if (arg ! =null) return arg;
else throw new ArgumentNullException();
}
public string Foo { get; set => (string)CheckNullObject(value); }
public Stream BarStream { get; set => (Stream)CheckNullObject(value); } thus cleaning the code and still being intuitive (in my eyes). PS: would be even nicer when allowing public string Foo { get; set => value ?? throw new ArgumentNullException(); }
public Stream BarStream { get; set => value ?? throw new ArgumentNullException(); } |
He means that in your special syntax public void SayHello(string name) => Console.WriteLine($"Hello {name}"); |
*@HaloFour * public string Foo {
get => _bar;
set => _bar = value ?? throw new ArgumentNullException(); // throw: see pattern.md for C# 7
} In #8364 I'm now suggesting the use of a new |
I had a look into the roslyn source code and it seems to be very easy to make this work. |
I have implemented the expression bodies for getter and setter (for regular properties by now). They are working fine in the test environment. But I don't know how to contribute it to the git archive. Can anybody help? |
Interesting. |
We will discuss if we want to do this in the next language version based on the work of @lachbaer. |
Interesting. |
@DiryBoy Don't. 😄 |
@alrz Well, I wonder why this is not in the current version. |
@lachbaer We discussed this at the LDM today, and we are supportive of this, but we'd prefer to support all relevant contexts when we do this. In particular, we'd like to add constructors and destructors to the list of things that can have => bodies. Are you motivated to do that? |
Should both forms of read-only properties be supported? public string FirstName => _firstName;
public string LastName { get => _lastName; } |
@HaloFour Yes. |
@gafter For ctor and dtor I don't see that many use cases, but to be consistent over the language it would make sense.
|
I added <Field Name="ExpressionBody" Type="ArrowExpressionClauseSyntax" Optional="true" /> to |
What about support expression bodies for events: add/remove methods like get/set methods for properties? |
Sorry, have found #11198 by tag New Language Feature - Expression-Bodied Ctor/Dtor/Accessor |
@alrz, Thank you for the clarification. Now I see the difference. |
This was completed in #13543. |
This is a really nice feature. I would like to propose an extension to this one. I find now and then a common scenario, where I have a class Foo that exposes a property Bar, and then a second class Blah that is composed with an instance of Foo, and has to expose the Bar property in Foo unchanged. eg
With this proposal, Blah could be written like:
but that doesn't look to much of an improvement.
What I would like to be able to do, is say the property Bar in Blah is binded bidirectionally to the property Bar in the instance f of Foo. Maybe something like:
Hope I'm not too late for this one. |
I'm not sure if this is the right place to bug this, but the following should be legal in C# and it's currently not: [MethodImpl( MethodImplOptions.AggressiveInlining )] // <-- Error
public float Length => Math.Sqrt( X * X + Y * Y + Z * Z ); This results in:
Method attributes should be allowed on getter-only expression-bodied properties as the following is allowed: public float Length
{
[MethodImpl( MethodImplOptions.AggressiveInlining )]
get => Math.Sqrt( X * X + Y * Y + Z * Z );
} |
@Ziflin That is not a bug. If you need to place an attribute on the getter (but not on the property itself), you can't use the shortest form. |
@fedeazzato class Foo {
private OtherClass oc = new OtherClass(); // has int BarMethod();
public int BarMethod() => oc.BarMethod();
} For fields it would be something like: class Foo {
private OtherClass oc = new OtherClass(); // has int BarProperty { get; set; }
public int FoosProperty <=> oc.BarProperty;
// I'd rather use a syntax like:
public int FoosProperty { get; private set; } => oc.BarProperty
// allows arrow operator after get; set;
} But, I am not a friend of this! Properties are somehow like fields, just with get/set/access field control. In terms of clean programming it wouldn't be a great idea if you could delegate a field: /* discouraged example */
class Foo {
private OtherClass oc = new OtherClass(); // has int BarField = 0;
public int FoosField <=> oc.BarField;
/* <=> operator delegates to oc.BarField; */
} Well, it looks okay, but is really, really messy! @Ziflin (also) My conclusion: there sould be no PS: #15708 is okay, but that is Visual Basic ;-) |
C# 6.0 allowed get-only properties and all methods to have an expression body. This is a great feature that can help keep the focus on business logic rather than code. However, when you need to fall back to writing a
get
andset
method manually, it can be annoying that you're no longer allowed to use expression bodies.I propose supporting expression bodies for
get
andset
methods. It makes code less verbose and also would provide more symmetry in the language.The text was updated successfully, but these errors were encountered: