Overriding static members

A

Aamir Mahmood

Hi all,

I have a question. Can static members (methods and nested types) be made
virtual in a class? So that they can be overridden in the child classes.

For example:
-------------------
public class Parent {
public enum AllowedColors = {Red, Blue, Green}
public static void DoSomething() {
}
}

public class Child : Parent{
public new enum AllowedColors = {Black, Blue, Yellow}
public new static void DoSomething() {
}
}
-------------------

Is it allowed in Csharp?

Regards,

-
Aamir
 
D

Devi JV [MSFT]

Yes, of course; Your code sample is allowed in C#. Although a static member
in C# can't be marked as override, virtual or abstract, it is possible to
hide a base class static method in a derived class by using the keyword new.
The following example might help for a better understanding.

class MyBase{ public static int x = 25; public static void Method()
{ Console.WriteLine("Base static method"); }}class MyDerived :
MyBase{ public new static int x = 50; public new static void Method()
{ Console.WriteLine("Derived static method"); }}class
MyClient{ public static void Main() { MyDerived.Method(); //
Displays 'Derived static method' Console.WriteLine(MyClass.x);//
Displays 50 }}
- Devi J V [MSFT]--
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


You cannot make them virtual.

You can use the code you posted though.

Now depending of what you want to do the code work or not, remember that the
statics method can be called only from the type itself, not an instance.
This mean that there is no way for a client to confusion or to dynamically
select which one to call.

The only purpose I can think of right now of having such a construction is
to allow a derived type to have a method with the same signature.

cheers,
 
T

Telmo Sampaio

Aamir,

Static members do not work with polymorphism. Therefore, they cannot be
marked as virtual/override. Remember that polymorphism will guarantee that
the correct method is called based on the object we are accessing, not the
variable type we are accessing it from. Hence, polymorphism is used for
instance members, not static members.

However, you can hide the members on your derived classes using the new
keyword as you demonstrated in your code.

Telmo Sampaio
MCT
 
B

Bruce Wood

At first I was skeptical when I read this, but I tried it. The C#
compiler does indeed require the keyword "new" on static a method in a
derived class that have the same name signature as a static method in
the base class.

This makes absolutely no sense to me. Does anyone know why C# would
require the keyword "new" here? It seems useless to me.

Static methods are associated with classes, not instances, and are
called by using the class name, not an instance variable. There is no
polymorphism involved at all.

So what good does the "new" keyword do, when you already know that in
order to call the method you have to give the class name, so there can
be no confusion?
 
A

Alexander Shirshov

I guess "new" is required to give the subclass *author* a sense that he's
doing something wrong. Or better, something not desirable.
 

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