views:

126

answers:

5

I would like to be able to keep a C# API the same as it is now, but simply deprecate one of the parameters in a method call. Is it possible to do so, or do I need to create a new method without the parameter and mark the original one as Obsolete?

+6  A: 

Yes. You can only mark types and members of types as obsolete, not individual parameters. You could, however, overload the method, thereby keeping the same name and mark the old one as obsolete:

class Act
{
    [Obsolete("DoSomething(int, int) is obsolete", false /*warn only*/)]
    public void DoSomething(int i, int j)
    {
    }

    public void DoSomething(int i)
    {
    }
}
Chris Wallis
+2  A: 

I think it would be best to create a new method.

What do you mean by making the parameter obsolete? Does this depend on a specific value for that argument? When you call a method, you always have to specify the argument anyway.

Pieter
+7  A: 

Yes, I think the only way is to create a new method without the parameter and mark the original one with ObsoleteAttribute.

Andrew Bezzub
+1 Absolutely! =)
Will Marcouiller
+8  A: 

With the Obsolete attribute:

[Obsolete("Please use void AnotherMethod() instead", false)]
void SomeMethod(Int32 data){
}

The Boolean in the attribute tells the compiler to generate a warning, if you change it to true the compiler will generate an error. See here for the documentation on it.

Steve Ellinger
+1 That is the best way to go! =)
Will Marcouiller
Is it possible to do this without marking the whole method as obsolete, though?
Brian
No, as the other answers (such as Chris Wallis) state, Parameter is not a target for the ObsoleteAttribute.
Steve Ellinger
+5  A: 

Short answer:

You will need to create a new nethod with the new signature, and mark the current as obsolete.

Longer answer

What you want to avoid at all cost is a code break! Then, particularly in a company framework, you want to advertise that your method will no longer be supported, for example, but you do not want to be responsible for depending solutions to crash because of an architecture or design decision or your side, right?

The ObsoleteAttribute class will do the trick for you.

Once a class member marked as obsolete, a warning will be raised on the client-side, the ones who use your framework, to continue that way, or even one of your colleague under the same project.

public class MyClass {
    [Obsolete("This method should no longer be used, please use MyNewMethod() instead.")]
    public void MyMethod(string name, long phoneNumber, long faxNumber) { 
    }

    public void MyNewMethod(string name, long phoneNumber, long faxNumber, string email) { 
    }
}

This will advertise that MyMethod is no longer supported throughout your code users.

After a certain period of time, reasonable enough to allow everyone to change his/her code, you may tell this attribute to throw an error message when your obsolete method is still used in the code.

public class MyClass {
    [Obsolete("This method should no longer be used, please use MyNewMethod() instead.", true)]
    public void MyMethod(string name, long phoneNumber, long faxNumber) { 
    }

    public void MyNewMethod(string name, long phoneNumber, long faxNumber, string email) { 
    }
}

By setting the second ObsoleteAttribute class constructor parameter to true, you tel the compiler to advertise the use of this method as an error.

After some time only, you can completely remove your method from your code to clean it up a little. This is part of the refactoring methods encouraged by the Agile Software Development methodology.

Does this help?

Will Marcouiller
+1 for a more compete answer and spelling out the steps. More informative than Steves answer
Gorgen