【问题标题】:How to fix "Field X is never assigned to and will have its default value null"?如何修复“字段 X 从未分配给并将其默认值为 null”?
【发布时间】:2019-05-30 14:46:22
【问题描述】:

我正在尝试从控制台(街道、城市、国家/地区)输入输入,但字段带有下划线并显示消息(字段从未分配给并将其值为 null)。我还创建了一个不起作用的方法 SetFullAddress(如果是因为该消息,则为 idk)。

Address 类中的代码:

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


    }

}

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

【问题讨论】:

  • @ViRuSTriNiTy 我是初学者,我需要澄清和更多信息。这就是我问的原因。感谢您的帮助!

标签: c# null field default-value


【解决方案1】:

如果您没有遇到任何错误并且只想禁用该脚本的警告,您可以使用#pragma warning disable 0649

但不要滥用它,这些警告可能会在以后对您有所帮助。

【讨论】:

    【解决方案2】:

    这可能会对您有所帮助:

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

    【讨论】:

    • 构造函数是我任务的一部分,所以我认为我需要使用它
    • 好的,那么请参考TheGeneral的回答。
    • @Ada 我编辑了我的答案。请检查是否是您想要的
    【解决方案3】:

    警告的原因是您从未使用(读取分配值)私有字段。

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

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

    【讨论】:

      【解决方案4】:

      警告告诉你到底是什么问题,以下永远不会分配

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

      Compiler Warning (level 4) CS0649

      字段 'field' 永远不会分配给,并且将始终具有其默认值 价值'价值'

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

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

      【讨论】:

      • 我删除了字段并向构造函数添加了三个字符串参数,但现在 Address address = new Address();显示错误。我不应该输入任何信息,例如:Address address = new Address("street", "city", "country");而是让用户从控制台输入该信息。知道如何解决这个问题吗?
      • @Ada 完全删除该结构或添加一个额外的默认结构public Address() {}
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2021-10-13
      相关资源
      最近更新 更多