C#进阶入门

devtools/2025/3/5 12:11:53/

当你对 C# 有了基础的了解后,想要进一步提升自己的技能,可以从以下几个方面进行进阶学习:

1. 深入理解面向对象编程

继承与多态

继承允许一个类继承另一个类的属性和方法,而多态则允许你以统一的方式处理不同类型的对象。

// 基类
public class Animal
{public virtual void MakeSound(){Console.WriteLine("动物发出声音");}
}// 派生类
public class Dog : Animal
{public override void MakeSound(){Console.WriteLine("汪汪汪");}
}public class Cat : Animal
{public override void MakeSound(){Console.WriteLine("喵喵喵");}
}class Program
{static void Main(){Animal dog = new Dog();Animal cat = new Cat();dog.MakeSound();cat.MakeSound();}
}
抽象类和接口

抽象类是不能实例化的类,它可以包含抽象方法,要求派生类必须实现这些方法。接口则是一种契约,定义了一组方法签名,实现接口的类必须实现这些方法。

// 抽象类
public abstract class Shape
{public abstract double Area();
}// 实现抽象类
public class Circle : Shape
{public double Radius { get; set; }public Circle(double radius){Radius = radius;}public override double Area(){return Math.PI * Radius * Radius;}
}// 接口
public interface IPrintable
{void Print();
}// 实现接口
public class Document : IPrintable
{public void Print(){Console.WriteLine("打印文档");}
}

2. 泛型编程

泛型允许你创建可重用的组件,这些组件可以处理不同类型的数据,而不需要为每种类型都编写重复的代码。

// 泛型类
public class GenericList<T>
{private T[] items = new T[100];private int count;public void Add(T item){items[count++] = item;}public T Get(int index){return items[index];}
}class Program
{static void Main(){GenericList<int> intList = new GenericList<int>();intList.Add(10);int num = intList.Get(0);GenericList<string> stringList = new GenericList<string>();stringList.Add("Hello");string str = stringList.Get(0);}
}

3. 委托和事件

委托

委托是一种类型,它可以引用一个或多个方法。通过委托,你可以将方法作为参数传递给其他方法。

// 定义委托
public delegate int Calculate(int a, int b);class Program
{public static int Add(int a, int b){return a + b;}public static int Subtract(int a, int b){return a - b;}static void Main(){Calculate calc = Add;int result = calc(5, 3);Console.WriteLine(result);calc = Subtract;result = calc(5, 3);Console.WriteLine(result);}
}
事件

事件是基于委托的一种机制,它允许对象在发生特定事件时通知其他对象。

// 定义事件发布者类
public class Publisher
{// 定义事件public event EventHandler MyEvent;public void RaiseEvent(){// 触发事件MyEvent?.Invoke(this, EventArgs.Empty);}
}// 定义事件订阅者类
public class Subscriber
{public void HandleEvent(object sender, EventArgs e){Console.WriteLine("事件已触发");}
}class Program
{static void Main(){Publisher publisher = new Publisher();Subscriber subscriber = new Subscriber();// 订阅事件publisher.MyEvent += subscriber.HandleEvent;// 触发事件publisher.RaiseEvent();}
}

4. LINQ(语言集成查询)

LINQ 提供了一种统一的语法来查询不同类型的数据,如数组、集合、数据库等。

using System;
using System.Linq;class Program
{static void Main(){int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };// 查询偶数var evenNumbers = from num in numberswhere num % 2 == 0select num;foreach (var num in evenNumbers){Console.WriteLine(num);}}
}

5. 异步编程

在 C# 中,异步编程可以提高应用程序的性能和响应能力,特别是在处理 I/O 密集型操作时。

using System;
using System.Threading.Tasks;class Program
{public static async Task Main(){// 模拟异步操作Task<string> task = GetDataAsync();Console.WriteLine("正在执行其他操作...");string result = await task;Console.WriteLine(result);}public static async Task<string> GetDataAsync(){await Task.Delay(2000);return "数据已获取";}
}

6. 学习设计模式

设计模式是解决软件开发中常见问题的通用解决方案。常见的设计模式包括单例模式、工厂模式、观察者模式等。

// 单例模式
public class Singleton
{private static Singleton instance;private Singleton() { }public static Singleton Instance{get{if (instance == null){instance = new Singleton();}return instance;}}
}class Program
{static void Main(){Singleton singleton1 = Singleton.Instance;Singleton singleton2 = Singleton.Instance;Console.WriteLine(object.ReferenceEquals(singleton1, singleton2)); }
}

http://www.ppmy.cn/devtools/164740.html

相关文章

力扣HOT100之哈希:1. 两数之和

这道题之前刷代码随想录的时候已经刷过好几遍了&#xff0c;看到就直接秒了。这道题主要是通过unordered_map<int, int>来建立哈希表&#xff0c;其中键用来保存向量中的元素&#xff0c;而对应的值则为元素的下标。遍历整个向量&#xff0c;当遍历到nums[i]时&#xff0…

C++蓝桥杯基础篇(七)

片头 嗨~小伙伴们&#xff0c;大家好&#xff01;今天我们来一起学习蓝桥杯基础篇&#xff08;七&#xff09;&#xff0c;学习相关字符串的知识&#xff0c;准备好了吗&#xff1f;咱们开始咯&#xff01; 一、字符与整数的联系——ASCII码 每个常用字符都对应一个-128~127的…

Rust~String、str、str、String、Box<str> 或 Box<str>

Rust语言圣经中定义 str Rust 语言类型大致分为两种&#xff1a;基本类型和标准库类型&#xff0c;前者由语言特性直接提供&#xff0c;后者在标准库中定义 str 是唯一定义在 Rust 语言特性中的字符串&#xff0c;但也是几乎不会用到的字符串类型 str 字符串是 DST 动态大小…

2025-03-04 学习记录--C/C++-C语言 判断是否是素数

合抱之木&#xff0c;生于毫末&#xff1b;九层之台&#xff0c;起于累土&#xff1b;千里之行&#xff0c;始于足下。&#x1f4aa;&#x1f3fb; C语言 判断是否是素数 一、代码 ⭐️ #include <stdio.h> #include <stdbool.h> // 使用 bool 类型// 判断是否是…

Versal - XRT(CPP) 2024.1

目录 1.简介 2. XRT 2.1 XRT vs OpenCL 2.2 Takeways 2.3 XRT C APIs 2.4 Device and XCLBIN 2.5 Buffers 2.5.1 Buffer 创建 2.5.1.1 普通 Buffer 2.5.1.2 特殊 Buffer 2.5.1.3 用户指针 Buffer 2.5.2 Data Transfer 2.5.2.1 read/write API 2.5.2.2 map API 2…

请你说一下你对服务降级的理解

服务降级是指在服务出现故障或资源不足的情况下&#xff0c;通过牺牲部分功能或性能&#xff0c;以确保核心功能或服务的可用性。其主要目的是在系统面临压力或故障时&#xff0c;仍然能够提供基本的服务&#xff0c;而不是完全失败。 服务降级的核心思想 保证核心功能&#x…

RMSNorm模块

目录 代码代码解释1. 初始化方法 __init__2. 前向传播方法 forward3. 总结4. 使用场景 可视化 代码 class RMSNorm(torch.nn.Module):def __init__(self, dim: int, eps: float):super().__init__()self.eps epsself.weight nn.Parameter(torch.ones(dim))def forward(self,…

合理规划时间,从容应对水利水电安全员考试

合理规划时间&#xff0c;从容应对水利水电安全员考试 在忙碌的工作与生活节奏中备考水利水电安全员考试&#xff0c;合理规划时间是实现高效备考的核心。科学的时间管理能让你充分利用每一分每一秒&#xff0c;稳步迈向考试成功。 制定详细的学习计划是第一步。依据考试时间…