Allow C++ like syntax for variable declaration and initialisation via constructor #2466
Replies: 18 comments
-
What advantage does it provide over current (as of C# 8): Person person = new("Matt", "Smith"); ? |
Beta Was this translation helpful? Give feedback.
-
Less typing (= new) apart from that, not much! I think it looks a little cleaner, personally. |
Beta Was this translation helpful? Give feedback.
-
I think a better example of its usage would be something like this: var matt = Person("Matt", "Smith") or var persons = List
{
Person("Matt", "Smith"),
...
} |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I think that C# struct should use C++ style of constructor, while C# class constructor should keep unchanged. |
Beta Was this translation helpful? Give feedback.
-
Except that it doesn't. The struct is always initialized in place on the stack by loading the address of the local slot and invoking the struct constructor. Not only has this always been the way that C# has behaved but C# doesn't need a second redundant syntax just for the sake of comforting those few developers who would be worrying about how and when value types are copied but aren't willing to read the documentation about it. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
Foo foo = new Foo(1, 2, 3); turns into:
There is no copy. The struct is initialized in place onto the address of the local slot. |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
@HaloFour |
Beta Was this translation helpful? Give feedback.
-
@ygc369 |
Beta Was this translation helpful? Give feedback.
-
@YairHalberstadt |
Beta Was this translation helpful? Give feedback.
-
That still doesn't have full support for C# 7. The earliest this change would occur is C# 9. |
Beta Was this translation helpful? Give feedback.
-
The last change to MCS (the mono compiler) was last december. I really don't think it's going to keep up with new C# features. Besides it currently makes no optimizations at all - anyone who cares about performance will not be using it. Adding a language feature just so that MCS will have an absolutely tiny performance gain when you use it, is totally pointless. |
Beta Was this translation helpful? Give feedback.
-
This isn't an optimization. This is a requirement per the CLI spec. A struct constructor is required to accept a first But all of this is irrelevant. If you're so concerned that the C# spec leaves any wiggle room for the potential of unnecessary copies, then request that the C# spec be amended to make it abundantly clear. There's zero reason to add redundant syntax to enforce the idea of what the compiler is already going to do, either to the specification or to developers. |
Beta Was this translation helpful? Give feedback.
-
If you do introduce this new syntax, then inexperienced programmers will still be puzzled, because they will have two different ways to do the same thing and it will make them worry about which one they should use. I don't think that's a better situation to be in. |
Beta Was this translation helpful? Give feedback.
-
Dangit, I have to walk that back. The compiler can emit Either way, my second point stands. If you're concerned, raise the issue to have the spec amended. There's zero reason for redundant syntax here. |
Beta Was this translation helpful? Give feedback.
-
Doesn't Mono use Roslyn now anyway? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I think it would be cool to declare and initialise variables neatly like in C++
Like:
Person person("Matt", "Smith");
I've just started using C++ and I really like this syntax, would it work in the C# world though?
Beta Was this translation helpful? Give feedback.
All reactions