如何在C#跨类场景下定义ProductPrices多维数组
代码存在的具体问题
- 字典访问逻辑错误:定义的
productInfo是Dictionary<string, ProductData>字典容器,本身没有Name/Description这类产品实体属性,必须先通过字典的键取出对应的ProductData实例,才能访问这些实体属性,直接对字典对象取属性会触发编译报错 - 属性名不匹配:
ProductData类中定义的价格集合属性名称为Prices,取值代码里写的ProductPrices[0]和类内实际的属性名不匹配,同样会触发编译错误 - 结构认知偏差:你提到的“为ProductPrices配置多维数组”,本质是需要实现“单产品关联多个批发价格档位”的嵌套结构,当前代码里的
ProductPrices[]一维数组已经完全可以承载这类需求——所谓的多层结构是指「产品对象 → 产品下的价格数组 → 单条价格档位的属性」的嵌套访问路径,不需要数组本身达到多维维度 - 潜在异常风险:类中的数组、List集合属性如果没有显式初始化,直接通过索引/下标访问会触发空引用异常;数组访问时如果没有判断长度,还可能触发索引越界异常
可直接运行的修正方案
首先优化类结构,把裸公共字段改为标准属性(更符合C#编码规范,方便后续加校验逻辑),同时提前初始化集合避免空引用:
// 价格档位类:将裸字段改为自动属性,遵循C#命名规范 public class ProductPrices { public decimal Price { get; set; } public int MinQuantity { get; set; } public DateTime ValidUntil { get; set; } public string Currency { get; set; } } // 产品特性类:将裸字段改为自动属性 public class ProductFeatures { public string Feature { get; set; } } // 产品数据类 class ProductData { private ProductPrices[] prices = Array.Empty<ProductPrices>(); public string ParentName { get; set; } public string Name { get; set; } public string Description { get; set; } public string TechnicalDescription { get; set; } public bool Obsolete { get; set; } public bool Deleted { get; set; } public string Innercarton { get; set; } public string Outercarton { get; set; } public string Package { get; set; } public string Barcode { get; set; } public decimal Weight { get; set; } public decimal EndUserPrice { get; set; } public int MOQ { get; set; } public bool ComingSoon { get; set; } public bool NewProduct { get; set; } public string Equivalent { get; set; } public string ReplacedBy { get; set; } public ProductPrices[] Prices { get => prices; // 赋值为null时自动替换为空数组,从根源避免空引用 set => prices = value ?? Array.Empty<ProductPrices>(); } public List<ProductFeatures> Feature { get; set; } = new(); public List<string> Specification { get; set; } = new(); public List<string> Image { get; set; } = new(); public string File { get; set; } public string Icon { get; set; } }
然后是匹配你预期访问路径的正确取值逻辑:
// 初始化产品字典,可按需填充全量产品数据 Dictionary<string, ProductData> productInfo = new Dictionary<string, ProductData>(); // 示例:向字典中新增一条产品数据 productInfo.Add("PROD-001", new ProductData { Name = "工业级螺丝刀", Description = "铬钒钢材质耐磨损", Prices = new ProductPrices[] { new ProductPrices { Price = 12.5m, MinQuantity = 1, Currency = "CNY" }, new ProductPrices { Price = 9.8m, MinQuantity = 100, Currency = "CNY" }, new ProductPrices { Price = 7.2m, MinQuantity = 1000, Currency = "CNY" } } }); // 正确的取值流程 // 1. 先从字典中根据键取出对应的产品实体 if (productInfo.TryGetValue("PROD-001", out ProductData product)) { // 2. 访问产品基础属性 string productName = product.Name; string description = product.Description; // 3. 访问产品下的第一档批发价格(判断数组长度避免越界) if (product.Prices.Length > 0) { decimal wholesalePrice = product.Prices[0].Price; int minQty = product.Prices[0].MinQuantity; } }
优化建议
- 如果后续需要动态增减价格档位,比起
ProductPrices[]定长数组,更推荐用List<ProductPrices>集合,不需要手动维护数组长度,可以直接通过Add()/Remove()方法增减档位 - 所有嵌套的数组/集合访问前,必须先判断集合本身是否为空、集合长度是否满足索引要求,避免线上出现空引用、索引越界类异常
内容的提问来源于stack exchange,提问作者Paul Albers
相关产品推荐
相关产品推荐

