Let’s say I have:
public class Fruit
{
public static List<String> Suppliers { get; protected set; }
static Fruit()
{
Suppliers = new List<String>();
Suppliers.Add("Company A");
}
}
public class Banana : Fruit
{
static Banana()
{
Suppliers.Add("Company B");
}
}
If I just do this in the calling code:
foreach(String supplier in Banana.Suppliers)
Console.WriteLine(supplier);
I get:
- Company A
Whereas if I do:
Banana b = new Banana();
foreach(String supplier in Banana.Suppliers)
Console.WriteLine(supplier);
I get (the desired result):
- Company A
- Company B
Edit:
After reading the responses I understand that this won’t work.
What I want in my production code is a list of values that is common to the type of object and I want to dynamically add different values to that list of strings based on the subtype. (The context is LDAP – all entries have objectClass=top and all user-objects have objectClass=user,top,organizationPerson,person). Guess I have to use an interface or different lists in each subclass or something if no one has a better suggestion?
For one thing, accessing
Banana.Suppliersis misleading. It will always yield the same result as accessingApple.Suppliersetc – you’ve got a single collection of suppliers.Basically any time you access
Banana.Suppliersthe compiler emits a call toFruit.Suppliers: that’s why just callingBanana.Suppliersisn’t triggering the static constructor which adds the banana supplier.The reason you only see the suppliers added in the static constructor for bananas after you’ve created a banana is that that forces the static constructor to run. You could do anything else that forced the static initializer to run, and you’d get the same results. One example would be calling a static method within
Bananaitself.Now, I strongly suspect that you’ve got a significant problem in that you’ll be using the same suppliers for all types. Obviously this isn’t your real code, and the best solution will depend on what you want your real code to do. Generics can give you effectively “per type” static variables using type arguments :
Foo<Banana>.StaticPropertyandFoo<Apple>.StaticPropertywill really be different, assumingStaticPropertyis declared inFoo<T>.EDIT: With regards to your edit, I would suggest avoiding using statics here. Possibly create a factory for each type (implementing an interface which may be generic). Note that you may well be able to avoid creating a separate factory type for each subtype, if you can create an appropriate instance with all the relevant items for each type.
We’d really need to see more of an example to say for sure, but in general I find that the less static data you have, the more testable your design will be and the less you’ll run into problems like this 🙂