C# 编程语言41-50个经典案例(含完整源代码和输出结果)

C# 编程语言41-50个经典案例(含完整源代码和输出结果)

编码文章call10242025-08-18 3:55:422A+A-


案例 41:异常捕获与处理



代码:

using System;


class Program {

static void Main() {

try {

int x = 10, y = 0;

int z = x / y;

} catch (DivideByZeroException e) {

Console.WriteLine("不能除以零:" + e.Message);

} finally {

Console.WriteLine("程序执行完毕");

}

}

}

输出:

不能除以零:尝试除以零。

程序执行完毕





案例 42:简单单元测试实例(使用 MSTest)



代码:

public class Calculator {

public int Add(int a, int b) => a + b;

}

测试代码:

[TestClass]

public class CalculatorTests {

[TestMethod]

public void AddTest() {

Calculator calc = new Calculator();

Assert.AreEqual(5, calc.Add(2, 3));

}

}





案例 43:通过反射获取类信息



代码:

using System;

using System.Reflection;


class Student {

public string Name { get; set; }

public int Age { get; set; }

}


class Program {

static void Main() {

Type t = typeof(Student);

Console.WriteLine("类名: " + t.Name);

foreach (var prop in t.GetProperties()) {

Console.WriteLine("属性: " + prop.Name);

}

}

}

输出:

类名: Student

属性: Name

属性: Age





案例 44:写入并读取 INI 文件(需添加引用)



代码:

using System.Runtime.InteropServices;

using System.Text;


class IniHelper {

[DllImport("kernel32")]

private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


public static void WriteIni(string path, string section, string key, string value) {

WritePrivateProfileString(section, key, value, path);

}


public static string ReadIni(string path, string section, string key) {

StringBuilder temp = new StringBuilder(255);

GetPrivateProfileString(section, key, "", temp, 255, path);

return temp.ToString();

}

}





案例 45:读取 CSV 数据并展示



代码:

using System;

using System.IO;


class Program {

static void Main() {

string[] lines = File.ReadAllLines("data.csv");

foreach (string line in lines) {

string[] parts = line.Split(',');

Console.WriteLine(#34;姓名: {parts[0]}, 年龄: {parts[1]}");

}

}

}





案例 46:WPF 简单按钮事件



XAML:

<Button Content="点击我" Click="Button_Click"/>

C# 代码:

private void Button_Click(object sender, RoutedEventArgs e) {

MessageBox.Show("按钮被点击");

}





案例 47:WinForms 创建计算器



使用 Visual Studio 创建一个新 Windows Forms 应用程序,拖放按钮和文本框,设置按钮事件如下:

private void btnAdd_Click(object sender, EventArgs e) {

int a = int.Parse(txtA.Text);

int b = int.Parse(txtB.Text);

txtResult.Text = (a + b).ToString();

}





案例 48:使用 async/await 异步下载网页



代码:

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));

}

}





案例 49:创建自定义控件



代码:

public class MyLabel : Label {

public MyLabel() {

this.Text = "我是自定义标签";

this.ForeColor = Color.Red;

}

}





案例 50:创建 DLL 并调用



步骤:


  1. 创建 Class Library 项目并添加以下代码:


public class MathLib {

public int Multiply(int a, int b) => a * b;

}


  1. 在其他项目引用 DLL,并调用:


MathLib lib = new MathLib();

Console.WriteLine(lib.Multiply(3, 5));

点击这里复制本文地址 以上内容由文彬编程网整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

文彬编程网 © All Rights Reserved.  蜀ICP备2024111239号-4