以下是按照索引和目录形式整理的C#高级特性注释版示例:
目录
- C# 特性(Attribute)
- C# 反射(Reflection)
- C# 属性(Property)
- C# 索引器(Indexer)
- C# 委托(Delegate)
- C# 事件(Event)
- C# 集合(Collection)
- C# 泛型(Generic)
- C# 匿名方法
- C# 不安全代码
- C# 多线程
1. C# 特性(Attribute)
// 定义一个自定义特性
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public class MyCustomAttribute : Attribute
{public string Name { get; set; }public MyCustomAttribute(string name){Name = name;}
}// 应用特性
[MyCustomAttribute("Hello")]
class Program
{static void Main(string[] args){// 反射获取特性信息var attributes = typeof(Program).GetCustomAttributes(false);foreach (var attribute in attributes){var customAttribute = (MyCustomAttribute)attribute;Console.WriteLine($"Attribute Name: {customAttribute.Name}");}}
}
2. C# 反射(Reflection)
using System;
using System.Reflection;class ReflectionExample
{public void Display(){Console.WriteLine("Reflection Example");}static void Main(string[] args){// 获取类型信息Type type = typeof(ReflectionExample);// 创建类型实例object obj = Activator.CreateInstance(type);// 获取方法信息MethodInfo methodInfo = type.GetMethod("Display");// 调用方法methodInfo.Invoke(obj, null);}
}
3. C# 属性(Property)
class PropertyExample
{// 私有字段private string _name;// 公共属性public string Name{get { return _name; }set { _name = value; }}static void Main(string[] args){var obj = new PropertyExample();// 使用属性obj.Name = "Kimi";Console.WriteLine(obj.Name);}
}
4. C# 索引器(Indexer)
class IndexerExample
{private int[] _array = new int[5];public int this[int index]{get { return _array[index]; }set { _array[index] = value; }}static void Main(string[] args){var obj = new IndexerExample();// 使用索引器obj[0] = 10;Console.WriteLine(obj[0]);}
}
5. C# 委托(Delegate)
using System;class DelegateExample
{// 定义委托public delegate void DisplayDelegate(string message);// 方法public void Display(string message){Console.WriteLine(message);}static void Main(string[] args){var obj = new DelegateExample();// 创建委托实例DelegateExample.DisplayDelegate del = obj.Display;// 委托调用方法del("Hello, Delegate!");}
}
6. C# 事件(Event)
using System;class EventExample
{// 定义事件public event EventHandler OnEventOccurred;// 引发事件的方法protected virtual void OnEvent(){OnEventOccurred?.Invoke(this, EventArgs.Empty);}static void Main(string[] args){var obj = new EventExample();// 订阅事件obj.OnEventOccurred += (sender, e) =>{Console.WriteLine("Event occurred!");};// 引发事件obj.OnEvent();}
}
7. C# 集合(Collection)
using System;
using System.Collections.Generic;class CollectionExample
{static void Main(string[] args){// 创建集合List<int> list = new List<int> { 1, 2, 3, 4, 5 };// 添加元素list.Add(6);// 遍历集合foreach (var item in list){Console.WriteLine(item);}}
}
8. C# 泛型(Generic)
using System;
using System.Collections.Generic;class GenericExample<T>
{private T _value;public T Value{get { return _value; }set { _value = value; }}static void Main(string[] args){var genericObj = new GenericExample<int>();// 设置值genericObj.Value = 10;// 获取值Console.WriteLine(genericObj.Value);}
}
9. C# 匿名方法
using System;class AnonymousMethodExample
{static void Main(string[] args){// 委托Action<string> action = delegate(string message) { Console.WriteLine(message); };// 调用委托action("Hello, Anonymous Method!");}
}
10. C# 不安全代码
using System;class UnsafeCodeExample
{static void Main(){// 不安全代码块unsafe{int number = 0;int* p = &number;// 修改指针指向的值*p = 1234;Console.WriteLine(number);}}
}
11. C# 多线程
using System;
using System.Threading;class ThreadExample
{static void ThreadMethod(){for (int i = 0; i < 10; i++){Console.WriteLine("Thread: " + i);Thread.Sleep(1000);}}static void Main(string[] args){// 创建线程Thread thread = new Thread(new ThreadStart(ThreadMethod));// 启动线程thread.Start();for (int i = 0; i < 5; i++){Console.WriteLine("Main Thread: " + i);Thread.Sleep(500);}thread.Join(); // 等待线程结束}
}
这个索引和目录形式的整理提供了C#高级特性的基本用法和详细注释,帮助您理解每个特性的工作原理和应用场景。希望这些示例对您有所帮助!