如何解决“现场X从未分配,将有它的默认值空”?(How to fix “Field X is nev

2019-09-28 09:01发布

我试图从控制台(街道,城市,国家)进入输入,但是字段下划线并显示信息(场永远不会分配给和将其null值)。 我也创建了不工作(IDK如果是因为该消息的)的方法SetFullAddress。

地址类中的代码:

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());


    }

}

而且里面主要的方法:

        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();

Answer 1:

该警告是告诉你到底是什么问题,以下是从未分配

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;
}

编译器警告(级别4)CS0649

现场“场”永远不会分配给,永远有它的默认值“value”

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

下面的示例生成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()  
   {  
   }  
} 


Answer 2:

究其原因,警告你永远不会使用(读分配值)的私有字段。

    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; 
}

请注意,当您使用自动实现的属性,编译器创建的支持字段。 你可以阅读更多关于自动实现的属性在这里 。



Answer 3:

这可以帮助你:

   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());


        }

    }


文章来源: How to fix “Field X is never assigned to and will have its default value null”?