IT学习者 | 文章大全 | 技术文档 | 桌面壁纸 | 实用查询 | 网络电台 | 成语 | 歇后语 | 网址 | 下载 | 周公解梦 | 生日密码 | 电视剧365 | Flash
 您现在的位置: IT学习者 >> 文章大全 >> 网络编程 >> ASP.NET

C#速成(Quick C#)

【 作者:bigqiang翻译    来源:http://www.fazhuan.com/  更新时间:2004-7-27 | 字体:


-------------------
语句
-------------------

除了对某些新增语句和对某些语句的修改以外,C#语句与C++非常相象。

下面是新增的语句:

foreach
用于循环依次访问集合元素,比如象数组等。

示例:

    foreach (string s in array)
        Console.WriteLine(s);


lock
用于锁住代码块,使线程在临界争区内,别的线程无法进入锁定的临界区。


checked/unchecked
用于数值运算中的溢出检测。

示例:

int x = Int32.MaxValue; x++;    // 溢出检测
{
x++;                            // 异常
}
unchecked
{
x++;                            // 溢出}
}


下面的语句在C#当中已经被修改:

执行一个case语句后,程序流程不允许跳到下一个相邻case语句。这在C++当中是被允许的。

示例:
int var = 100;{
    case 100: Console.WriteLine("<Value is 100>");
        // 没有break语句
    case 200: Console.WriteLine("<Value is 200>"); break;
}
C++编译后的输出:
    <Value is 100><Value is 200>

C#下,编译时会报错:
error CS0163: Control cannot fall through from one case label
('case 100:') to another

但是你仍然能做C++类似的事{
    case 100:
    case 200: Console.WriteLine("100 or 200<VALUE is 200>");
              break;
}

你也可以常数变量作为case 的值:
示例:

const string WeekEnd  = "Sunday";
const string WeekDay1 = "Monday";

....

string WeekDay = Console.ReadLine();{
case WeekEnd: Console.WriteLine("It's weekend!!"); break;
case WeekDay1: Console.WriteLine("It's Monday"); break;

}




-------------------
委托
-------------------
委托让我们把一个函数引用存储在一个变量里。C++当中,这类似于使用typedef定义的函数指针,我们通常用存储一个函数指针。

声明委托使用的关键字是 delegate。瞧瞧这个示例,你会理解什么是委托:

示例:

delegate int Operation(int val1, int val2);
public int Add(int val1, int val2)
{
    return val1 + val2;
}
public int Subtract (int val1, int val2)
{
    return val1- val2;
}

public void Perform()
{
    Operation Oper;
    Console.WriteLine("Enter + or - ");
    string optor = Console.ReadLine();
    Console.WriteLine("Enter 2 operands");

    string opnd1 = Console.ReadLine();
    string opnd2 = Console.ReadLine();

    int val1 = Convert.ToInt32 (opnd1);
    int val2 = Convert.ToInt32 (opnd2);

    if (optor == "+")
        Oper = new Operation(Add);
    else
        Oper = new Operation(Subtract);
        
    Console.WriteLine(" Result = {0}", Oper(val1, val2));
}





-------------------
继承和多态
-------------------
C#仅允许单继承,多继承要通过接口来实现。

示例:

class Parent{
}

class Child : Parent





-------------------
虚拟方法
-------------------
除了在子类中实现虚拟方法采用override关键字外,虚拟方法实现多态的概念C#与C++相同。父类使用相同的virtual关键字。从重载虚拟方法的每个类都要使用override关键字。

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Shape.Draw")    ;
    }
}

class Rectangle : Shape

{
    public override void Draw()
    {
        Console.WriteLine("Rectangle.Draw");
    }
}

class Square : Rectangle
{
    public override void Draw()
    {
        Console.WriteLine("Square.Draw");
    }
}
class MainClass
{
    static void Main(string[] args)
    {
        Shape[] shp = new Shape[3];
        Rectangle rect = new Rectangle();
        
        shp[0] = new Shape();
        shp[1] = rect;
        shp[2] = new Square();

        shp[0].Draw();
        shp[1].Draw();
        shp[2].Draw();
    }
}

输出t:
Shape.Draw
Rectangle.Draw
Square.Draw



-------------------
使用"new"来隐藏父方法
-------------------
你可以定义一个子类成一个新方法版本,隐藏基类当中的那个版本。使用new关键字就可以定义一个新版本。思考下面的示例,它是上面示例的修改后的版本。注意当我用Rectangle类中的new关键字代替override关键字时示例的输出情况。

class Shape
{
    public virtual void Draw()
    {
        Console.WriteLine("Shape.Draw")    ;
    }
}

class Rectangle : Shape
{
    public new void Draw()
    {
        Console.WriteLine("Rectangle.Draw");
    }
}
class Square : Rectangle
{
    //没在这里让你重载
    public new void Draw()
    {
        Console.WriteLine("Square.Draw");
    }
}
class MainClass
{
    static void Main(string[] args)
    {
        Console.WriteLine("Using Polymorphism:");
        Shape[] shp = new Shape[3];
        Rectangle rect = new Rectangle();

        shp[0] = new Shape();
        shp[1] = rect;
        shp[2] = new Square();

        shp[0].Draw();
        shp[1].Draw();
        shp[2].Draw();

        Console.WriteLine("Using without Polymorphism:");
        rect.Draw();
        Square sqr = new Square();
        sqr.Draw();
    }
}

输出:
Using Polymorphism
Shape.Draw
Shape.Draw
Shape.Draw
Using without Polymorphism:
Rectangle.Draw
Square.Draw

这里的多态性不会把Rectangle类的Draw方法当做Shape的Draw方法多态性的一种表现。相反,它会认为这是一种不同的方法。因此,为了避免父类与子类间的命名冲突,我们使用了new修饰符。

注意:你不能使用同一类下面一种方法的两个版本,即一个是用new修饰符的版本,另一个是用override或virtual修饰符的版本。正象上面示例所说明的,我不能再在拥有virtual或override方法的Rectangle类中添加另一个命名为Draw的方法。同样地,在Square类中,我也不能重载Square类的虚拟的Draw方法。

文章共6页,当前在第5页 9 7 [1] [2] [3] [4] [5] [6] 8 :
相 关 文 章
相 关 软 件
逃生 放生 黄玫瑰 想太多 那滋味 擦肩而过 放手去爱 北京欢迎你 依然在一起 吻得太逼真 感动天感动地 坐上火车去拉萨 怎么会狠心伤害我
心碎 冲动 小太阳 别碰我 蒲公英 千山万水 改变自己 一定要爱你 等爱的玫瑰 陷入爱里面 北极星的眼泪 最后一次的温柔 亲爱的那不是爱情
光荣 火花 坏女人 日不落 樱花草 为你写诗 独家记忆 夏天的味道 寂寞才说爱 忘不掉的伤 爱上你是个错 第三者的第三者 地球人都知道我爱你
假如 相思 是非题 有缘人 舍不得 我的答铃 死而无憾 外滩十八号 越爱越难过 123木头人 和寂寞说分手 爱上你是我的错 爱情里没有谁对谁错
加入收藏留言建议自助友情链接普通友情链接站长的Blog
版权所有   COPYRIGHT 2002-2008 ★IT学习者★ ALL RIGHTS RESERVED.