开发者

How can I mark a specific parameter as obsolete/deprecated in C#?

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 witho开发者_Go百科ut the parameter and mark the original one as Obsolete?


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?


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


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.


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)
    {
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜