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
"Aamir Mahmood" <a@b.c> wrote in message
news:OP**************@TK2MSFTNGP14.phx.gbl...
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