简体   繁体   English

如何修复“字段 X 从未分配给并且其默认值为空”?

[英]How to fix "Field X is never assigned to and will have its default value null"?

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).我还创建了一个不起作用的方法 SetFullAddress(如果是因为该消息,则为 idk)。

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

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


        }

    }

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)您可以安全地删除它们并按如下方式重写 SetFullAddress 方法(使用自动实现的属性)

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 .您可以在此处阅读有关自动实现的属性的更多信息

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 编译器警告(级别 4)CS0649

Field 'field' is never assigned to, and will always have its default value 'value'字段“field”永远不会分配给,并且将始终具有其默认值“value”

The compiler detected an uninitialized private or internal field declaration that is never assigned a value.编译器检测到从未赋值的未初始​​化私有或内部字段声明。

The following sample generates 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()  
   {  
   }  
} 

If you're not encountering any errors and just want to disable the warnings from that script you caa use #pragma warning disable 0649如果您没有遇到任何错误并且只想禁用来自该脚本的警告,您可以使用#pragma warning disable 0649

But don't abuse it, these warnings may help you later.但是不要滥用它,这些警告可能会在以后对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告:永远不会将字段分配给,并且始终将其默认值设置为null - Warning: Field is never assigned to, and will always have its default value null 字段'xxx'永远不会分配给,并且将始终具有其默认值null - Field 'xxx' is never assigned to, and will always have its default value null 永远不会将字段xxx分配给,并始终将其默认值设置为null - Field xxx is never assigned to, and will always have its default value null 字段…永远不会分配给它,并且其默认值始终为null - Field … is never assigned to, and will always have its default value null 该字段永远不会分配给它,并且其默认值始终为null - The Field Is Never Assigned To And Will Always Have Its Default Value null 字段从未分配给它,并且将始终具有其默认值null - Field is never assigned to, and will always have its default value null 私有类中的公共字段:“Field [FieldName]永远不会分配给,并且将始终具有其默认值null” - Public field in private class: “Field [FieldName] is never assigned to, and will always have its default value null” 字段“ FIELD”从未分配,并且将始终具有其默认值 - field 'FIELD' is never assigned to, and will always have its default value 字段'XXX.AppMain.p1'从未分配,并且其默认值始终为空 - Field 'XXX.AppMain.p1' is Never Assigned to, And Will Always Have Its Default Value Null 永远不会分配c#字段,并且其默认值始终为null? - c# field is never assigned to and will always have its default value null?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM