C# 快速入门指南

C# 快速入门指南

1. 安装 C#

  • Microsoft Visual Studio 下载并安装最新版本的 Visual Studio IDE。
  • 选择“ASP.NET 和 Web 开发”或“桌面和移动应用开发”等工作负载。

2. 基本语法

2.1 变量和数据类型

C# 支持多种数据类型,如整数、浮点数、字符、字符串、数组和对象。以下是一些示例代码:

using System;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            // 数字
            int a = 10;
            double b = 3.14;

            // 字符
            char c = 'A';

            // 字符串
            string name = "Alice";

            // 布尔值
            bool isActive = true;

            // 数组
            int[] numbers = { 1, 2, 3, 4, 5 };

            Console.WriteLine($"Name: {name}");
        }
    }
}
2.2 基本操作
using System;

namespace BasicOperations
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            double b = 3.14;

            // 算术运算
            int sum = a + (int)b;
            int difference = a - (int)b;
            double product = a * b;
            double quotient = a / b;

            // 字符串操作
            string name = "Alice";
            string greeting = "Hello, " + name;

            // 数组操作
            int[] numbers = { 1, 2, 3, 4, 5 };
            numbers[0] = 10;

            Console.WriteLine($"Sum: {sum}");
            Console.WriteLine(greeting);
        }
    }
}

3. 控制结构

3.1 条件语句

C# 使用 ifelse ifelse 进行条件判断:

using System;

namespace ConditionalStatements
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 10;
            int b = 5;

            if (a > b)
            {
                Console.WriteLine("a is greater than b");
            }
            else if (a == b)
            {
                Console.WriteLine("a is equal to b");
            }
            else
            {
                Console.WriteLine("a is less than b");
            }
        }
    }
}
3.2 循环

C# 支持 forwhiledo-while 循环:

using System;

namespace Loops
{
    class Program
    {
        static void Main(string[] args)
        {
            // for 循环
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine($"i: {i}");
            }

            // while 循环
            int count = 0;
            while (count < 5)
            {
                Console.WriteLine($"count: {count}");
                count++;
            }

            // do-while 循环
            int num = 0;
            do
            {
                Console.WriteLine($"num: {num}");
                num++;
            } while (num < 5);
        }
    }
}

4. 函数

函数用 return_typefunction_name 定义:

using System;

namespace Functions
{
    class Program
    {
        static void Main(string[] args)
        {
            int sum = Add(10, 20);
            Console.WriteLine($"Sum: {sum}");
        }

        static int Add(int x, int y)
        {
            return x + y;
        }
    }
}

5. 类和对象

C# 是面向对象的编程语言,可以定义类和创建对象:

using System;

namespace ClassesAndObjects
{
    class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Person(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public void Introduce()
        {
            Console.WriteLine($"My name is {Name} and I am {Age} years old.");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person alice = new Person("Alice", 25);
            alice.Introduce();
        }
    }
}

6. 集合和泛型

6.1 集合

C# 提供了多种集合类型,如 ListDictionary 等:

using System;
using System.Collections.Generic;

namespace Collections
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
            numbers.Add(6);

            Dictionary<string, int> ages = new Dictionary<string, int>
            {
                { "Alice", 25 },
                { "Bob", 30 }
            };

            Console.WriteLine($"Alice's age: {ages["Alice"]}");
        }
    }
}
6.2 泛型

泛型允许你编写与类型无关的代码:

using System;

namespace Generics
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"Int Sum: {Add(10, 20)}");
            Console.WriteLine($"Double Sum: {Add(3.14, 2.72)}");
        }

        static T Add<T>(T x, T y)
        {
            dynamic dx = x;
            dynamic dy = y;
            return dx + dy;
        }
    }
}

7. 文件操作

C# 提供简单的文件读写功能:

using System;
using System.IO;

namespace FileOperations
{
    class Program
    {
        static void Main(string[] args)
        {
            // 写入文件
            File.WriteAllText("example.txt", "Hello, world!");

            // 读取文件
            string content = File.ReadAllText("example.txt");
            Console.WriteLine(content);
        }
    }
}

8. 异常处理

使用 trycatchfinally 处理异常:

using System;

namespace ExceptionHandling
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                int a = 10;
                int b = 0;
                int c = a / b;
            }
            catch (DivideByZeroException ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            finally
            {
                Console.WriteLine("This will always execute.");
            }
        }
    }
}

9. 异步编程

C# 提供了强大的异步编程支持:

using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace AsyncProgramming
{
    class Program
    {
        static async Task Main(string[] args)
        {
            string url = "https://api.github.com";
            string content = await GetContentAsync(url);
            Console.WriteLine(content);
        }

        static async Task<string> GetContentAsync(string url)
        {
            using HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; GrandCircus/1.0)");
            return await client.GetStringAsync(url);
        }
    }
}

总结

这份文档涵盖了 C# 的基础知识和常用操作。通过不断练习和参考官方文档,你将能够掌握 C# 并应用于各种项目中。

有关更多详细信息,请参考 C# 官方文档

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

东城十三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值