Interface method marked as Obsolete does not issue a message from the compiler when implemented

c#, compiler-construction

Solution

You've only marked IAnimal.Eat as obsolete, not Animal.Eat. The var keyword resolves to Animal, and so when you call animal.Eat, you're not calling into any method marked as Obsolete.

To fix, either explicitly change var to IAnimal, or better still, mark Animal.Eat as obsolete as well:

    public interface IAnimal
    {
        [Obsolete("Animals can't eat anymore", true)]
        void Eat();
    }

    public class Animal : IAnimal
    {
        [Obsolete("Animals can't eat anymore", true)]
        public void Eat()
        {
            Console.WriteLine("Hello");
        }
    }

Problem

Consider this example ``` public interface IAnimal { [Obsolete("Animals can't eat anymore", true)] void Eat(); } public class Animal : IAnimal { public void Eat() { Console.WriteLine("Hello"); } } ``` I have an interface IAnimal with an obsoleted method. The Class Animal implements that interface. Later on, i call the Eat method as such: ``` var animal = new Animal(); animal.Eat(); ``` The compiler does not fail to compile (i have Obsolete marked to give an error instead of an warning). The program compiles and the method is invoked with no errors, as well. As far as i can see this is a bug from the compiler. Am i missing anything? Note: i am using VS2010

Original source