百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

C# 编程语言 31-40个经典案例

liuian 2025-08-01 18:41 3 浏览

案例 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!

相关推荐

前端开发为什么需要Promise

一、引言在Web前端开发中,异步操作是绕不开的话题。随着用户对网页交互性和响应速度要求的不断提高,开发者们不得不处理越来越多的异步任务,如数据获取、文件读写等。本文旨在探讨Promise作为现代Jav...

『React』组件副作用,useEffect讲解

在React开发中,有时候会听到“副作用”这个词。特别是用到useEffect这个Hook的时候,官方就明确说它是用来处理副作用的。那什么是副作用?为什么我们要专门管控它?今天就聊聊Re...

图解 Promise 实现原理(一):基础实现

作者:孔垂亮转发链接:https://mp.weixin.qq.com/s/UNzYgpnKzmW6bAapYxnXRQ前言很多同学在学习Promise时,知其然却不知其所以然,对其中的用法理解不...

C#实现归并排序与快速排序

字数914,阅读大约需5分钟C#实现归并排序与快速排序以下是使用C#实现的归并排序和快速排序算法代码:usingSystem;usingSystem.Collections.Gener...

C#.NET Newtonsoft.Json 详解

简介Newtonsoft.Json(又称Json.NET)是.NET生态中最流行的JSON序列化/反序列化库,支持.NETFramework、.NETCore、Mono、Xamarin...

C# - 类文件构成,C#基本语法,Console属性与方法 007

类文件(.cs)构成类文件主要分为引用命名空间与自己项目的命名空间1)引用命名空间主要是引用类库,分为内部(.Net类库与解决方案内其他项目的命名空间)外部(引用别人的命名空间),之前说过类库的...

不要过度使用列表(List): C# 数据结构

编程中的每一个决定都会对性能和清晰度产生无声的影响。在C#中,这样重要的选择之一就是选择正确的数据结构。数据结构是基础支柱。这些结构是数据生存、呼吸和交互的地方,决定了代码的效率和可读性。但...

C# 编程语言 31-40个经典案例

案例31:LINQ查询学生成绩排序说明:演示如何使用LINQ查询并排序数据集合。usingSystem;usingSystem.Collections.Generic;usingSyst...

C#中常用的数据结构

写在前面最近在使用.net开发一些程序。它使用的编程语言是C#。我们来看一下它的常用的数据结构有哪些。常用数据结构C#中常见的数据结构:1数组(Array):用于存储固定大小的同类型元素集合...

C# 编程10个经典案例

C#是微软推出的一门现代化、面向对象的高级编程语言,在桌面应用、Web、移动、游戏和云计算等开发领域广泛应用。本篇文章为广大程序员整理了50个必须收藏的经典C#编程案例,助你提升实战能力。案...

C# 动态数组(ArrayList)

动态数组(ArrayList)代表了可被单独索引的对象的有序集合。它基本上可以替代一个数组。但是,与数组不同的是,您可以使用索引在指定的位置添加和移除项目,动态数组会自动重新调整它的大小。它也允许在...

c#集合排序

在C#中,集合排序是一种常见的操作,它可以帮助我们对集合中的元素进行排序。C#中提供了多种集合排序方法,包括Array.Sort、List.Sort、SortedList和SortedSet等。下面分...

c#学习手册 (苏素芳等) 高清PDF版

《c#学习手册》以初学者为核心,全面介绍了使用c#语言进行程序开发的各种技术。在内容排列上由浅入深,让读者循序渐进地掌握编程技术;在内容讲解上结合丰富的图解和形象的比喻,帮助读者理解“晦涩难懂”的技术...

C#中的数组探究与学习

C#中的数组一般分为:①.一维数组。②.多维数组,也叫矩形数组。③.锯齿数组,也叫交错数组。一.数组定义:数组是一种聚合数据类型,它是将具有相同类型的若干变量有序地组织在一起的集合,是最基本的数据结构...

C# 12最新特性解析:代码还能这样写?!微软工程师都惊呆了

在C#的持续进化历程中,每一个新版本都宛如一场技术革新的盛宴,C#12更是如此。它所带来的全新特性,不仅刷新了开发者对代码编写方式的认知,甚至连微软工程师们都为之惊叹。今天,就让我们一同深入探索C#...