C# related on Constructor and Property

江枫思渺然 提交于 2019-12-24 00:48:12

问题


I am building some tiny lib, and I have run into a problem. I want to provide a two-way solution, for example:

How can I accomplish this? I am getting exception thrown, because it expects something... Any example that will do is welcomed :) Thanks!

EDIT: I am executing something, initially my code is similar to this one:

 System.IO.DriveInfo d = new System.IO.DriveInfo("C:"); 

I want to achieve with my class the following:

Driver d = new Driver(); 
d.DriverLetter = "C:"; 

And still get the same results, I use ManagementObjectSearch, ManagementObjectCollection and some other System.Management classes.


回答1:


You need to provide both constructors:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }

    // Paramterless constructor  -  for   new Person();
    // All properties get their default values (string="" and int=0)
    public Person () { }

    // Parameterized constructor -  for   new Person("Joe", 16, "USA");
    public Person (string name, int age, string country)
    {
        Name = name;
        Age = age;
        Country = country;
    }
}

If you define a parameterized constructor, the default parameterless constructor is not included for you. Therefore you need to include it yourself.

From MSDN 10.10.4 Default constructors:

If a class contains no instance constructor declarations, a default instance constructor is automatically provided.




回答2:


You have to define a constructor that takes those three arguments:

public class Person
{
    public Person(string name, string age, string country)
    {
        this.Name = name;
        this.Age = age;
        this.Country = country;
    }
 }

This way, you can assign the values to the properties when the class is constructed. You can have more than one constructor for a class taking different parameters and you can have one constructor call another constructor with : this() syntax:

public class Person
{
    public Person()
        : this(string.Empty, string.Empty, string.Empty)
    {

    }

    public Person(string name, string age, string country)
    {
        this.Name = name;
        this.Age = age;
        this.Country = country;
    }
 }

Here the "empty" constructor will call the other constructor and set all properties to empty strings.




回答3:


Try this:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }

    public Person()
    {
    }

    public Person(string name, int age, string country)
    {
        Name = name;
        Age = age;
        Country = country;
    }
}

class Test
{
    static void Main(string[] args)
    {
        var person1 = new Person();
        person1.Name = "Joe";
        person1.Age = 2;
        person1.Country = "USA";

        var person2 = new Person("John", 4, "USA");
    }
}

The .NET Framework will implicitly provide a default/parameterless constructor if you don't define a constructor. If you define a parameterized constructor, though, you need to explicitly define a default constructor too.




回答4:


You probably missing your Age property type as int or string.

class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Country { get; set; }
    public Person()
    {

    }
    public Person(string name, int age, string country)
    {
        this.Name = name;
        this.Age = age;
        this.Country = country;
    }
}
class Program
{
    static void Main(string[] args)
    {
        Person p1 = new Person("Erik", 16, "United States");

        Person p2 = new Person();
        p2.Name = "Erik";
        p2.Age = 16;
        p2.Country = "United States"; 
    }
}

EDIT: Also you need parameterless constructor for also.



来源:https://stackoverflow.com/questions/16559937/c-sharp-related-on-constructor-and-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!