Search code examples
c#visual-studio-2010obsolete

Obsolete attribute doesn't cause any compiler warning in VS2010


I'm using Visual Studio 2010. But find that the Obsolete attribute doesn't cause any compiler warning (I want it to cause a compiler warning). The warning level is 4.

Thanks.


Solution

  • The obsolete attribute will cause a compiler warning when you try to use the class/method that's marked with it. For example the following causes a warning:

    [Obsolete("some obsolete message")]
    class Foo { }
    
    class Program
    {
        static void Main(string[] args)
        {
            Foo foo = new Foo();
        }
    }
    

    while this doesn't:

    [Obsolete("some obsolete message")]
    class Foo { }
    
    class Program
    {
        static void Main(string[] args)
        { }
    }