C #: how to make the method obsolete and how to reject the Method,

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.