Home > OS >  How to automatically create a default constructor which sets default values
How to automatically create a default constructor which sets default values

Time:05-16

Is there any way to auto generate a constructor which looks like this:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }

    public User(int id, string name)
    {
        Id = 0;
        Name = "";
    }
}

Currently I am creating a constructor like that with the refactoring tool (CTRL .):

    public User(int id, string name)
    {
        Id = id;
        Name = name;
    }

and editing each line afterwards which is pretty cumbersome when you have 20 properties per class. Is there a better way to that? (Maybe to define a code snippet, so that we can somehow read the class properties with reflection and define them as snippet parameters?)

CodePudding user response:

If you have a class with 20 properties, why do you need a constructor with 20 parameters? Maybe have a sense, but I usually create constructors to initialize properties that are relevant, to simplify the code, not to set all properties.

For your class, you can set the default values when you define the property and all constructors will use this values as the default.

public class User
{
    public int Id { get; set; } = 0;
    public string Name { get; set; } = string.Empty;

    // Here you can even omit the constructor
    public User()
    {
    }
}

Another thing that maybe useful is define a constructor with X parameters and reuse this constructor in other constructors with less parameters:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }

    public User()
        : this(0, string.Empty)
    {
    }

    public User(int id, string name)
    {
        Id = id;
        Name = name;
    }
}

You can replace this(0, string.Empty) for this(default, default) if you want use the default value of each type.

CodePudding user response:

do you mean initialize properties? Initializing properties through the code reflection mechanism also requires one-by-one assignments. For private object properties, it is necessary to de-private encapsulation. The operation of initializing properties in c# is generally to initialize object properties or object initializers in the form of constructors. Thank you hope it helps you

 class Program
  {
    static void Main(string[] args)
    {
        Student student = new Student()
        {
            age = 25,
            name = "java",
            sex = "男"
        };
    }
   class Student
    {
        public int age { get; set; }
        public string name { get; set; }
        public string sex { get; set; }
        public Student()
        {

        }
        public Student(int age, string name,string sex)
        {
            this.age = age;
            this.name = name;
            this.sex = sex;
        }
    }

}

CodePudding user response:

If you need object create with default value for properties. You can code like this:

public class User
{
    public int Id { get; set; } = 0;
    public string Name { get; set; } = "";
}

Purpose of quick action "generate constructor" make method contructor for assign value to fields or properties. Don't use it in the case of just assigning default values.

  • Related