C#表达式

 

Category

Bits

Type

Range/Precision

Signed integral

8

sbyte

–128...127

16

short

–32,768...32,767

32

int

–2,147,483,648...2,147,483,647

64

long

–9,223,372,036,854,775,808...9,223,372,036,854,775,807

Unsigned integral

8

byte

0...255

16

ushort

0...65,535

32

uint

0...4,294,967,295

64

ulong

0...18,446,744,073,709,551,615

Floating point

32

float

1.5 × 10−45 to 3.4 × 1038, 7-digit precision

64

double

5.0 × 10−324 to 1.7 × 10308, 15-digit precision

Decimal

128

decimal

1.0 × 10−28 to 7.9 × 1028, 28-digit precision



   

lock statement

class Account
{
    decimal balance;

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

using statement

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


 

goto statement

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

return statement

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

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

yield statement

static IEnumerable<int> Range(int from, int to) {
    for (int i = from; i < to; i++) {
       yield return i;
    }
    yield break;
}

static void Main() {
    foreach (int x in Range(-10,10)) {
       Console.WriteLine(x);
    }
}

throw and try
statements

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);
    }
    finally {
       Console.WriteLine(“Good bye!”);
    }
}

checked and unchecked statements

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

转载于:https://www.cnblogs.com/witer666/articles/922261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值