超硬核!C#学霸笔记,考试/面试随便秒杀,也可以作为深入学习 C# 的基础

以下是 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. 异步编程
  • asyncawait:
    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# 的基础。

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Python学习笔记的问题,我找到了两个引用内容相关的资料。引用是一个Python学习笔记合集,共有26天的学习内容,包括Python基本语句、导包、条件语句、循环语句、数据类型、日期、函数、文件I/O、面向对象等多个方面的知识点。引用是一份由大神整理的Python学习笔记,共有137页的内容,将核心知识点统筹在一个章节里面,方便新手入门。这份笔记具有条理性和提炼性,适合帮助大家学习和理解Python的难点。你可以通过微信扫描相应二维码免费获取完整的学习笔记。希望这些资料可以帮助你更好地学习Python编程。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Python学习笔记合集(总结)](https://blog.csdn.net/qq_54129105/article/details/127954575)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [世界顶级整理!137页Python学习笔记,全面总结看这一篇就够了](https://blog.csdn.net/m0_74942241/article/details/127900214)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [硬核!数据结构学霸笔记考试面试吹牛就靠它](https://blog.csdn.net/hebtu666/article/details/115233244)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值