Why can't we use expression-bodied constructors?

Gigi

Using the new Expression-Bodied Members feature in C# 6.0, we can take a method like this:

public void Open()
{
    Console.WriteLine("Opened");
}

...and change it to a simple expression with equivalent functionality:

public void Open() => Console.WriteLine("Opened");

This is not true for constructors, however. Code such as this doesn't compile:

private DbManager() => Console.WriteLine("ctor");

Nor does this:

private DbManager() => {}

Is there any reason why constructors cannot benefit from the expression-bodied members feature, and must be declared the traditional way?

i3arnon

It would be more confusing than useful. Especially when you add a call to another constructor.

Here's the direct quote from the design notes:

Constructors have syntactic elements in the header in the form of this(…) or base(…) initializers which would look strange just before a fat arrow. More importantly, constructors are almost always side-effecting statements, and don’t return a value.

From C# Design Notes for Nov 4, 2013

In a more general way:

To summarize, expression bodies are allowed on methods and user defined operators (including conversions), where they express the value returned from the function, and on properties and indexers where they express the value returned from the getter, and imply the absence of a setter.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why can't we just use arrays instead of varargs?

Why can't I use protected constructors outside the package?

Why can't I use a starred expression?

Why can't we use 'this' keyword in a static method

Why can't we use assertion for public methods?

Why can't we use @Cacheable with static method in spring with ehcache?

Why can't we use nested type through class-member-access expression?

Why can't we always use the register storage class in C?

Why can't we use . as a parameter in an anonymous function with %>%

Can't write an expression-bodied function member

Why can’t we use GraphQL just with Redux

Why can't we mutate prototype of Built-in Function Constructors?

Why can't we use this instance in the static method of extended class?

Why can't we use only comparable in every situation?

Why can't we use DTrace in iOS

Why can't we use/set function keys as password keys?

Why can't we use interpolation expression with "ng-model" but can use with "ng-src"?

Java, why can't we use null as package name?

Why can't we use 192.168.1 as the subnet mask instead

Why can't we use websocket in Tomcat?

Why can't we use setAtrribute function in jquery?

Why Can't We Use Foreach Loop On The String Like For Loop

Why can't we use a magnitude comparator through programming languages?

Why use expression-bodied properties for primitive values?

Why can't we use "pass" in a ternary statement?

Why exactly can't we use relational expressions in a switch statement?

Why can't we use square brackets in case of dynamic vectors?

Why can't we use square brackets in the code below?

Can i traverse a expression bodied member with an ExpressionVisitor?