How to fix “Field X is never assigned to and will

2019-08-02 17:29发布

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

3条回答
虎瘦雄心在
2楼-- · 2019-08-02 17:41

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.

查看更多
Evening l夕情丶
3楼-- · 2019-08-02 17:48

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


        }

    }
查看更多
爷的心禁止访问
4楼-- · 2019-08-02 17:53

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()  
   {  
   }  
} 
查看更多
登录 后发表回答