I have a problem which Im having problem with and not sure whether Its doable in C# or not.
I have a base class with a static variable, and I want 2 of its conrecte class to have “their own” view of that static variable, so all the instances of Concrete1 will share the same static variable of Concrete 1,
but all the instances of Concrete2 will share Concrete2 static variable?
Currently im definding the same code in both concrete classes, but is there a way i could define the static on base class and then get the above result?
If Mammal had a static variable that is either public or protected, then Cat and Horse both have access to that static variable. Cat and Horse and Dog and Monkey can all declare their own static variables which are only visible to their respective classes.
There is a ‘new’ keyword which would behave somewhat similar to what you propose, but I don’t recommend it. If Mammal had a static variable, then Human could say it has a ‘new’ static variable reusing the base class’s name. Human and its subclasses would not access or modify the base Mammal version of the variable without special care. The problem is it’s really easy to accidentally access the wrong one, so it’s a bug magnet. I feel it’s meant as a stop-gap to work around conflicts later in a mature/legacy stage of an overengineered crusty codebase, and not as a feature you want to go basing intentional functionality upon.
In your case, it sounds like you might need to compose a feature in, rather than inherit. Lots of animals have liver function, even if they don’t all inherit it from a common ancestor. Create a specialized class which manages JUST the pseudo-shared capability, and then let them all inherit or implement an interface to use it.
There is a bit of a hack that you can do with generics.
public class Mammal
{
protected class MyInheritableStatic<T>
{
public static int x = 5;
}
}
public class Human : Mammal
{
void SomeFunc()
{
MyInheritableStatic<Human>().x += 1;
}
}
public class Cow : Mammal
{
void SomeFunc()
{
MyInheritableStatic<Cow>().x -= 1;
}
}
While something like this may satisfy the question asked, in practice it’s not very useful. This is the kind of thing that leads to confusing code and unique code structure problems.
For example, would “ class Child : Human” want to modify the static var or the static var.
Side note you will probably want to add a restriction to the generic to only accept classes of type Mammal.
protected class MyInheritableStatic<T> where T : Mammal
{
public static int x = 0;
}
You could just make a virtual/abstract property and override that in child classes.
public abstract Animal
{
public abstract int LegCount { get; }
}
public abstract Horse : Animal
{
public override int LegCount => 4;
}
public abstract Spider : Animal
{
public override int LegCount => 8;
}
Not that you should generally code like this. As noted above, composition over inheritance.
Otherwise this feels like an XY problem. What’s the actual problem your trying to solve here?
Also, this generic line you have to use every time essentially creates a new concrete type each time you use a different generic argument. Just that this type is compiler / runtime generated, but you still have to actually write it out explicitly. I also don’t see the benefit here
If the goal is to have an actual polymorphic static field across different concrete child classes, you can simply do
public abstract class Base
{
public abstract int MyValue { get; set; }
}
public class SomeChild : Base
{
public static int TheActualStatic;
public override int MyValue
{
get => TheActualStatic;
set => TheActualStatic = value;
}
}
Now each child class would simply have its own static field but you can also access it polymorphically through the MyValue property.
This whole thread looks like you want invent your own instances system even if instances are already invented just with no static variables. This really doesn’t make any sense for me.