9

Note: I already checked msdn, it doesn't address my actual question, see below.

I'm trying to use the obsolete attribute on a (obviously obsolete) constructor in one of my classes. Here's the scenario:

I want to be able to force the developer to update to the new constructor without affecting already existing and deployed code. This way I can deploy my code to production just fine, but from a developers perspective, whenever they go into their code, instead of just getting a "warning" which I'm sure they'll just ignore, I want them to get a compile error because the status quo is no longer ok.

So my question is, will this affect only developers, or all calling apps, or do I have the whole thing wrong?

sample code:

public class MyClass
{
   private string _userID; //new code

   [Obsolete("This constructor is obsolete, please use other constructor.", true)]
   public MyClass()
   {
      _userID = ""; //defaulting to empty string for all those using this constructor
   }

   public MyClass(string userID)
   {
      _userID = userID; //this is why they need to use this constructor
   }
}

Any and all help will be appreciated, thanks in advance!

IWriteApps
  • 973
  • 1
  • 13
  • 30

4 Answers4

11

Yes, this primarily affects the compiler - any pre-built code won't be affected... unless that code explicitly checks for this attribute. For example, some serialization code (XmlSerializer, IIRC) checks for this - so it might not be entirely side-effect free... but in principal existing code won't usually be affected until they try to compile next.

Of course, if you are using this code from something that uses dynamic compilation (for example ASP.NET without pre-compile) then all bets are off.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
3

The attribute is only an instruction to the compiler. Already existing binaries can still use the constructor.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
2

So my question is, will this affect only developers, or all calling apps, or do I have the whole thing wrong?

This will only be used at compile time, by the compiler. It will not affect applications which have already been deployed.

As such, this will have the behavior you are trying to accomplish.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
2

This is what [Obsolete] already does, no extra help is needed. It is not a compile time warning, it generates an error:

error CS0619: 'ConsoleApplication1.MyClass.MyClass()' is obsolete: 'This constructor is obsolete, please use other constructor.'

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • I know from this very class the question is regarding that simply putting obsolete only gives the user a warning. If you use the overloaded attribute to make the error display then error shows up, I just want to make sure this doesn't affect our users using pre-existing code. – IWriteApps May 30 '12 at 17:41
  • To be precise, only when the second parameter to the ObsoleteAttribute is true, the compiler will emit an error. Otherwise and by default, the compiler will only emit a warning (unless you also compile with warnings-as-errors enabled). – Christian.K May 30 '12 at 17:44
  • 1
    It isn't a warning when you use the constructor overload that takes the `bool error` argument and you pass `true`. Which is what you do. No existing code is affected, only the compiler checks for the attribute. – Hans Passant May 30 '12 at 17:45