如何解决“字段X永远不会分配给它,并且其默认值为null”?

时间:2019-01-04 01:19:00

标签: c# null field default-value

我正在尝试从控制台(街道,城市,国家/地区)输入输入,但是这些字段带有下划线并显示消息(该字段从未分配给该字段,并且其值为null)。我还创建了一个SetFullAddress方法,该方法不起作用(如果是由于该消息,则为idk)。

地址类中的代码:

public class Address
{
    private string street;
    private string city;
    private string country;

    public Address()
    {
        this.Street = street;
        this.City = city;
        this.Country = country;
    }

    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }

    public string SetFullAddress()
    {
        return ($"Full address: {street} {city} {country}");
    }

    public void DisplayAddress()
    {
        Console.WriteLine($"Street: {Street}");
        Console.WriteLine($"City: {City}");
        Console.WriteLine($"Country: {Country}");
        Console.WriteLine(SetFullAddress());


    }

}

在Main方法中:

        Address address = new Address();
        Console.Write("Street: ");
        address.Street = Console.ReadLine();

        Console.Write("City: ");
        address.City = Console.ReadLine();

        Console.Write("Country: ");
        address.Country = Console.ReadLine();

        Console.WriteLine();
        address.DisplayAddress();

4 个答案:

答案 0 :(得分:0)

这可能会帮助您:

   public static void Main()
    {

        Console.Write("Street: ");
        string street = Console.ReadLine();

        Console.Write("City: ");
        string city = Console.ReadLine();

        Console.Write("Country: ");
        string country = Console.ReadLine();

        Address address = new Address(street, city, country);
        Console.WriteLine();
        address.DisplayAddress();
    }

    public class Address
    {
        private string street;
        private string city;
        private string country;

        public Address(string street, string city, string country)
        {
            this.street = street;
            this.city = city;
            this.country = country;
        }

        public string Street {
            get => street;
            set => street = value;
        }
        public string City {
            get => city;
            set => city = value;
        }
        public string Country {
            get => country;
            set => country = value;
        }

        public string SetFullAddress()
        {
            return ($"Full address: {street} {city} {country}");
        }

        public void DisplayAddress()
        {
            Console.WriteLine($"Street: {Street}");
            Console.WriteLine($"City: {City}");
            Console.WriteLine($"Country: {Country}");
            Console.WriteLine(SetFullAddress());


        }

    }

答案 1 :(得分:0)

警告是告诉您确切的问题是,永远不会分配以下内容

private string street;
private string city;
private string country;

也许您想在构造函数中初始化实际属性

//private string street;
//private string city;
//private string country;

public Address(string street, string city, string country)
{
    this.Street = street;
    this.City = city;
    this.Country = country;
}

Compiler Warning (level 4) CS0649

  

字段“字段”从未分配给它,并且始终具有其默认值   值“值”

     

编译器检测到未初始化的私有或内部字段   从未分配值的声明。

以下示例生成CS0649:

class MyClass  
{  
   Hashtable table;  // CS0649  
   // You may have intended to initialize the variable to null  
   // Hashtable table = null;  

   // Or you may have meant to create an object here  
   // Hashtable table = new Hashtable();  

   public void Func(object o, string p)  
   {  
      // Or here  
      // table = new Hashtable();  
      table[p] = o;  
   }  

   public static void Main()  
   {  
   }  
} 

答案 2 :(得分:0)

出现警告的原因是您从不使用(读取分配值)私有字段。

    private string street;
    private string city;
    private string country;

    public Address()
    {
        this.Street = street;
        this.City = city;
        this.Country = country;
    }

    public string Street { get; set; }
    public string City { get; set; }
    public string Country { get; set; }

您是,而不是使用“自动实施的属性”。您可以安全地删除它们,然后按照以下方式重写您的SetFullAddress方法(使用自动实施的属性)

public string SetFullAddress()
{
    return ($"Full address: {Street} {City} {Country}");
}

或者您可以使用隐式键入的私有后备字段创建属性,

public string Street 
{ 
    get => street; 
    set => street = value; 
}
public string City
{ 
    get => city; 
    set => city = value; 
}
public string Country
{ 
    get => country; 
    set => country = value; 
}

请注意,当您使用“自动实施”属性时,编译器会创建后备字段。您可以阅读有关自动实施属性here的更多信息。

答案 3 :(得分:0)

如果您没有遇到任何错误,只是想从该脚本中禁用警告,则可以使用#pragma warning disable 0649

但是请不要滥用它,这些警告以后可能会对您有所帮助。