王剑编程网

分享专业编程知识与实战技巧

C# 编程语言 31-40个经典案例(c#经典例子)

案例 31:LINQ 查询学生成绩排序

说明:演示如何使用 LINQ 查询并排序数据集合。

using System;

using System.Collections.Generic;

using System.Linq;


class Program

{

class Student

{

public string Name { get; set; }

public int Score { get; set; }

}


static void Main()

{

var students = new List<Student>

{

new Student { Name = "Tom", Score = 85 },

new Student { Name = "Jerry", Score = 92 },

new Student { Name = "Lucy", Score = 78 }

};


var result = students.OrderByDescending(s => s.Score);


foreach (var student in result)

{

Console.WriteLine(#34;{student.Name}: {student.Score}");

}

}

}

输出:

Jerry: 92

Tom: 85

Lucy: 78






案例 32:序列化与反序列化 JSON(使用 System.Text.Json)


using System;

using System.Text.Json;


class Program

{

class User

{

public string Name { get; set; }

public int Age { get; set; }

}


static void Main()

{

User user = new User { Name = "Alice", Age = 28 };

string json = JsonSerializer.Serialize(user);

Console.WriteLine("Serialized: " + json);


User deserialized = JsonSerializer.Deserialize<User>(json);

Console.WriteLine("Deserialized: " + deserialized.Name + ", " + deserialized.Age);

}

}

输出:

Serialized: {"Name":"Alice","Age":28}

Deserialized: Alice, 28






案例 33:反转字符串


using System;


class Program

{

static void Main()

{

string input = "Hello World";

char[] chars = input.ToCharArray();

Array.Reverse(chars);

string reversed = new string(chars);

Console.WriteLine(reversed);

}

}

输出:

dlroW olleH






案例 34:创建自定义异常


using System;


class MyException : Exception

{

public MyException(string message) : base(message) { }

}


class Program

{

static void Main()

{

try

{

throw new MyException("这是自定义异常!");

}

catch (MyException ex)

{

Console.WriteLine("捕获异常: " + ex.Message);

}

}

}

输出:

捕获异常: 这是自定义异常!






案例 35:简单计算器(加减乘除)


using System;


class Program

{

static void Main()

{

double a = 10, b = 5;

Console.WriteLine(#34;加法: {a + b}");

Console.WriteLine(#34;减法: {a - b}");

Console.WriteLine(#34;乘法: {a * b}");

Console.WriteLine(#34;除法: {a / b}");

}

}

输出:

加法: 15

减法: 5

乘法: 50

除法: 2






案例 36:列出文件夹下所有文件(使用 System.IO)


using System;

using System.IO;


class Program

{

static void Main()

{

string[] files = Directory.GetFiles(@"C:\Windows");


foreach (string file in files)

{

Console.WriteLine(file);

}

}

}

输出:(示例)

C:\Windows\explorer.exe

C:\Windows\notepad.exe

...






案例 37:使用 Thread 创建线程


using System;

using System.Threading;


class Program

{

static void Print()

{

Console.WriteLine("子线程运行中...");

}


static void Main()

{

Thread t = new Thread(Print);

t.Start();

Console.WriteLine("主线程结束");

}

}

输出:

主线程结束

子线程运行中...






案例 38:使用 lock 实现线程同步


using System;

using System.Threading;


class Program

{

static int count = 0;

static object locker = new object();


static void Add()

{

for (int i = 0; i < 1000; i++)

{

lock (locker)

{

count++;

}

}

}


static void Main()

{

Thread t1 = new Thread(Add);

Thread t2 = new Thread(Add);

t1.Start(); t2.Start();

t1.Join(); t2.Join();

Console.WriteLine("Count = " + count);

}

}

输出:

Count = 2000






案例 39:Web 请求示例(使用 HttpClient)


using System;

using System.Net.Http;

using System.Threading.Tasks;


class Program

{

static async Task Main()

{

HttpClient client = new HttpClient();

string html = await client.GetStringAsync("https://example.com");

Console.WriteLine(html.Substring(0, 100)); // 截取前100字符

}

}

输出:(示例)

<!doctype html>

<html>

<head>

<title>Example Domain</title>

...




案例 40:读取并写入文本文件


using System;

using System.IO;


class Program

{

static void Main()

{

string path = "test.txt";

File.WriteAllText(path, "Hello, file!");

string content = File.ReadAllText(path);

Console.WriteLine(content);

}

}

输出:

Hello, file!

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言