How to fix “Field X is never assigned to and will have its default value null”?

♀尐吖头ヾ 提交于 2019-12-11 04:14:19

问题


I am trying to enter input from the console(street, city, country) but the fields are underlined and display the message(field is never assigned to and will have its value null). I have also created a method SetFullAddress which doesn't work(idk if it is because of that message).

Code inside Address class:

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


    }

}

And inside Main method:

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

回答1:


The warning is telling you exactly what is the problem, the following are never assigned

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

Maybe you wanted to initialise the actual properties in the constructor

//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

Field 'field' is never assigned to, and will always have its default value 'value'

The compiler detected an uninitialized private or internal field declaration that is never assigned a value.

The following sample generates 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:


The reason for the warnings are you are never using (read assigning values to) the private fields.

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

You are , instead using Auto Implemented Properties. You can, either safely remove them and rewrite your SetFullAddress method as following (using Auto Implemented Properties)

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

Or you can creating Properties with implicitly typed private backing fields as

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

Please be aware that when you are using Auto Implemented properties, compiler creates the backing fields. You can read more on Auto Implemented Properties here.




回答3:


This might help you:

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


        }

    }


来源:https://stackoverflow.com/questions/54031995/how-to-fix-field-x-is-never-assigned-to-and-will-have-its-default-value-null

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