[ASP.NET MVC 小牛之路]02 - C#知识点提要

在学习 ASP.NET MVC 的过程中,扎实的 C# 基础是非常关键的。C# 是一门功能强大且广泛应用的编程语言,它为 ASP.NET MVC 提供了坚实的语言支撑。本文将对一些重要的 C# 知识点进行提要,帮助你更好地理解和运用 C# 进行 ASP.NET MVC 开发。

目录#

  1. 数据类型
    • 值类型
    • 引用类型
  2. 变量与常量
    • 变量声明与赋值
    • 常量定义
  3. 运算符
    • 算术运算符
    • 关系运算符
    • 逻辑运算符
  4. 控制语句
    • if - else 语句
    • switch 语句
    • for 循环
    • while 循环
    • do - while 循环
  5. 方法
    • 方法定义
    • 方法重载
    • 方法参数(值传递、引用传递、输出参数)
  6. 类与对象
    • 类的定义与实例化
    • 类的成员(字段、属性、方法)
  7. 继承与多态
    • 继承的实现
    • 多态的体现(虚方法、抽象类、接口)
  8. 委托与事件
    • 委托的定义与使用
    • 事件的定义与触发
  9. 泛型
    • 泛型类
    • 泛型方法
  10. 异常处理
    • try - catch - finally 结构

详细内容#

1. 数据类型#

  • 值类型:包括整数类型(如 intlong 等)、浮点类型(floatdouble)、字符类型(char)、布尔类型(bool)等。值类型直接存储数据值,在栈上分配内存。例如:
int num = 10;
bool isTrue = true;
  • 引用类型:如 string、数组、类、接口等。引用类型存储的是对象的引用,对象在堆上分配内存。例如:
string str = "Hello";
int[] arr = { 1, 2, 3 };

2. 变量与常量#

  • 变量声明与赋值:变量需要先声明类型,再赋值。例如:
int age;
age = 20;
// 也可以声明并初始化
string name = "John";
  • 常量定义:使用 const 关键字定义,一旦赋值不能更改。例如:
const double PI = 3.1415926;

3. 运算符#

  • 算术运算符+(加)、-(减)、*(乘)、/(除)、%(取余)。例如:
int a = 5, b = 3;
int sum = a + b;
int remainder = a % b;
  • 关系运算符==(等于)、!=(不等于)、>(大于)、<(小于)、>=(大于等于)、<=(小于等于)。返回布尔值。例如:
bool isGreater = a > b;
  • 逻辑运算符&&(逻辑与)、||(逻辑或)、!(逻辑非)。用于组合布尔表达式。例如:
bool condition1 = true;
bool condition2 = false;
bool result = condition1 && condition2;

4. 控制语句#

  • if - else 语句:根据条件执行不同的代码块。例如:
int score = 85;
if (score >= 90)
{
    Console.WriteLine("优秀");
}
else if (score >= 80)
{
    Console.WriteLine("良好");
}
else
{
    Console.WriteLine("一般");
}
  • switch 语句:根据表达式的值匹配不同的 case 分支。例如:
int day = 3;
switch (day)
{
    case 1:
        Console.WriteLine("星期一");
        break;
    case 2:
        Console.WriteLine("星期二");
        break;
    case 3:
        Console.WriteLine("星期三");
        break;
    default:
        Console.WriteLine("其他");
        break;
}
  • for 循环:常用于已知循环次数的情况。例如:
for (int i = 0; i < 5; i++)
{
    Console.Write(i + " ");
}
  • while 循环:先判断条件,再执行循环体。例如:
int i = 0;
while (i < 5)
{
    Console.Write(i + " ");
    i++;
}
  • do - while 循环:先执行一次循环体,再判断条件。例如:
int j = 0;
do
{
    Console.Write(j + " ");
    j++;
} while (j < 5);

5. 方法#

  • 方法定义:方法是一段可重复使用的代码块。例如:
int Add(int num1, int num2)
{
    return num1 + num2;
}
  • 方法重载:方法名相同,但参数列表不同(参数个数、类型、顺序)。例如:
int Add(int num1, int num2)
{
    return num1 + num2;
}
double Add(double num1, double num2)
{
    return num1 + num2;
}
  • 方法参数
    • 值传递:默认方式,传递参数的副本。例如:
void ChangeValue(int num)
{
    num = 100;
}
int a = 5;
ChangeValue(a); // a 还是 5
- **引用传递**:使用 `ref` 关键字,传递参数的引用。例如:
void ChangeReference(ref int num)
{
    num = 100;
}
int b = 5;
ChangeReference(ref b); // b 变为 100
- **输出参数**:使用 `out` 关键字,方法必须给 `out` 参数赋值。例如:
void GetValues(out int num1, out int num2)
{
    num1 = 10;
    num2 = 20;
}
int c, d;
GetValues(out c, out d);

6. 类与对象#

  • 类的定义与实例化:类是对象的模板。例如:
class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
Person person = new Person();
person.Name = "Alice";
person.Age = 30;
  • 类的成员
    • 字段:类中的变量。例如:
class Car
{
    private string brand; // 私有字段
    public string Brand
    {
        get { return brand; }
        set { brand = value; }
    }
}
- **属性**:提供对字段的访问控制(get 和 set 访问器)。如上面的 `Brand` 属性。
- **方法**:类中的函数。例如:
class Animal
{
    public void Eat()
    {
        Console.WriteLine("Animal is eating");
    }
}

7. 继承与多态#

  • 继承的实现:使用 : 符号,子类继承父类的成员(除了构造函数和析构函数)。例如:
class Dog : Animal
{
    public void Bark()
    {
        Console.WriteLine("Dog is barking");
    }
}
  • 多态的体现
    • 虚方法:父类方法用 virtual 修饰,子类可以用 override 重写。例如:
class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Drawing a shape");
    }
}
class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a circle");
    }
}
Shape shape = new Circle();
shape.Draw(); // 输出 "Drawing a circle"
- **抽象类**:用 `abstract` 修饰,包含抽象方法(没有实现)。子类必须实现抽象方法。例如:
abstract class AbstractShape
{
    public abstract void Draw();
}
class Square : AbstractShape
{
    public override void Draw()
    {
        Console.WriteLine("Drawing a square");
    }
}
- **接口**:用 `interface` 定义,类实现接口(用 `:` 后跟接口名)。例如:
interface IPrintable
{
    void Print();
}
class Document : IPrintable
{
    public void Print()
    {
        Console.WriteLine("Printing document");
    }
}

8. 委托与事件#

  • 委托的定义与使用:委托是一种类型,用于封装方法。例如:
delegate void MyDelegate(int num);
void PrintNumber(int num)
{
    Console.WriteLine(num);
}
MyDelegate del = PrintNumber;
del(10);
  • 事件的定义与触发:事件基于委托,是一种发布 - 订阅模式。例如:
class Button
{
    public event EventHandler Click;
    public void OnClick()
    {
        Click?.Invoke(this, EventArgs.Empty);
    }
}
class Program
{
    static void Button_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Button clicked");
    }
    static void Main()
    {
        Button button = new Button();
        button.Click += Button_Click;
        button.OnClick();
    }
}

9. 泛型#

  • 泛型类:可以在类定义时指定类型参数。例如:
class GenericList<T>
{
    private T[] items;
    public GenericList(int size)
    {
        items = new T[size];
    }
    public T this[int index]
    {
        get { return items[index]; }
        set { items[index] = value; }
    }
}
GenericList<int> intList = new GenericList<int>(5);
intList[0] = 1;
  • 泛型方法:方法中使用类型参数。例如:
T GetMax<T>(T num1, T num2) where T : IComparable<T>
{
    return num1.CompareTo(num2) > 0? num1 : num2;
}
int maxInt = GetMax(5, 3);

10. 异常处理#

  • try - catch - finally 结构:用于捕获和处理异常。例如:
try
{
    int result = 10 / 0; // 会抛出异常
}
catch (DivideByZeroException ex)
{
    Console.WriteLine("除数不能为 0: " + ex.Message);
}
finally
{
    Console.WriteLine("Finally block always executes");
}

引用#

通过对这些 C# 知识点的学习和掌握,你将为深入学习 ASP.NET MVC 打下坚实的基础,能够更好地理解和编写 ASP.NET MVC 应用程序中的 C# 代码。希望本文对你有所帮助,让你在 ASP.NET MVC 的学习道路上更进一步。