当前位置:首页 > C#学习教程 > 正文内容

C#基础学习教程

xmwl4周前 (02-15)C#学习教程79

一、C#简介

  • 面向对象:C#是微软开发的现代编程语言,基于.NET框架,支持面向对象编程(OOP)。

  • 跨平台:借助.NET Core,可在Windows、Linux和macOS上运行。

  • 应用场景:广泛用于桌面应用(WPF、WinForms)、Web后端(ASP.NET)、游戏开发(Unity)等。


二、开发环境搭建

  1. 安装工具:

    • Visual Studio:微软官方IDE(推荐社区版)。

      Visual Studio Code:轻量级编辑器,需安装C#扩展。

      第一个程序:

    • using System;
      
      class Program
      {
          static void Main(string[] args)
          {
              Console.WriteLine("Hello, C#!");
          }
    • 运行结果:在控制台输出 Hello, C#!


三、基础语法

 1.变量与数据类型

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, C#!");
    }
}

2.控制流语句

条件语句:

if (age >= 18)
{
    Console.WriteLine("成年人");
}
else
{
    Console.WriteLine("未成年人");
}


  • 循环语句

  • for (int i = 0; i < 5; i++)
    {
        Console.WriteLine(i);  // 输出0到4
    }
    
    int j = 0;
    while (j < 3)
    {
        Console.WriteLine(j);  // 输出0、1、2
        j++;
    }

四、面向对象编程(OOP)

1. 类与对象

public class Person
{
    // 字段
    private string name;
    
    // 属性
    public int Age { get; set; }
    
    // 构造函数
    public Person(string name)
    {
        this.name = name;
    }
    
    // 方法
    public void Introduce()
    {
        Console.WriteLine($"我叫{name}, 今年{Age}岁。");
    }
}

// 使用类
Person person = new Person("张三");
person.Age = 30;
person.Introduce();  // 输出:我叫张三, 今年30岁。


2. 继承与多态

public class Student : Person  // 继承自Person类
{
    public string StudentId { get; set; }
    
    public Student(string name) : base(name) { }
    
    // 方法重写(需父类方法标记为virtual)
    public override void Introduce()
    {
        Console.WriteLine($"我是学生{StudentId}, 名字是{Name}。");
    }
}

五、进阶特性

1. 属性与索引器

// 自动属性
public string Email { get; private set; }

// 索引器
public class MyCollection
{
    private int[] data = new int[10];
    public int this[int index]
    {
        get => data[index];
        set => data[index] = value;
    }
}

2. 异常处理

try
{
    int result = 10 / int.Parse(Console.ReadLine());
}
catch (DivideByZeroException ex)
{
    Console.WriteLine("除数不能为零!");
}
catch (Exception ex)
{
    Console.WriteLine($"错误:{ex.Message}");
}
finally
{
    Console.WriteLine("执行完毕。");
}

3. 文件操作

using System.IO;

// 写入文件
File.WriteAllText("test.txt", "Hello C#!");

// 读取文件
string content = File.ReadAllText("test.txt");
Console.WriteLine(content);  // 输出Hello C#!

4. 集合类型

List<string> names = new List<string> { "Alice", "Bob" };
names.Add("Charlie");

Dictionary<int, string> students = new Dictionary<int, string>();
students.Add(1, "张三");
students[2] = "李四";

六、实战示例:学生管理系统

public class StudentManager
{
    private List<Student> students = new List<Student>();
    
    public void AddStudent(Student student)
    {
        students.Add(student);
    }
    
    public void DisplayAll()
    {
        foreach (var s in students)
        {
            Console.WriteLine($"{s.Name} - {s.StudentId}");
        }
    }
}

// 使用示例
var manager = new StudentManager();
manager.AddStudent(new Student("李雷") { StudentId = "S001" });
manager.DisplayAll();

七、学习资源

版权声明:
本站资源和文章内容大部分收集于网络,
本站所有资源的版权均属于原作者所有,
本站资源只用于参考学习,请勿直接商用,
若由于商用引起版权纠纷,一切责任均由使用者承担。
若有侵权之处请联系站长我们会第一时间删除

本文由XM技术学习分享发布,如需转载请注明出处。

本文链接:http://www.xmwl.cc/csharp/247.html

标签: C#教程
分享给朋友:
返回列表

没有更早的文章了...

没有最新的文章了...

“C#基础学习教程” 的相关文章