C#高级教程

以下是按照索引和目录形式整理的C#高级特性注释版示例:

目录

  1. C# 特性(Attribute)
  2. C# 反射(Reflection)
  3. C# 属性(Property)
  4. C# 索引器(Indexer)
  5. C# 委托(Delegate)
  6. C# 事件(Event)
  7. C# 集合(Collection)
  8. C# 泛型(Generic)
  9. C# 匿名方法
  10. C# 不安全代码
  11. C# 多线程

1. C# 特性(Attribute)

csharp 复制代码
// 定义一个自定义特性
[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)

csharp 复制代码
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)

csharp 复制代码
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)

csharp 复制代码
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)

csharp 复制代码
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)

csharp 复制代码
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)

csharp 复制代码
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)

csharp 复制代码
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# 匿名方法

csharp 复制代码
using System;

class AnonymousMethodExample
{
    static void Main(string[] args)
    {
        // 委托
        Action<string> action = delegate(string message) { Console.WriteLine(message); };

        // 调用委托
        action("Hello, Anonymous Method!");
    }
}

10. C# 不安全代码

csharp 复制代码
using System;

class UnsafeCodeExample
{
    static void Main()
    {
        // 不安全代码块
        unsafe
        {
            int number = 0;
            int* p = &number;

            // 修改指针指向的值
            *p = 1234;

            Console.WriteLine(number);
        }
    }
}

11. C# 多线程

csharp 复制代码
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#高级特性的基本用法和详细注释,帮助您理解每个特性的工作原理和应用场景。希望这些示例对您有所帮助!

相关推荐
2501_930707783 分钟前
使用 C# .NET 从 PowerPoint 演示文稿中提取背景图片
c#·powerpoint·.net
0思必得013 分钟前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
Misnice16 分钟前
Webpack、Vite、Rsbuild区别
前端·webpack·node.js
青茶36017 分钟前
php怎么实现订单接口状态轮询(二)
前端·php·接口
初级代码游戏33 分钟前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
大橙子额1 小时前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
大空大地20262 小时前
流程控制语句--switch多分支语句使用、while循环语句的使用、do...while语句、for循环
c#
爱喝白开水a3 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
董世昌413 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
chian-ocean3 小时前
百万级图文检索实战:`ops-transformer` + 向量数据库构建语义搜索引擎
数据库·搜索引擎·transformer