C#语言规范

一 C#类型系统概述: 

类别

说明

值类型

简单类型

有符号整型 sbyte, short, int, long

无符号整型 byte, ushort, uint, ulong

Unicode字符 char

IEEE 浮点型 float, double

高精度小数 decimal

布尔型 bool

枚举类型

enum E {...}形式的用户定义的类型

结构类型

struct S {...}形式的用户定义的类型

引用类型

类类型

所有其他类型的最终基类 object

Unicode 字符串 string

classC{...}形式的用户定义的类型

接口类型

interface I {...} 形式的用户定义的类型

数组类型

一维和多维数组例如int[]int[,]

委托类型

delegate T D(...)形式的用户定义的类型


 二 C#的数值类型

类别

说明

值类型

简单类型

有符号整型 sbyte, short, int, long

无符号整型 byte, ushort, uint, ulong

Unicode字符 char

IEEE 浮点型 float, double

高精度小数 decimal

布尔型 bool

枚举类型

enum E {...}形式的用户定义的类型

结构类型

struct S {...}形式的用户定义的类型

引用类型

类类型

所有其他类型的最终基类 object

Unicode 字符串 string

classC{...}形式的用户定义的类型

接口类型

interface I {...} 形式的用户定义的类型

数组类型

一维和多维数组例如int[]int[,]

委托类型

delegate T D(...)形式的用户定义的类型


C# 程序使用类型声明(type declaration) 创建新类型。类型声明指定新类型的名称和成员。有五种类别的 C# 类型是可由用户定义的:类类型、结构类型、接口类型、枚举类型和委托类型。

类类型定义一个包含数据成员字段和函数成员方法、属性等的数据结构。类类型支持继承和多态,这些是派生类可用来扩展和专用化基类的一种机制。

结构类型与类类型相似表示一个带有数据成员和函数成员的结构。但是,与类不同,结构是一种值类型,并且不需要堆分配。结构类型不支持用户指定的继承,并且所有结构类型都隐式地从类型 object 继承。

接口类型定义了一个协定作为一个函数成员的命名集合。实现某个接口的类或结构必须提供该接口的函数成员的实现。一个接口可以从多个基接口继承,而一个类或结构可以实现多个接口。

枚举类型是具有命名常量的独特的类型。每种枚举类型都具有一个基础类型,该基础类型必须是八种整型之一。枚举类型的值集和它的基础类型的值集相同。

委托类型表示对具有特定参数列表和返回类型的方法的引用。通过委托,我们能够将方法作为实体赋值给变量和作为参数传递。委托类似于在其他某些语言中的函数指针的概念,但是与函数指针不同,委托是面向对象的,并且是类型安全的。

C# 支持由任何类型组成的一维和多维数组。与其他类型不同,数组类型不必在使用之前事先声明。实际上,数组类型是通过在某个类型名后加一对方括号来构造的。例如,int[] 是一维 int 数组,int[,] 是二维 int 数组,int[][] 是一维 int 数组的一维数组。

三 变量类型
 

变量类型

可能的内容

值类型

类型完全相同的值

object

空引用、对任何引用类型的对象的引用或对任何值类型的装箱值的引用

类类型

空引用、对该类类型的实例的引用或者对从该类类型派生的类的实例的引用

接口类型

空引用、对实现该接口类型的类类型的实例的引用或者对实现该接口类型的值类型的装箱值的引用

数组类型

空引用、对该数组类型的实例的引用或者对兼容数组类型的实例的引用

委托类型

空引用或对该委托类型的实例的引用


四 C#运算符
 

类别

表达式

说明

基本

x.m

成员访问

x(...)

方法和委托调用

x[...]

数组和索引器访问

x++

后增量

x--

后减量

new T(...)

对象和委托创建

new T[...]

数组创建

typeof(T)

获得T System.Type对象

checked(x)

checked 上下文中计算表达式

unchecked(x)

unchecked 上下文中计算表达式

一元

+x

表达式的值相同

-x

求相反数

!x

逻辑求反

~x

按位求反

++x

前增量

--x

前减量

(T)x

显式将x转换为类型T

乘除

x * y

乘法

x / y

除法

x % y

求余

加减

x + y

加法、字符串串联、委托组合

x – y

减法、委托移除

移位

x << y

左移

x >> y

右移

关系和类型检测

x < y

小于

x > y

大于

x <= y

小于或等于

x >= y

大于或等于

x is T

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

x as T

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

相等

x == y

等于

x != y

不等于

逻辑AND

x & y

整型按位 AND布尔逻辑 AND

逻辑XOR

x ^ y

整型按位 XOR布尔逻辑 XOR

逻辑OR

x | y

整型按位 OR布尔逻辑 OR

条件AND

x && y

仅当xtrue才对y求值

条件OR

x || y

仅当xfalse 才对 y求值

条件

x ? y : z

如果xtrue则对y求值如果xfalse则对z求值

赋值

x = y

赋值

x op= y

复合赋值支持的运算符有

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


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

throwtry
语句

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

checkedunchecked语句

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");
    }
}


 六 类成员

成员

说明

常量

与类关联的常数值

字段

类的变量

方法

类可执行的计算和操作

属性

与读写类的命名属性相关联的操作

索引器

与以数组方式索引类的实例相关联的操作

事件

可由类生成的通知

运算符

类所支持的转换和表达式运算符

构造函数

初始化类的实例或类本身所需的操作

析构函数

在永久丢弃类的实例之前执行的操作

类型

类所声明的嵌套类型


七 可访问性
 

可访问性

含义

public

访问不受限制

protected

访问仅限于此类和从此类派生的类

internal

访问仅限于此程序

protected internal

访问仅限于此程序和从此类派生的类

private

访问仅限于此类


八 函数成员
 

public class List
{

    const int defaultCapacity = 4;

常量

    object[] items;
    int count;

字段

    public List(): List(defaultCapacity) {}

    public List(int capacity) {
       items = new object[capacity];
    }

构造函数

    public int Count {
       get { return count; }
    }

    public string Capacity {
       get {
           return items.Length;
       }
       set {
           if (value < count) value = count;
           if (value != items.Length) {
              object[] newItems = new object[value];
              Array.Copy(items, 0, newItems, 0, count);
              items = newItems;
           }
       }
    }

属性

    public object this[int index] {
       get {
           return items[index];
       }
       set {
           items[index] = value;
           OnListChange();
       }
    }

索引器

    public void Add(object item) {
       if (count == Capacity) Capacity = count * 2;
       items[count] = item;
       count++;
       OnChanged();
    }

    protected virtual void OnChanged() {
       if (Changed != null) Changed(this, EventArgs.Empty);
    }

    public override bool Equals(object other) {
       return Equals(this, other as List);
    }

    static bool Equals(List a, List b) {
       if (a == null) return b == null;
       if (b == null || a.count != b.count) return false;
       for (int i = 0; i < a.count; i++) {
           if (!object.Equals(a.items[i], b.items[i])) {
              return false;
           }
       }
      return true;
    }

方法

    public event EventHandler Changed;

事件

    public static bool operator ==(List a, List b) {
       return Equals(a, b);
    }

    public static bool operator !=(List a, List b) {
       return !Equals(a, b);
    }

运算符

}

posted on 2007-12-27 15:13  hemman  阅读(961)  评论(0)    收藏  举报

导航