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();
The reason for the warnings are you are never using (read assigning values to) the private fields.
You are , instead using Auto Implemented Properties. You can, either safely remove them and rewrite your SetFullAddress method as following (using Auto Implemented Properties)
Or you can creating Properties with implicitly typed private backing fields as
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.
This might help you:
The warning is telling you exactly what is the problem, the following are never assigned
Maybe you wanted to initialise the actual properties in the constructor
Compiler Warning (level 4) CS0649
The following sample generates CS0649: