简体   繁体   English

C#中带参数的构造方法

[英]Constructor with arguments in C#

C# question: How can I use the constructor: C#问题:我如何使用构造函数:

AcctHolder ah1 = new AcctHolder("Dumitru", "St", "Bucharest");

and be able to obtain ah1.Fname ? 并且能够获得ah1.Fname (instead of null) (而不是null)

using System;

    namespace ConsoleApplication1
    {
        class ATM
        {
            public static void Main(string[] args)
            {
                AcctHolder ah1 = new AcctHolder("Dumitru", "St", "Bucharest");
                Console.WriteLine(ah1.FName); //returns null - why???

                AcctHolder ah2 = new AcctHolder();
                ah2.FName = "Dumi";
                Console.WriteLine(ah2.FName); // returns "Dumi"

                Console.ReadKey();
            }


            public class AcctHolder
            {
                private string fname, lname, city;
                public string FName { get; set; }
                public string LName { get; set; }
                public string City {
                    get { return city; }
                    set { city = value; }

                }
                public AcctHolder(string a, string b, string c)
                {
                    fname = a;
                    lname = b;
                    city = c;
                }
                public AcctHolder()
                {

                }
            }

        }
    }

returns null - why??? 返回null - 为什么???

Because you are initializing unrelated fields in the constructor not the backing fields of the properties. 因为您正在构造函数中初始化不相关的字段而不是属性的支持字段。 You don't need them with auto-implemented properties : 您不需要具有自动实现的属性

public class AcctHolder
{
    public string FName { get; set; }
    public string LName { get; set; }
    public string City { get; set; }
    public AcctHolder(string a, string b, string c)
    {
        FName = a;
        LName  = b;
        City = c;
    }
    public AcctHolder()
    {

    }
}

If you want to keep the backing fields: 如果要保留支持字段:

public class AcctHolder
{
    private string fname;
    public string FName 
    {
        get { return fname; }
        set { fname = value; }
    }

    private string lname;
    public string LName 
    {
        get { return lname; }
        set { lname = value; }
    }

    private string city;
    public string City
    {
        get { return city; }
        set { city = value; }
    } 
    public AcctHolder(string a, string b, string c)
    {
        fname = a;
        lname = b;
        city = c;
    }
    public AcctHolder()
    {

    }
}

You are setting the wrong (private) fname in your constructor. 您在构造函数中设置了错误的(私有) fname Use the public one. 使用公共的

change 更改

public AcctHolder(string a, string b, string c)
{
    fname = a;
    lname = b;
    city = c;
}

to

public AcctHolder(string a, string b, string c)
{
    Fname = a;
    LName = b;
    City = c;
}

In the constructor, set 在构造函数中,设置

FName = a;

instead of 代替

fname = a;

The properties FName and LName don't need the bo backed with fields since you're using get;set; 属性FNameLName不需要支持字段的bo,因为你正在使用get;set; -syntax to declare them. -syntax宣布他们。 They get that automatically, so you can just remove fname and lname from your code completely. 它们会自动获取,因此您可以完全从代码中删除fnamelname

Since you use autoproperties you dont need the private fields. 由于您使用autoproperties,您不需要私有字段。 Simplify: 简化:

public class AcctHolder
{
    public string FName { get; set; }
    public string LName { get; set; }
    public string City { get;set;}

    public AcctHolder(string a, string b, string c)
    {
        FName = a;
        LName = b;
        City = c;
    }
    public AcctHolder()
    {

    }
}

Alternatively, change the class to use the private strings as you have with City. 或者,更改类以使用与City一样的私有字符串。

        public class AcctHolder
        {
            private string fname, lname, city;
            public string FName { get {return fname;} set {fname = value; }
            public string LName { get {return lname;} set {lname = value;} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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