Hassan Hashemi`s blog

Afghan software developers

A closer look at C# 6 Primary Constructor

In my previous post which can be found at my blog and here i`v briefly explained new features of C# 6, most of them are extremely simple as they do not really need explanation however i believe that there are some items to note about Primary constructors and i would like to share them with you.

Introduction:

In my opinion the newly added thing which is called "Primary Constructor" is not really a constructor since it dose not have any implementation plus a constructor is supposed to be some kind of a method, please correct if i am wrong dudes.)))
any way lets call it Primary Constructor because it is what creators call it.

The aim to design primary constructor for C# as Microsoft announced was to destroy constructors that were written only to capture parameters of a constructor and store them in a field for further operations.

a simple example of such a task can be seen here:

public class Person
{
    private string _firstName;
    private string _lastName;

    public Person(string firstName, string lastName)
    {
        // Store parameter values into a field
        this._firstName = firstName;
        this._lastName = lastName;
    }

    public string FullName
    {
        get
        {
            return String.Concat(this._firstName, " ", this._lastName);
        }
    }
}

Primary constructor Declaration:

Above code can be simplified by using a primary Constructor that actually has not a body.

Declaration of a primary constructor is some thing like this:

// There we go, this is the primary constructor
// define parameters and just use them in the class scope
public class Person(string firstName, string lastName)
{
    //using parameter vaules here
    private string _firstName = firstName;
    private string _lastName = lastName;

    public string FullName
    {
        get
        {
            return String.Concat(this._firstName, " ", this._lastName);
        }
    }
}
behind the scene the compiler generates just a regular constructor though

constraints:

- custom access modifier:

The first problem i`v faced when using a primary constructor was that i needed them to be internal and that was not possible so i totally forgot about using it in that particular class. this is a limit for primary constructors for now, Maybe the easiest way to add this feature (access modifier) for Microsoft is to provide some sort of attribute.

- in addition to custom access modifier limit you also can use parameters of a primary constructor only on right side of an equals sign when initializing a field or an auto property initializer if you do not you are gonna face a compiler error though.

Last words:
Frankly C# 6 was not amazing as expected but the really nice and amazing thing was the Roslyn its self, by the way you can now add any thing you want to c# compiler, maybe remove some syntactical features of C# that you or your team dont want to use them or add some thing.
another really really fascinating thing that came with Roslyn was diagnostics framework. With these api`s you can now hook into compiler events maybe into its lexical analysis part and interact with visual studio.
If you and your team have`nt used team or read about them until now you probably missed a big Cake :D.

Comments (2) -

  • James

    9/16/2014 2:56:49 PM | Reply

    Primary Constructor is a constructor though it dose not have a body, it is Invoked like a function call in fact its like a layer before a regular constructor.

  • unknown

    6/2/2015 3:41:19 PM | Reply

    why don't you put any other post......................

Loading