C #: how to make the method obsolete and how to reject the Method,
When using. Net Frameworkd for development, you can often see a method [Rejected] On the ToolTip smart prompt of the method ],
Or in the warning:
Even the [Rejected] method may cause compilation errors:
I believe that everyone has encountered these problems in the daily development process.
Why should we reject a method? This method may be inefficient, may cause memory leakage, or poor compatibility.
So how to reject a method? I think many of you know that using the Obsolete feature
I'm not happy to say that I only know how to reject a method till now, so I recorded it with my blog so that I would not forget it after a long time.
Next, let's take a look at how the Obsolete feature is used.
1. Suppose there is such a class:
using System;namespace Test{ class Program { static void Main(string[] args) { } static void OldPrintHelloWorld() { Console.Write("Hello "); Console.Write("World"); Console.Write("\n"); } static void NewPrintHelloWorld() { Console.WriteLine("Hello World"); } }}
2. for various reasons, we want to reject the OldPrintHelloWorld method, so that the OldPrintHelloWorld method can be used as little as possible in the project or not allowed.
3. After we add the Obsolete feature to OldPrintHelloWorld, the compiler will give a warning:
4. the Obsolete feature has three reconstructed constructors. You can go to http://msdn.microsoft.com/zh-cn/library/system.obsoleteattribute (v = vs.110). aspx for reference.
This document can be reproduced, but please attach a link to the original article.
Link: http://www.zhoumy.cn