inherit static members

T

Tony Johansson

Hello!

I know that it's not possible to inherit static members but is this only a
decision that was made by microsoft.
Would it have been possible if microsoft had decided to support static
inheritance.
So would it have been any point to support inheritane for static members ?

//Tony
 
F

Faisal

Hello!

I know that it's not possible to inherit static members but is this only a
decision that was made by microsoft.
Would it have been possible if microsoft had decided to support static
inheritance.
So would it have been any point to support inheritane for static members ?

//Tony

You can inherit the static members
For eg:

class MyBase
{
public static int x = 25;
public static void Method()
{
Console.WriteLine("Base static method");
}
}
class MyClass : MyBase
{
}
class MyClient
{
public static void Main()
{
MyClass.Method(); // Displays 'Base static method'
Console.WriteLine(MyClass.x);// Displays 25
}
}

But a static member in C# can't be marked as override, virtual or
abstract. However it is possible to hide a base class static method in
a derived class by using the keyword new.

In order to override a method, there must be a type instance
associated with it. For accessing static members we don't create any
instance. The whole virtual function mechanism works based on
instances. Read about C++ virtual functions, you will get better idea
how things works.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top