1

If I have a class with a lot of properties and I would like to create a constructor with all these properties I have to type a lot. Is there a smarter way in C# 4.0 to do this?

class MyClass {
  public string A{get;set;}
  public string B{get;set;}
  public string C{get;set;}
  public string D{get;set;}
  public string E{get;set;}
  public string F{get;set;}
  public string G{get;set;}
  //and so on...

  public MyClass(string a, string b, string c /*and so on...*/)
  {
    this.A = a;
    this.B = b;
    this.C = c;
    //...
  }
}

I would like to force the user to create an object with all of the properties.

miri
  • 1,611
  • 5
  • 20
  • 24
  • Duplicate of http://stackoverflow.com/questions/7809677/snippet-code-to-create-constructor-in-vs2010-express – Amiram Korach Jul 25 '12 at 08:55
  • That doesn't sound very caller friendly. Since they are read-write, why not just let the caller use object-initializer syntax? i.e. just `new MyClass { A = "abc", F = "def" }` ? IMO the "smarter" way to do that is: don't do that. Just have a parameterless constructor and let the caller do as they will. – Marc Gravell Jul 25 '12 at 08:55
  • 1
    Here is an idea: make this class partial and add an attribute like [FullConstructor]. Add a T4 template that will scan the project for classes like this and generate constructors for them in a separate file. – Amiram Korach Jul 25 '12 at 08:57
  • @MarcGravell do you mean I should not make any constructor in my class? Just use new MyClass{ A...} if I want to create an object? – miri Jul 25 '12 at 08:58
  • @Miri The only downside to Marc's suggestion is it doesn't "force" all properties to be set. – Adam Houldsworth Jul 25 '12 at 08:59
  • @miri I'm saying that you should consider whether you *need* that constructor. In many cases, you don't. In ***some*** cases (especially with immutable types) you might. – Marc Gravell Jul 25 '12 at 09:07

3 Answers3

2

If you install Resharper, one of the refactoring tools allows you to "Generate Constructor" the wizard then provides you a list of properties and you choose which ones you wish to include.

And no, I am in no way affiliated with Jetbrains, I just use R# and find it an invaluable tool.

Bob Vale
  • 18,094
  • 1
  • 42
  • 49
1

I asked a similar question a while ago:

Refactoring large constructors

The short of it is, there isn't much support for making it look nicer or easier to create. The solutions never "solve" the problem, they only move it to another place or side-step it.

The first thing to look at is if the class actually needs this much state. Is the class doing too much? Can it be broken down into more, simpler classes? This isn't always possible, and sometimes you simply have classes with lots of constructor arguments.

Community
  • 1
  • 1
Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
0

If you are using .Net4.0 you may use Named parameters.

Something like this:

class Person
    {
        public long Age { get; set; }
        public string Name { get; set; }
        public double Salary { get; set; }

        public Person(int age=22,string name="Ram",int salary=9000)
        {
            Age = age;
            Name = name;
            Salary = salary;
        }
    }
Bob Vale
  • 18,094
  • 1
  • 42
  • 49
Embedd_0913
  • 16,125
  • 37
  • 97
  • 135