C# – Is a static variable shared across all instance

cc#-4.0net

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)?

Best Answer

Your static variable TaxBrakets is not associated with an instance. this.TaxBrakets would not compile. All occurrences of TaxBrakets will 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.