📌  相关文章
📜  属性“productForm”没有初始化程序,也没有在构造函数中明确分配. (1)

📅  最后修改于: 2023-12-03 14:53:57.506000             🧑  作者: Mango

属性“productForm”没有初始化程序,也没有在构造函数中明确分配.

这个错误通常表示在类的构造函数中的某个属性没有被初始化或赋值。在实例化对象时,该属性被称为未初始化的 “null” 值,因此在后续访问该属性时会引发错误。

常见原因
  • 构造函数中没有初始化该属性
  • 该属性没有设置默认值
  • 操作符重载显示调用了构造函数
错误示例
public class Product
{
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductForm { get; set; } // 没有初始化或赋值的属性

    public Product(string name, string description)
    {
        ProductName = name;
        ProductDescription = description;
    }
}

public class Program
{
    static void Main(string[] args)
    {
        var product = new Product("Shampoo", "For healthy hair");
        Console.WriteLine(product.ProductForm.Trim()); // 会引发错误:属性“productForm”没有初始化程序,也没有在构造函数中明确分配.
    }
}
解决方案

1. 在构造函数中初始化属性

最简单的解决方案是在声明属性时设置默认值,在构造函数中对其进行赋值,或在构造函数中初始化它们。

public class Product
{
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductForm { get; set; } = string.Empty; // 设置默认值

    public Product(string name, string description, string form)
    {
        ProductName = name;
        ProductDescription = description;
        ProductForm = form; // 在构造函数中初始化
    }
}

public class Program
{
    static void Main(string[] args)
    {
        var product = new Product("Shampoo", "For healthy hair", "Liquid");
        Console.WriteLine(product.ProductForm.Trim()); // Liquid
    }
}

2. 初始化属性

初始化器是一个新特性,它允许您在声明的同时实现属性的初始化。

public class Product
{
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductForm { get; set; } = string.Empty; // 使用初始化器

    public Product(string name, string description)
    {
        ProductName = name;
        ProductDescription = description;
    }
}

public class Program
{
    static void Main(string[] args)
    {
        var product = new Product { ProductName = "Shampoo", ProductDescription = "For healthy hair", ProductForm = "Liquid" };
        Console.WriteLine(product.ProductForm.Trim()); // Liquid
    }
}

3. 使用属性默认值

如果属性具有默认值,则可以在构造函数中跳过对其进行初始化。

public class Product
{
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    public string ProductForm { get; set; } = "Tablet"; // 设置默认值

    public Product(string name, string description, string form = null) //使用可选参数
    {
        ProductName = name;
        ProductDescription = description;
        if (form != null)
        {
            ProductForm = form;
        }
    }
}

public class Program
{
    static void Main(string[] args)
    {
        var product1 = new Product("Vitamin C", "For immune system support");
        Console.WriteLine(product1.ProductForm.Trim()); // Tablet

        var product2 = new Product("Calcium", "For strong bones", "Capsule");
        Console.WriteLine(product2.ProductForm.Trim()); // Capsule
    }
}
结论

这个错误通常是由于在构造函数中初始化属性时被忽略所导致的。建议在声明属性时初始化或使用构造函数/初始化器对其进行初始化。如果您需要更高级的方案,请考虑使用属性默认值或设置可选参数。