gpt4 book ai didi

c# - 理解字典,使用c#向字典添加新值

转载 作者:太空狗 更新时间:2023-10-30 00:25:46 24 4
gpt4 key购买 nike

如果我有 Customer 对象,它有 Payment 属性,它是自定义枚举类型和十进制值的字典,如

Customer.cs
public enum CustomerPayingMode
{
CreditCard = 1,
VirtualCoins = 2,
PayPal = 3
}
public Dictionary<CustomerPayingMode, decimal> Payment;

在客户端代码中,我在向字典添加值时遇到问题,像这样尝试

Customer cust = new Customer();
cust.Payment = new Dictionary<CustomerPayingMode,decimal>()
.Add(CustomerPayingMode.CreditCard, 1M);

最佳答案

Add()方法不返回可以分配给 cust.Payment 的值,您需要创建字典然后调用创建的 Dictionary 对象的 Add() 方法:

Customer cust = new Customer();
cust.Payment = new Dictionary<CustomerPayingMode,decimal>();
cust.Payment.Add(CustomerPayingMode.CreditCard, 1M);

关于c# - 理解字典,使用c#向字典添加新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15615816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com