S
Steve James
I am trying to mark an override method in a derived class as obsolete using
the ObsoleteAttribute. The compiler, however is not picking up this
attribute and is not generating a warning or an error.
Sample code is below:
If I put the attribute on the static method DoSomething() in the main class,
a warning/error is generated by the compiler.
If I put the attribute on the virtual instance method DoSomething() of
Class2, a warning/error is generated.
If I put the attribute on the override instance method DoSomething() of
Class3 ( which is derived from Class2), no warning or error is generated by
the compiler.
Is this a bug, or is there some good reason for this? It certainly isn't
documented. Any ideas?
using System;
namespace ConsoleApplication
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Class2 o = new Class2();
o.DoSomething();
o = new Class3();
o.DoSomething();
DoSomething();
Console.ReadLine();
}
[Obsolete("Function 'DoSomething()' is obsolete.", true )]
static void DoSomething()
{
Console.WriteLine( "I've done something.");
}
}
public class Class2
{
public Class2() {}
public virtual void DoSomething()
{
Console.WriteLine("Class2.DoSomething()");
}
}
public class Class3 : Class2
{
public Class3(){}
[Obsolete("Use somethiing else instead.", true)]
public override void DoSomething()
{
Console.WriteLine("Class3.DoSomething()");
}
}
}
Thanks,
Steve
the ObsoleteAttribute. The compiler, however is not picking up this
attribute and is not generating a warning or an error.
Sample code is below:
If I put the attribute on the static method DoSomething() in the main class,
a warning/error is generated by the compiler.
If I put the attribute on the virtual instance method DoSomething() of
Class2, a warning/error is generated.
If I put the attribute on the override instance method DoSomething() of
Class3 ( which is derived from Class2), no warning or error is generated by
the compiler.
Is this a bug, or is there some good reason for this? It certainly isn't
documented. Any ideas?
using System;
namespace ConsoleApplication
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Class2 o = new Class2();
o.DoSomething();
o = new Class3();
o.DoSomething();
DoSomething();
Console.ReadLine();
}
[Obsolete("Function 'DoSomething()' is obsolete.", true )]
static void DoSomething()
{
Console.WriteLine( "I've done something.");
}
}
public class Class2
{
public Class2() {}
public virtual void DoSomething()
{
Console.WriteLine("Class2.DoSomething()");
}
}
public class Class3 : Class2
{
public Class3(){}
[Obsolete("Use somethiing else instead.", true)]
public override void DoSomething()
{
Console.WriteLine("Class3.DoSomething()");
}
}
}
Thanks,
Steve