Initial commit: C# 学习笔记和示例代码

- Lesson 01-10: C# 基础语法
- WebView2: 集成示例
- notes/: 详细笔记
This commit is contained in:
2026-07-01 16:31:35 +08:00
commit f604d53191
51 changed files with 2745 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
using System;
namespace Lesson05.Helpers
{
static class MathHelper
{
public static int Add(int a, int b)
{
return a + b;
}
public static int Multiply(int a, int b)
{
return a * b;
}
public static double Divide(int a, int b)
{
if (b == 0)
{
Console.WriteLine("除数不能为0!");
return 0;
}
return (double)a / b;
}
}
}
+10
View File
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+46
View File
@@ -0,0 +1,46 @@
using System;
namespace Lesson05.Models
{
class Person
{
// public: 任何地方都能访问
public string name;
// private: 只有本类内部能访问
private int age;
private string secret = "秘密信息";
// Property: 受保护的访问方式
public int Age
{
get { return age; }
set
{
// value 是关键字,表示赋值时的右值
if (value < 0) value = 0;
if (value > 150) value = 150;
age = value;
}
}
// 构造函数
public Person(string name, int age)
{
this.name = name;
this.Age = age; // 通过 Property 设置,自动限制范围
}
public void SayHi()
{
Console.WriteLine($"我是 {name},今年 {age} 岁");
Console.WriteLine($"我的秘密: {GetSecret()}"); // 本类内可以调用私有方法
}
// 私有方法:本类内部使用
private string GetSecret()
{
return secret;
}
}
}
+42
View File
@@ -0,0 +1,42 @@
using System;
using Lesson05.Models;
using Lesson05.Services;
using Lesson05.Helpers;
namespace Lesson05
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== 多文件项目演示 ===\n");
// 使用 Models 里的类
Person p = new Person("无言势", 25);
p.SayHi();
// 使用 Property
p.Age = -5; // 会自动限制范围
Console.WriteLine($"设置后年龄: {p.Age}");
p.Age = 200;
Console.WriteLine($"设置后年龄: {p.Age}");
// 使用 Services
AudioService service = new AudioService();
service.StartListening();
service.StopListening();
// 使用 Helpers
int sum = MathHelper.Add(100, 200);
Console.WriteLine($"\n100 + 200 = {sum}");
// 演示 private 无法访问
// p.secret = "xxx"; // 编译错误
// p.SetSecret("xxx"); // 编译错误
Console.WriteLine("\n=== 演示完成 ===");
Console.ReadLine();
}
}
}
+39
View File
@@ -0,0 +1,39 @@
using System;
namespace Lesson05.Services
{
class AudioService
{
private bool isListening;
private string deviceName;
public AudioService()
{
this.deviceName = "默认麦克风";
this.isListening = false;
}
public void StartListening()
{
if (!isListening)
{
isListening = true;
Console.WriteLine($"[{deviceName}] 开始监听...");
}
}
public void StopListening()
{
if (isListening)
{
isListening = false;
Console.WriteLine($"[{deviceName}] 停止监听");
}
}
public bool IsListening()
{
return isListening;
}
}
}