zoukankan      html  css  js  c++  java
  • c#特性

    c#当中的特性分为系统特性和自定义特性,就现在我们学习两种系统特性。

    特性一:Obsolete,检查方法是否过时。当然在其中的方法重载中,默认是不报错的,最后的参数可以自己选择。

    using System.Reflection;
    using System.Diagnostics;
    
    namespace 特性
    {
        class Program
        {
            [Obsolete("这是一个过时的方法,请检查",true)]
            public void OldMethod()
            {
                Console.WriteLine("这是一个旧方法");
            }
    
            public void NewMethod()
            {
                Console.WriteLine("这是一个新方法");
            }
    
            public void Test()
            {
                OldMethod();
                NewMethod();
            }
    
            static void Main(string[] args)
            {
                Program obj = new Program();
                obj.Test();
            }
        }
    }

    特性二:Conditional,实现条件编译,阻止一个方法的进行,比如测试方法。

    #define 测试
    #define debug
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    using System.Diagnostics;
    using System.Reflection;
    
    namespace 特性
    {
        class Demo1
        {
            [Conditional("测试")]
            public void TestMethod()
            {
                Console.WriteLine("这是一个测试方法");
            }
    
            public void Method1()
            {
                Console.WriteLine("这是方法1");
            }
    
            public void Method2()
            {
                Console.WriteLine("这是方法2");
            }
    
            public void Test()
            {
    #if debug
                TestMethod();
    #else
                Method1();
    #endif
                Method1();
            }
    
            static void Main(string[] args)
            {
                Demo1 obj = new Demo1();
                obj.Test();
            }
        }
    }

    当然实现条件编译还有使用“预编译”指令,

    #if debug

    #else

    #endif

    当然不管是Conditional特性,还是预编译指令,都可以选择某个方法是否可以执行,也都需要宏定义去实现。

    有梦的时候还知道去哪,别轻易丢掉,加油吧!

  • 相关阅读:
    104.Maximum Depth of Binary Tree
    103.Binary Tree Zigzag Level Order Traversal
    102.Binary Tree Level Order Traversal
    101.Symmetric Tree
    100.Same Tree
    99.Recover Binary Search Tree
    98.Validate Binary Search Tree
    97.Interleaving String
    static静态初始化块
    serialVersionUID作用
  • 原文地址:https://www.cnblogs.com/Optimism/p/10485439.html
Copyright © 2011-2022 走看看