T
Thomas Fritzen
I have a problem with static functions and inheritance as demonstrated by the below samples
using System;
namespace ConsoleApplication1
{
class x
{
public static void Log()
{
Console.WriteLine("x.Log()");
}
public static void DoLog()
{
Log();
}
}
class y : x
{
public static new void Log()
{
Console.WriteLine("y.Log()");
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
y.DoLog();
}
}
}
I was hoping that this program would output "y.log", since I call the "y" inherited static method
but instead it output "x.log", because the DoLog() method is binding to the "local" static
Can I do something to the code in my "x" class to make it call y - while still using static functions?
I do not want to override all the "DoLog"-like functions in my x class.
I know that this would work for non-static functions e.g.:
using System;
namespace ConsoleApplication1
{
class x
{
public virtual void Log()
{
Console.WriteLine("x.Log()");
}
public void DoLog()
{
Log();
}
}
class y : x
{
public override void Log()
{
Console.WriteLine("y.Log()");
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
new y().DoLog();
}
}
}
Rgds,
Thomas Fritzen
using System;
namespace ConsoleApplication1
{
class x
{
public static void Log()
{
Console.WriteLine("x.Log()");
}
public static void DoLog()
{
Log();
}
}
class y : x
{
public static new void Log()
{
Console.WriteLine("y.Log()");
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
y.DoLog();
}
}
}
I was hoping that this program would output "y.log", since I call the "y" inherited static method
but instead it output "x.log", because the DoLog() method is binding to the "local" static
Can I do something to the code in my "x" class to make it call y - while still using static functions?
I do not want to override all the "DoLog"-like functions in my x class.
I know that this would work for non-static functions e.g.:
using System;
namespace ConsoleApplication1
{
class x
{
public virtual void Log()
{
Console.WriteLine("x.Log()");
}
public void DoLog()
{
Log();
}
}
class y : x
{
public override void Log()
{
Console.WriteLine("y.Log()");
}
}
class Class1
{
[STAThread]
static void Main(string[] args)
{
new y().DoLog();
}
}
}
Rgds,
Thomas Fritzen