C#笔记

  1. C#  中的每个类型直接或间接地从 object 类类型派生 object 是所有类型的最终基类。
  2. C# 运算符

    类别

    表达式

    说明

    基本

    x.m

    成员访问

    x(...)

    方法和委托调用

    x[...]

    数组和索引器访问

    x++

    后增量

    x--

    后减量

    new T(...)

    对象和委托创建

    new T[...]

    数组创建

    typeof(T)

    获得 T System.Type 对象

    checked(x)

    checked 上下文中计算表达式

    unchecked(x)

    unchecked 上下文中计算表达式

    关系和类型检测

    x < y

    小于

    x > y

    大于

    x <= y

    小于或等于

    x >= y

    大于或等于

    x is T

    如果 x 属于 T 类型则返回 true否则返回 false

    x as T

    返回转换为类型 T x如果 x 不是 T 则返回 null

    逻辑 AND

    x & y

    整型按位 AND布尔逻辑 AND

    逻辑 XOR

    x ^ y

    整型按位 XOR布尔逻辑 XOR

    逻辑 OR

    x | y

    整型按位 OR布尔逻辑 OR

    条件 AND

    x && y

    仅当 x true 才对 y 求值

    条件 OR

    x || y

    仅当 x false 才对 y 求值

    条件

    x ? y : z

    如果 x true则对 y 求值如果 x false则对 z 求值

    赋值

    x = y

    赋值

    x  op= y

    复合赋值支持的运算符有

    *=   /=   %=   +=   -=   <<=   >>=   &=   ^=   |=

  3. 下表列出了 C# 的各语句并提供每个语句的示例。

    语句

    示例

    局部变量声明

    static void Main() {
        int a;
        int b = 2, c = 3;
        a = 1;
        Console.WriteLine(a + b + c);
    }

    局部常量声明

    static void Main() {
        const float pi = 3.1415927f;
        const int r = 25;
        Console.WriteLine(pi * r * r);
    }

    表达式语句

    static void Main() {
        int i;
        i = 123;                    // Expression statement
        Console.WriteLine(i);    // Expression statement
        i++;                        // Expression statement
        Console.WriteLine(i);    // Expression statement
    }

    if 语句

    static void Main(string[] args) {
        if (args.Length == 0) {
           Console.WriteLine("No arguments");
        }
        else {
           Console.WriteLine("One or more arguments");
        }
    }

    switch 语句

    static void Main(string[] args) {
        int n = args.Length;
        switch (n) {
           case 0:
               Console.WriteLine("No arguments");
               break;
           case 1:
               Console.WriteLine("One argument");
               break;
           default:
               Console.WriteLine("{0} arguments", n);
               break;
           }
        }
    }

    while 语句

    static void Main(string[] args) {
        int i = 0;
        while (i < args.Length) {
           Console.WriteLine(args[i]);
           i++;
        }
    }

    do 语句

    static void Main() {
        string s;
        do {
           s = Console.ReadLine();
           if (s != null) Console.WriteLine(s);
        } while (s != null);
    }

    for 语句

    static void Main(string[] args) {
        for (int i = 0; i < args.Length; i++) {
           Console.WriteLine(args[i]);
        }
    }

    foreach 语句

    static void Main(string[] args) {
        foreach (string s in args) {
           Console.WriteLine(s);
        }
    }

    break 语句

    static void Main() {
        while (true) {
           string s = Console.ReadLine();
           if (s == null) break;
           Console.WriteLine(s);
        }
    }

    continue 语句

    static void Main(string[] args) {
        for (int i = 0; i < args.Length; i++) {
           if (args[i].StartsWith("/")) continue;
           Console.WriteLine(args[i]);
        }
    }

    goto 语句

    static void Main(string[] args) {
        int i = 0;
        goto check;
        loop:
        Console.WriteLine(args[i++]);
        check:
        if (i < args.Length) goto loop;
    }

    return 语句

    static int Add(int a, int b) {
        return a + b;
    }

    static void Main() {
        Console.WriteLine(Add(1, 2));
        return;
    }

    throw try
    语句

    static double Divide(double x, double y) {
        if (y == 0) throw new DivideByZeroException();
        return x / y;
    }

    static void Main(string[] args) {
        try {
           if (args.Length != 2) {
               throw new Exception("Two numbers required");
           }
           double x = double.Parse(args[0]);
           double y = double.Parse(args[1]);
           Console.WriteLine(Divide(x, y));
        }
        catch (Exception e) {
           Console.WriteLine(e.Message);
        }
    }

    checked unchecked 语句

    static void Main() {
        int i = int.MaxValue;
        checked {
           Console.WriteLine(i + 1);       // Exception
        }
        unchecked {
           Console.WriteLine(i + 1);       // Overflow
        }
    }

    lock 语句

    class Account
    {
        decimal balance;

        public void Withdraw(decimal amount) {
           lock (this) {
               if (amount > balance) {
                  throw new Exception("Insufficient funds");
               }
               balance -= amount;
           }
        }
    }

    using 语句

    static void Main() {
        using (TextWriter w = File.CreateText("test.txt")) {
           w.WriteLine("Line one");
           w.WriteLine("Line two");
           w.WriteLine("Line three");
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值