none
Typescript style property initialization in C# RRS feed

  • General discussion

  • Are there any plans to add to Typescript style property declaration in the constructor to C#?  

    Eg, instead of:

    private Dependency1 dep1;
    private Dependency2 dep2;
    
    public MyClass(Dependency1 dep1, Dependency2 dep2) 
    {
        this.dep1 = dep1;
        this.dep2 = dep2;
    }

    we'd be able to do:

    public MyClass(private Dependency1 dep1, 
                   private Dependency2 dep2) 
    {}

    This is so much cleaner, espcially when the dependency list gets a little larger.

    Thursday, May 16, 2019 8:35 AM

All replies

  • Are there any plans to add to Typescript style property declaration in the constructor to C#?  

    Eg, instead of:

    private Dependency1 dep1;
    private Dependency2 dep2;
    
    public MyClass(Dependency1 dep1, Dependency2 dep2) 
    {
        this.dep1 = dep1;
        this.dep2 = dep2;
    }

    we'd be able to do:

    public MyClass(private Dependency1 dep1, 
                   private Dependency2 dep2) 
    {}

    This is so much cleaner, espcially when the dependency list gets a little larger.


    No, let's not start swinging from the ceiling. 
    Thursday, May 16, 2019 8:41 AM
  • I hope not. I find this syntax in TS to be non-intuitive and I don't use it myself. It saves you very little in typing in my experience.

    Your argument about it being cleaner for larger dependency lists tends to point me to the argument that larger dependency lists should be harder to write. This is indicative, to me, of a design problem. Too many dependencies is an indication of over-architecting or too finely created components which is just as bad as too few. But again this is my opinion.

    While you started with a question I think this is more a discussion because there won't be any "right" answer. I'm going to change this to a discussion instead where others can voice their opinions for and against. Of course if there is enough for then the discussion would need to move to the C# language repo on GitHub where it can be weighed against other features. You might find someone has already asked this question but I'm not going through the 1400 open issues to find out myself :}


    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, May 16, 2019 2:09 PM
    Moderator