以下是 C# 程序员学霸级别的笔记,涵盖了 C# 的核心概念、技术细节以及常见的编程实践。
C# 语言基础
1. 基本语法
-
变量和数据类型:
int
,double
,float
,char
,bool
,string
,var
int age = 30; double salary = 1000.50; char initial = 'A'; bool isActive = true; string name = "John Doe";
-
常量和枚举:
const double Pi = 3.14159; enum Day { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday }
-
控制结构:
if
,else
,switch
,for
,while
,do-while
if (age > 18) { Console.WriteLine("Adult"); } else { Console.WriteLine("Minor"); } for (int i = 0; i < 10; i++) { Console.WriteLine(i); }
2. 面向对象编程 (OOP)
-
类和对象:
public class Person { public string Name { get; set; } public int Age { get; set; } public void Greet() { Console.WriteLine($"Hello, my name is {Name}"); } } Person person = new Person { Name = "Alice", Age = 30 }; person.Greet();
-
继承:
public class Employee : Person { public string Position { get; set; } public void DisplayPosition() { Console.WriteLine($"Position: {Position}"); } } Employee employee = new Employee { Name = "Bob", Age = 40, Position = "Manager" }; employee.Greet(); employee.DisplayPosition();
-
接口和抽象类:
public interface IAnimal { void Speak(); } public abstract class Animal { public abstract void MakeSound(); } public class Dog : Animal, IAnimal { public override void MakeSound() { Console.WriteLine("Bark"); } public void Speak() { Console.WriteLine("Woof"); } }
3. 集合和泛型
-
数组和列表:
int[] numbers = { 1, 2, 3, 4, 5 }; List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
-
字典和集合:
Dictionary<string, int> ages = new Dictionary<string, int> { { "Alice", 30 }, { "Bob", 40 } }; HashSet<string> uniqueNames = new HashSet<string> { "Alice", "Bob", "Alice" };
-
泛型:
public class Stack<T> { private List<T> elements = new List<T>(); public void Push(T item) { elements.Add(item); } public T Pop() { T item = elements[elements.Count - 1]; elements.RemoveAt(elements.Count - 1); return item; } } Stack<int> intStack = new Stack<int>(); intStack.Push(1); intStack.Push(2); int item = intStack.Pop();
4. 异常处理
- 使用
try
,catch
,finally
:try { int result = 10 / 0; } catch (DivideByZeroException ex) { Console.WriteLine("Cannot divide by zero"); } finally { Console.WriteLine("Execution completed"); }
5. LINQ (Language Integrated Query)
-
查询操作:
var numbers = new List<int> { 1, 2, 3, 4, 5 }; var evenNumbers = from number in numbers where number % 2 == 0 select number; foreach (var num in evenNumbers) { Console.WriteLine(num); }
-
方法语法:
var evenNumbers = numbers.Where(n => n % 2 == 0).ToList();
高级特性
1. 异步编程
async
和await
:public async Task<string> FetchDataAsync() { await Task.Delay(2000); // Simulate a delay return "Data fetched"; } public async Task ExecuteAsync() { string result = await FetchDataAsync(); Console.WriteLine(result); }
2. 委托和事件
- 委托:
public delegate void Notify(string message); public class Process { public event Notify OnProcessCompleted; public void StartProcess() { // Process logic here OnProcessCompleted?.Invoke("Process completed"); } }
3. 内存管理和垃圾回收
IDisposable
接口和using
语句:public class Resource : IDisposable { public void Dispose() { // Release resources here } } using (Resource res = new Resource()) { // Use resource }
编程实践
1. 代码风格
- 命名规范: 类名、方法名和变量名应遵循 PascalCase 和 camelCase 命名规则。
- 注释: 使用 XML 注释为公共 API 生成文档。
2. 设计模式
-
单例模式:
public class Singleton { private static readonly Singleton instance = new Singleton(); private Singleton() { } public static Singleton Instance { get { return instance; } } }
-
工厂模式:
public interface IProduct { void Create(); } public class ProductA : IProduct { public void Create() { Console.WriteLine("ProductA created"); } } public class ProductB : IProduct { public void Create() { Console.WriteLine("ProductB created"); } } public class ProductFactory { public static IProduct GetProduct(string type) { switch (type) { case "A": return new ProductA(); case "B": return new ProductB(); default: throw new ArgumentException("Invalid type"); } } }
这些笔记涵盖了 C# 编程的关键概念和技术细节,可以作为深入学习 C# 的基础。