I want to inherit that a class has a property, but different implementations of this class will have different values for this property and the property should be available without instantiating an object. Eg: every animal has a value numberOfLegs. For every cat it is 4 for every snake it is 0. Now I want to loop through some animal Types and print out how many legs that animal subclass has without creating an instance of that class.
-
2Static properties do not participate in inheritance. – spender Jul 02 '17 at 19:51
-
check out this SO https://stackoverflow.com/questions/11162652/c-sharp-get-property-value-without-creating-instance – rlcrews Jul 02 '17 at 19:53
-
"I want to loop through ... without creating an instance of that class" is not a real requirement. – H H Jul 02 '17 at 20:09
-
1If the number of legs can change from one animal to another, then that attribute isn't static, because it can change. – Carvo Loco Jul 02 '17 at 20:44
3 Answers
You could give a chance to the following:
First declare your Animal abstract base class which will be responsible for storing types and numberoflegs
abstract class Animal
{
protected readonly static IDictionary<Type, int> _legsDictionary = new Dictionary<Type, int>();
}
And an Animal abstract class which has the static property NumberOfLegs:
abstract class Animal<T> :Animal where T : class
{
public static int NumberOfLegs
{
get => _legsDictionary.ContainsKey(typeof(T)) ? _legsDictionary[typeof(T)] : -1;
set
{
_legsDictionary[typeof(T)] = value;
}
}
}
And then just declare as many Animals as you want >>>
class Cat : Animal<Cat> { }
class Snake : Animal<Snake> { }
class Human : Animal<Human> { }
And testing:
static void Main(string[] args)
{
Cat.NumberOfLegs = 4;
Snake.NumberOfLegs = 0;
Human.NumberOfLegs = 2;
Console.WriteLine(Cat.NumberOfLegs);
Console.WriteLine(Snake.NumberOfLegs);
Console.WriteLine(Human.NumberOfLegs);
Console.ReadLine();
}

- 2,667
- 2
- 18
- 29
-
1This meets the constraint of what was asked but would you ever want to use something like this? – H H Jul 02 '17 at 20:50
-
I must be honest and I will never use something like this =). I am just answering what he asked. I think that if he wanted to ask about its architecture or design then he would have posted (maybe in meta) a question for that. – taquion Jul 02 '17 at 20:54
The thing you want to achieve isn't possible in the way you want it to be.
The static
keyword stays persistent between every class.
What you can do is the following:
class Animal
{
public static int LegCount { get { return 0; } }
}
class Snake : Animal
{
public static new int LegCount { get { return 0; } }
}
class Human : Animal
{
public static new int LegCount { get { return 2; } }
}
class Cat : Animal
{
public static new int LegCount { get { return 4; } }
}
With the new
keyword you can make each new type return a different value.
But note that a List<Animal>
will always always return 0
, since the new
only hides within the type and is not the same as an override
.

- 3,717
- 1
- 22
- 39
-
Adding `new` before a member only suppresses a warning, the fact that this is a very wrong design doesn't change. – H H Jul 02 '17 at 20:49
I guess you could also define your interface requirement (having a numberOfLegs
property) by implementing an interface instead of inheriting from a base class. That way, you can place the static values at the levels of abstraction where they don't change across instances:
interface IAnimal
{
int numberOfLegs { get; }
}
class Snake : IAnimal
{
public static int numberOfLegs = 0;
int IAnimal.numberOfLegs
{
get { return numberOfLegs; }
}
}
class Cat: IAnimal
{
public static int numberOfLegs = 4;
int IAnimal.numberOfLegs
{
get { return numberOfLegs; }
}
}

- 2,068
- 13
- 21
-
2The interface won't work on the types, you still need instance to use this. Side note, with `=>` you only need half the lines. – H H Jul 02 '17 at 21:44
-
Well, the requirement of having a static property that can change as well as enumerating different types of animals without instantiating classes of animals is messy. To be honest, if I arrived to a similar situation myself, I might go back to the drawing board. Thanks for the hints! – Carvo Loco Jul 02 '17 at 22:04