The text of this article is not in this database — only its details are. Read it on the old site: Primary Constructor is Removed From C# 6.0
9 Comments
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
The text of this article is not in this database — only its details are. Read it on the old site: Primary Constructor is Removed From C# 6.0
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment.
RichPosted Aug 26, 2015, 12:58 PM
But... but... is there actually a syntax for using variables with property initializers as you suggest? There doesn't seem to be. For example we were promised we'd be able to do: public class MyClass(string myConstructionString) { public string MyValue { get; } = myConstructionString; } and then do var myClass = new MyClass("Hello"); I don't see there's any way of doing that now apart from the old-fashioned way of writing a (normal) constructor and setting the value from there. The only change seems to be that I no longer need a private setter on my property if I do this (below). I can't work out how to set MyValue using a variable. Or am I missing something? public class MyClass { public string MyValue { get; } public MyClass(string myConstructionString) { MyValue = myConstructionString; } }
Vipul MalhotraPosted Jul 6, 2015, 4:42 AM
nice
Praveen KumarPosted Mar 30, 2015, 6:14 AM
Thanks Valentijn Vanwynsberghe For example I put the constant values for initialization we may put a variable but here I had put the constant values because I wanted to initialize these properties with some default values.
Valentijn VanwynsberghePosted Mar 30, 2015, 5:53 AM
hmm, you are not really exactly proving that auto implemented property initialisers can do the same thing as primary constructors here. For example, how would you inject values in your class and want to enforce these should be injected then? Here you are not supplying any values to your class from outside that class, the values are just preset and hardcoded.
Abhishek TripathiPosted Dec 30, 2014, 1:43 AM
In actual work scenario i have never come across to Primary Constructor :)
Praveen KumarPosted Dec 6, 2014, 7:56 AM
it came with CTP 3 for testing and now it has been removed
VulpesPosted Dec 5, 2014, 7:03 PM
They were a proposed feature of C# 6.0 but they've been removed at least for the time being. They might make a comeback in a later version as part of a package which includes 'record' classes and pattern matching.
Sam HobbsPosted Dec 5, 2014, 5:11 PM
Are primary constructors a feature of Visual studio or are they a feature of the language? Also, have they been removed from C#? According to "Changes to the language feature set" they have been removed, but I probably misunderstand. See: https://roslyn.codeplex.com/discussions/568820