心 涯

NET&JAVA&PHP(要跨界,你不只要跨「脑」的界限,更要跨越「心」的界限,不怕改变,不怕再学习!)

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
 

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
    }
}

posted on 2007-10-12 14:34  witer666  阅读(367)  评论(0编辑  收藏  举报