Understanding dictionaries, adding new values to the dictionary using c#

c#, dictionary, initialization

Solution

The Add() methode does not return a value which you can assign to `cust.Payment`, you need to create the dictionary then call the Add() methode of the created Dictionary object:

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

Problem

If I have Customer object which have Payment property which is dictionary of custom enum type and decimal value like ``` Customer.cs public enum CustomerPayingMode { CreditCard = 1, VirtualCoins = 2, PayPal = 3 } public Dictionary<CustomerPayingMode, decimal> Payment; ``` In client code I have problem with adding values to the Dictionary, tried like this ``` Customer cust = new Customer(); cust.Payment = new Dictionary<CustomerPayingMode,decimal>() .Add(CustomerPayingMode.CreditCard, 1M); ```

Original source