【C#】常用类的基础知识及基本应用

在C#中,有许多常用的核心类库(.NET Framework 或 .NET Core),它们可以大大简化开发过程并提供强大的功能。下面是一些常见的类及其用途的简要介绍:

 

1. System.IO.File

用于文件操作,如创建、读取、写入和删除文件。

using System;

using System.IO;

 

class Program

{

    static void Main()

    {

        string path = "example.txt";

        

        // 创建并写入文件

        File.WriteAllText(path, "Hello from C# File Class");

 

        // 读取文件内容

        try

        {

            string fileContent = File.ReadAllText(path);

            Console.WriteLine($"File Content:\n{fileContent}");

        }

        catch (Exception ex)

        {

            Console.WriteLine("An error occurred while reading the file: " + ex.Message);

        }

 

        // 删除文件

        if (File.Exists(path))

            File.Delete(path);

 

        Console.WriteLine("Operation completed.");

    }

}

 

2. System.IO.Directory

用于目录操作,如创建、删除和列出目录。

using System;

using System.IO;

 

class Program

{

    static void Main()

    {

        string path = "exampleDirectory";

 

        // 创建目录

        Directory.CreateDirectory(path);

 

        // 列出目录下的文件和子目录

        Console.WriteLine("Files and directories in {0}:", path);

        foreach (string file in Directory.GetFiles(path))

            Console.WriteLine("\tFile: {0}", file);

 

        foreach (string directory in Directory.GetDirectories(path))

            Console.WriteLine("\tDirectory: {0}", directory);

 

        // 删除空目录

        Directory.Delete(path, false);

 

        Console.WriteLine("Operation completed.");

    }

}

 

3. System.Collections.Generic.List

用于处理动态数组,可以方便地添加、删除和遍历元素。

using System;

using System.Collections.Generic;

 

class Program

{

    static void Main()

    {

        List names = new List();

        

        // 添加元素

        names.Add("Alice");

        names.Add("Bob");

 

        Console.WriteLine("Names in the list:");

        foreach (string name in names)

            Console.WriteLine("\t{0}", name);

 

        // 删除元素

        if (names.Contains("Bob"))

            names.Remove("Bob");

 

        Console.WriteLine("Updated list of names:");

        foreach (string name in names)

            Console.WriteLine("\t{0}", name);

    }

}

 

4. System.Linq

提供了一种声明性的方法来查询集合。可以用于过滤、排序和投影数据。

using System;

using System.Collections.Generic;

using System.Linq;

 

class Program

{

    static void Main()

    {

        List numbers = new List();

        

        // 添加元素

        numbers.Add(5);

        numbers.Add(3);

        numbers.Add(8);

 

        // 使用 LINQ 过滤、排序和投影数据

        var sortedNumbers = numbers.OrderBy(n => n).Select(n => n * 2);

 

        Console.WriteLine("Sorted and multiplied numbers:");

        foreach (int number in sortedNumbers)

            Console.WriteLine("\t{0}", number);

    }

}

 

5. System.Text.Json

用于序列化和反序列化 JSON 数据。

using System;

using System.Text.Json;

 

class Program

{

    static void Main()

    {

        Person person = new Person { Name = "Alice", Age = 30 };

 

        // 序列化为 JSON 字符串

        string jsonString = JsonSerializer.Serialize(person);

        Console.WriteLine("Serialized JSON String:\n{0}", jsonString);

 

        // 反序列化为对象

        try

        {

            person = JsonSerializer.Deserialize(jsonString);

            Console.WriteLine("\nDeserialized Person Object:");

            Console.WriteLine($"\tName: {person.Name}, Age: {person.Age}");

        }

        catch (JsonException ex)

        {

            Console.WriteLine("An error occurred while deserializing the JSON string: " + ex.Message);

        }

    }

 

    public class Person

    {

        public string Name { get; set; }

        public int Age { get; set; }

    }

}

 

这些示例展示了如何使用一些常见的 C# 类来完成基本的操作。

 

你可能感兴趣的:(c#)