C# 嵌套类-获取为所有的公共类,仅为外部类设置

C# 嵌套类-获取为所有的公共类,仅为外部类设置,c#,C#,如何设置修改器?我想让嵌套类中的“get”为所有人公开,只为外部类设置? 错误: 索引器“Cart.CartItem.Quantity”的属性不能在中使用 此上下文不可用,因为集合访问器不可访问 'Cart.CartItem.CartItem(Guid itemId、字符串名称、十进制价格、整数 数量)“”由于其保护级别而不可访问 代码: 公共类购物车 { 公共列表项{get;private set;} 公共整数TotalQuantity=>CartItems.Sum(x=>x.Quantity

如何设置修改器?我想让嵌套类中的“get”为所有人公开,只为外部类设置? 错误:

索引器“Cart.CartItem.Quantity”的属性不能在中使用 此上下文不可用,因为集合访问器不可访问 'Cart.CartItem.CartItem(Guid itemId、字符串名称、十进制价格、整数 数量)“”由于其保护级别而不可访问

代码:

公共类购物车
{
公共列表项{get;private set;}
公共整数TotalQuantity=>CartItems.Sum(x=>x.Quantity);
公共十进制TotalPrice=>CartItems.Sum(x=>x.价格*x.数量);
公共购物车()
{
CartItems=新列表();
}
公共无效附加项(Guid项ID、字符串名称、十进制价格)
{
CartItem CartItem=CartItems.Find(x=>x.ItemId==ItemId);
如果(cartItem!=null)
{
cartItem.数量+=1;
}
其他的
{
添加(新的CartItem(itemId,name,price,1));
}
}
公共类商品
{
公共Guid ItemId{get;private set;}
公共字符串名称{get;private set;}
公共整数数量{get;私有集;}
公共十进制价格{get;private set;}
私有CartItem(Guid itemId、字符串名称、十进制价格、整数数量)
{
ItemId=ItemId;
名称=名称;
价格=价格;
数量=数量;
}
}
}

您没有完全理解使用嵌套类型的原因

嵌套类型可以访问封闭类型中定义的私有字段

public class Cart {
    List<CartItem> CartItems { get; set; }
    public int TotalQuantity => CartItems.Sum(x => x.Quantity);
    public decimal TotalPrice => CartItems.Sum(x => x.Price * x.Quantity);

    public Cart() {
        CartItems = new List<CartItem>();
    }

    public void AddItem(Guid itemId, string name, decimal price) {
        CartItem cartItem = CartItems.Find(x => x.ItemId == itemId);

        if (cartItem != null) {
            cartItem.Quantity += 1;
        } else {
            CartItems.Add(new CartItem(itemId, name, price, 1));
        }
    }

    class CartItem {
        public Guid ItemId { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }

        public CartItem(Guid itemId, string name, decimal price, int quantity) {
            ItemId = itemId;
            Name = name;
            Price = price;
            Quantity = quantity;
        }
    }
}

class Program {
    static void Main(string[] args) {
        var test = new Cart.CartItem(Guid.Empty, "", 0.0m, 10); // Error CS0122  'Cart.CartItem' is inaccessible due to its protection level 


    }
}
请看有关的链接

避免公开公开的嵌套类型。唯一的例外是,如果嵌套类型的变量只需要在少数情况下声明,例如子类化或其他高级自定义情况

如果类型可能在包含类型之外被引用,则不要使用嵌套类型

因此,正确的方法是保持类的私有性和成员的公共性,这样嵌套类型的成员和字段只能由封闭类型访问

public class Cart {
    List<CartItem> CartItems { get; set; }
    public int TotalQuantity => CartItems.Sum(x => x.Quantity);
    public decimal TotalPrice => CartItems.Sum(x => x.Price * x.Quantity);

    public Cart() {
        CartItems = new List<CartItem>();
    }

    public void AddItem(Guid itemId, string name, decimal price) {
        CartItem cartItem = CartItems.Find(x => x.ItemId == itemId);

        if (cartItem != null) {
            cartItem.Quantity += 1;
        } else {
            CartItems.Add(new CartItem(itemId, name, price, 1));
        }
    }

    class CartItem {
        public Guid ItemId { get; set; }
        public string Name { get; set; }
        public int Quantity { get; set; }
        public decimal Price { get; set; }

        public CartItem(Guid itemId, string name, decimal price, int quantity) {
            ItemId = itemId;
            Name = name;
            Price = price;
            Quantity = quantity;
        }
    }
}

class Program {
    static void Main(string[] args) {
        var test = new Cart.CartItem(Guid.Empty, "", 0.0m, 10); // Error CS0122  'Cart.CartItem' is inaccessible due to its protection level 


    }
}
公共类购物车{
列表项{get;set;}
公共整数TotalQuantity=>CartItems.Sum(x=>x.Quantity);
公共十进制TotalPrice=>CartItems.Sum(x=>x.价格*x.数量);
公共购物车(){
CartItems=新列表();
}
公共无效附加项(Guid项ID、字符串名称、十进制价格){
CartItem CartItem=CartItems.Find(x=>x.ItemId==ItemId);
如果(cartItem!=null){
cartItem.数量+=1;
}否则{
添加(新的CartItem(itemId,name,price,1));
}
}
类CartItem{
公共Guid ItemId{get;set;}
公共字符串名称{get;set;}
公共整数数量{get;set;}
公共十进制价格{get;set;}
公共CartItem(Guid itemId、字符串名称、十进制价格、整数数量){
ItemId=ItemId;
名称=名称;
价格=价格;
数量=数量;
}
}
}
班级计划{
静态void Main(字符串[]参数){
var test=new Cart.CartItem(Guid.Empty,“”,0.0m,10);//错误CS0122“Cart.CartItem”由于其保护级别而无法访问
}
}

生成此错误的代码是什么?@ZoharPeled,我认为这是一个错误=>
cartItem.Quantity+=1因为
set
对于
CartItem
中的
Quantity
是私有的class@er-是的,你可能是对的。它应该编译,除了
Main
中的代码,它将显示注释错误