views:

86

answers:

2

Hi,

I wonder that the obsolete attribute is checked at only runtime?

Think that you have two assemblies. Assembly A uses a method from Assembly B. After that we mark the method in Assembly B as obsolete which causes a compile time error when compiling assembly A.

No problem so far but the question is whether the older assembly A continue to work with new Assembly B or not? Thanks

+6  A: 

Building an assembly that uses a method from another assembly which is marked as Obsolete causes a compile time warning (Unless you have 'display warnings as errors' enabled).

There is nothing stopping you using this method while ever it remains in the referenced assembly. The Obsolete attribute is there as a way for library developers to let the people who use the library know that they should be looking to use a different method to achieve what they need.

To answer your question, yes, an older assembly A will continue to work with a new assembly B. (providing the assembly version remains the same)

Greg B
+1 Beat me to it.
Jeremy Roberts
That is not guaranteed in all cases; see my reply for why...
Marc Gravell
+6  A: 

It is primarily for use at compile-time, but as a side note: some of the runtime handles [Obsolete]; for example, this will only write Foo - Bar is not written:

using System;
using System.Xml.Serialization;
public class Data
{
    public int Foo { get; set; }
    [Obsolete] public int Bar {get;set;}

    static void Main()
    {
        var data = new Data { Foo = 1, Bar = 2 };
        new XmlSerializer(data.GetType()).Serialize(Console.Out, data);
    }
}

(XmlSerializer is a runtime too - not part of the compiler)

So: it depends what you are doing. It might cause problems, even to existing code that is not rebuilt. So we must conclude NO, [Obsolete] is not checked only at compile time.

Marc Gravell
Thanks for your detailed response Marc.
mkus