简体   繁体   中英

Set property default value before instantiation

I have a class named Student :

public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

When I create an instanse of Student class , it is null .

Student s = new Student();

" s.ID is null , s.Name is null and s.Age is null ."

I want to set a default value for Student class , so when I create instanse of it :

Student s = new Student();

" s.ID = 1 , s.Name = Parsa , s.Age = 20 "

In the other word , I want to change declaration or implementation of property getter or override it .

How can I do this ?

Update

I khow I can do this with Static class or define Constractor , but I do not have access to Student class and I want it Not-Static . I think this problem can be solved with Reflection

Thank you in Advance.

You'll have to initialize the field the the value you want as a default. For example:

class MyClass
{
    int value = 42;

    public int Value
    {
        get {return this.value;}
        set {this.value = value;}
    }
}

You can use the DefaultValue attribute to inform designers of this non-zero default:

class MyClass
{
    int value = 42;

    [DefaultValue(42)]    
    public int Value
    {
        get {return this.value;}
        set {this.value = value;}
    }
}

由于您无权访问Student类,因此只需将您的类包装到具有获取和设置Student类的属性的属性中,然后将其包装到另一个类中,并在新的类构造函数中根据需要定义默认值。

Simplely create a constructor with default values assigned:

public Student(){
   this.ID = 1;
   this.Name = "Some name";
   this.Age = 25;
}

When I create an instanse of Student class , it is null .

" s.ID is null , s.Name is null and s.Age is null ."

Firstly, Age and ID can't be null because they're value types not reference types.

Secondly, the member variables don't return any useful data because the properties are not initialised hence for numeric types it will be 0 and for reference types it will be null.

I want to set a default value for Student class , so when I create instanse of it :

Student s = new Student(); " s.ID = 1 , s.Name = Parsa , s.Age = 20 "

There are three solutions which I can think of:

Solution-1: Object initializers

Student student = new Student { ID = 1, Name = "Parsa", Age = 20 }; // from C# 3.0

Solution-2: auto property initializers

public class Student{
     public int ID { get; set; } = 1; 
     public int Name { get; set; } = "Parsa"; // C# 6 or higher
     public int Age { get; set; } = 20;
}

Solution-3: Using a constructor

public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }

    public Student(int id, String name, int age){
       this.ID = id;
       this.Name = name;
       this.Age = age;
    }
}

call it like this:

Student s = new Student(1,"Parsa",20);
{
    private int _id = 0;
    public int ID
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }
    private string _Name = string.Empty;
    public string Name 
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
        }
    }

    private int _Age = 0;
    public int Age 
    {
        get
        {
            return _Age;
        }
        set
        {
            _Age = value;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM