20241016打卡
C# 是一种面向对象的编程语言,拥有简洁而强大的语法设计,适合构建各种应用程序。下面是 C# 基础语法的介绍,涵盖语言的关键部分,帮助你快速理解和使用。
1. 基础结构
C# 的程序入口点是 Main
方法。每个 C# 程序都至少包含一个类和一个 Main
方法。代码组织在类和命名空间中。
using System;
namespace HelloWorldApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}
using
: 引入命名空间,类似于导入其他库。namespace
: 用于组织代码,防止命名冲突。class
: C# 是面向对象的,所有代码都必须包含在类中。Main
方法: 程序的入口点。
2. 变量与数据类型
C# 是强类型语言,所有变量在使用前必须声明类型。常见的基本数据类型包括:
int myInt = 42; // 整数
double myDouble = 3.14; // 浮点数
char myChar = 'A'; // 字符
string myString = "Hello"; // 字符串
bool myBool = true; // 布尔类型
int
: 表示整数(4字节,范围为 -2^31 到 2^31-1)。double
: 双精度浮点数,用于表示小数。char
: 单个 Unicode 字符,使用单引号。string
: 字符串,表示文本,用双引号。bool
: 布尔类型,用于表示true
或false
。
3. 条件语句
C# 支持标准的条件语句,包括 if
、else
、switch
等。
int x = 10;
if (x > 5)
{
Console.WriteLine("x is greater than 5");
}
else
{
Console.WriteLine("x is 5 or less");
}
switch (x)
{
case 10:
Console.WriteLine("x is 10");
break;
default:
Console.WriteLine("x is not 10");
break;
}
if
和else
用于条件判断。switch
语句用于根据值的不同执行不同的代码块。
4. 循环语句
C# 提供多种循环结构,如 for
、while
和 foreach
,用于重复执行代码。
// for 循环
for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
// while 循环
int j = 0;
while (j < 5)
{
Console.WriteLine(j);
j++;
}
// foreach 循环
int[] numbers = {1, 2, 3, 4, 5};
foreach (int number in numbers)
{
Console.WriteLine(number);
}
for
: 循环控制变量进行预定义次数的迭代。while
: 当条件为真时重复执行代码。foreach
: 专门用于遍历集合(如数组、列表)。
5. 方法
方法是执行特定任务的代码块。可以定义带有参数和返回值的方法。
class Program
{
static void Main(string[] args)
{
int result = Add(5, 3);
Console.WriteLine(result); // 输出 8
}
static int Add(int a, int b)
{
return a + b;
}
}
- 返回类型: 方法必须指定返回类型(如
int
、void
等)。 - 参数: 方法可以接受参数,参数类型必须显式声明。
return
: 用于返回结果。
6. 类与对象
C# 是面向对象的语言,支持类和对象的概念。类是对象的模板,对象是类的实例。
class Person
{
public string Name { get; set; }
public int Age { get; set; }
public void Introduce()
{
Console.WriteLine($"Hi, I am {Name}, and I am {Age} years old.");
}
}
class Program
{
static void Main(string[] args)
{
Person person = new Person();
person.Name = "John";
person.Age = 30;
person.Introduce(); // 输出: Hi, I am John, and I am 30 years old.
}
}
class
: 用于定义对象的属性和行为。- 属性(Properties): 类的变量,通过
get
和set
来访问。 - 方法(Methods): 类的行为,定义类的操作。
7. 继承与多态
C# 支持面向对象的特性,如继承和多态。子类可以继承父类的属性和方法,并可以重写它们。
class Animal
{
public virtual void Speak()
{
Console.WriteLine("The animal makes a sound");
}
}
class Dog : Animal
{
public override void Speak()
{
Console.WriteLine("The dog barks");
}
}
class Program
{
static void Main(string[] args)
{
Animal myAnimal = new Animal();
myAnimal.Speak(); // 输出: The animal makes a sound
Animal myDog = new Dog();
myDog.Speak(); // 输出: The dog barks
}
}
- 继承(Inheritance):
Dog
类继承Animal
类。 - 多态(Polymorphism): 使用
virtual
和override
关键字实现方法重写。
8. 异常处理
C# 提供异常处理机制来捕获和处理错误,避免程序崩溃。
try
{
int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[5]);
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("Index out of range: " + ex.Message);
}
finally
{
Console.WriteLine("This will always run.");
}
try
: 包含可能抛出异常的代码。catch
: 捕获并处理异常。finally
: 无论是否发生异常,最终都会执行的代码块。
9. 属性(Properties)
C# 中的属性通过 get
和 set
方法来控制变量的访问。它们允许你对字段的读取和写入进行封装。
class Person
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
- 属性封装了类的字段,提供了更灵活和安全的访问控制。
10. 集合(Collections)
C# 提供了多种集合类型,例如 List
、Dictionary
等,用于存储和操作数据。
List<int> numbers = new List<int>() { 1, 2, 3 };
numbers.Add(4);
Console.WriteLine(numbers.Count); // 输出 4
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("John", 30);
ages.Add("Alice", 25);
Console.WriteLine(ages["John"]); // 输出 30
List
: 动态数组,支持增删元素。Dictionary
: 键值对集合。
总结:
C# 的基础语法简洁且强大,最主要支持面向对象编程。