Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this code compile without error even though the class is marked Obsoleted?

This is Visual Studio 2008. Obviously has to do with the static class for an extensions.

public class Dummy
{
    public readonly int x;

    public Dummy(int x)
    {
        this.x = x;
    }

    public override string ToString()
    {
        return x.ToString();
    }
}

[Obsolete("Do Not Use", true)]
public static class Extensions
{
    public static int Squared(this Dummy Dummy)
    {
        return Dummy.x * Dummy.x;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var d = new Dummy(42);
        Console.WriteLine(String.Format("{0}^2={1}", d, d.Squared()));
    }
}
like image 490
Cade Roux Avatar asked May 10 '11 20:05

Cade Roux


2 Answers

That repros in VS2010 as well. Looks like a bug. I'll get it entered in the database.

You can work around the bug by putting the attribute on the actual method.

Thanks for the report!

like image 70
Eric Lippert Avatar answered Sep 21 '22 20:09

Eric Lippert


Calling an obsolete function is a warning, not an error, unless you change the compiler settings to stop on warnings too - to make warnings behave like errors.

Typically I don't see those warnings unless there are other 'real' errors in my code.

Also notice that in your specific case you marked the class as obsolete - not the method. That might matter.

like image 33
n8wrl Avatar answered Sep 22 '22 20:09

n8wrl