C#学习笔记(三)

本文详细介绍了C#中的特性(Attribute),包括ConditionalAttribute、Obsolete和自定义特性,并探讨了反射(Reflection)的作用,如查看元数据、延时绑定等。同时,也提及了索引器(Indexer)的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

特性Attribute

特性是用于运行时,传递程序中的各种元素的行为信息的声明性标签,通过放置在它所使用的元素的方括号[]来进行描述。

特性用于添加元数据,如编译器指令和注释、描述、方法、类等

ConditionalAttribute

ConditionalAttribute是用来做判断条件的,只有满足了相应的条件,程序才会执行,必须出现在相应的代码前面,当有多个Attribute的时候,只需要依次加上([])即可,不需要在乎顺序,Attribute实际上是一种比较特殊的实例化的类,这种操作类似于C语言里面的宏。

  • 条件属性必须是类声明或者结构声明中使用,如果用在接口中声明会报错。
  • 条件方法必须有返回类型。

写一段简单的测试代码来使用ConditionalAttribute:

#define Triangle
using System;
using System.Diagnostics;

namespace AttributeApplication
{
    class AttributeTest
    {
        [Conditional("Triangle")]
        public static void getTriangle()
        {
            Console.WriteLine("15");
        }

        [Conditional("Circle")]
        public static void getCircle()
        {
            Console.WriteLine("Circle");
        }

        static void Main(String[] args)
        {
            getTriangle();
            getCircle();
        }
    }
}

上述代码的执行结果如下所示:

img1

可见只有定义了相应的宏才能够执行相应的代码。

obsolete

obsolete特性的方法会被标志为是过时的,程序会显式相应的警告信息,自己写的一份学习代码如下所示:

img16

可以发现出现了相应的警告条,如果想要使用Obsolete的方法,可以在Main函数的前面加上Obsolete,如下所示:

img17

Self-define

#define Triangle
using System;
using System.Diagnostics;

namespace AttributeApplication
{
    [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited =true)]
    public class developerInfo : Attribute
    {
        private string name;
        private string date;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Date
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
            }
        }

        public developerInfo(string _name, string _date)
        {
            this.name = _name;
            this.date = _date;
        }
    }


    [developerInfo("Alva", "2019-07-18")]
    class Test
    {

    }


    class AttributeTest
    {
        static void Main(string[] args)
        {
            Test test = new Test();
            Type t = typeof(Test);
            foreach (developerInfo attribute in t.GetCustomAttributes(false))
            {
                Console.WriteLine("Name: {0}", attribute.Name);
                Console.WriteLine("Date: {0}", attribute.Date);
            }    
        }
    }
}

反射Reflection

反射的用途如下:

  • 允许在运行时查看特性信息
  • 允许审查集合中的各种类型
  • 允许延时绑定的方法和属性
  • 允许运行时创建新类型

索引器Indexer

using System;

namespace indexApplication
{
    class indexApp
    {
        private string[] name = new string[100];

        public string this[int index]
        {
            get
            {
                if (index >= 0 && index < 100)
                {
                    return name[index];
                }
                else
                {
                    return "";
                }
            }
            set
            {
                if (index >= 0 && index < 100)
                {
                    name[index] = value;
                }
            }
        }
    }

    class indexExecution
    {
        static void Main(string[] args)
        {
            indexApp t = new indexApp();
            t[0] = "Sam";
            t[1] = "Susan";
            t[2] = "Alva";

            Console.WriteLine(t[0]);
            Console.WriteLine(t[1]);
            Console.WriteLine(t[2]);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值