In a public class, I have a private static Dictionary. Since the Dictionary is static, does it mean that it is shared across all the other instance of the same object (see example below).
public class Tax
{
private static Dictionary<string, double> TaxBrakets = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase)
{
{ "Individual", 0.18 },
{ "Business", 0.20 },
{ "Other", 0.22 },
};
public string Type { get; set; }
public double ComputeTax(string type, double d)
{
return d * TaxBrakets[this.Type];
}
}
Is that acceptable to use a Dictionary in that way (as static variable)?
Your static variable
TaxBraketsis not associated with an instance.this.TaxBraketswould not compile. All occurrences ofTaxBraketswill refer to the same dictionary. In general, it’s totally acceptable to use static dictionaries. This particular use seems a little funny though, but I’d need to see more code to suggest any changes.