博文

C# 程序员参考--集合类教程(2006-09-03 18:10:00)

摘要:本教程展示如何实现可与 foreach 语句一起使用的集合类。 教程 foreach 语句是循环访问数组元素的方便方法。如果集合类已实现 System.Collections.IEnumerator 和 System.Collections.IEnumerable 接口,它还可以枚举该集合的元素。 示例 1 下面的代码示例阐释如何编写可与 foreach 一起使用的集合类。该类是字符串标记化拆分器,类似于 C 运行时库函数 strtok。// tokens.cs using System; // The System.Collections namespace is made available: using System.Collections; // Declare the Tokens class: public class Tokens : IEnumerable { private string[] elements; Tokens(string source, char[] delimiters) { // Parse the string into tokens: elements = source.Split(delimiters); } // IEnumerable Interface Implementation: // Declaration of the GetEnumerator() method // required by IEnumerable public IEnumerator GetEnumerator() { return new TokenEnumerator(this); } // Inner class implements IEnumerator interface: private class TokenEnumerator : IEnumerator { private int position = -1; private Tokens t; public TokenEnumera......

阅读全文(1529) | 评论:0

C# 程序员参考--版本控制教程(2006-09-03 18:08:00)

摘要:本教程使用 override 和 new 关键字来演示 C# 中的版本控制。版本控制在基类和派生类衍生时维护它们之间的兼容性。 教程 C# 语言被设计为不同库中的基类和派生类之间的版本控制可以衍生,并保持向后兼容。例如,这意味着在基类中引入与派生类中的某个成员名称相同的新成员不是错误。它还意味着类必须显式声明某方法是要重写一个继承方法,还是一个仅隐藏具有类似名称的继承方法的新方法。 在 C# 中,默认情况下方法不是虚拟的。若要使方法成为虚拟方法,必须在基类的方法声明中使用 virtual 修饰符。然后,派生类可以使用 override 关键字重写基虚拟方法,或使用 new 关键字隐藏基类中的虚拟方法。如果 override 关键字和 new 关键字均未指定,编译器将发出警告,并且派生类中的方法将隐藏基类中的方法。下面的示例在实际操作中展示这些概念。 示例// versioning.cs // CS0114 expected public class MyBase { public virtual string Meth1() { return "MyBase-Meth1"; } public virtual string Meth2() { return "MyBase-Meth2"; } public virtual string Meth3() { return "MyBase-Meth3"; } } class MyDerived : MyBase { // Overrides the virtual method Meth1 using the override keyword: public override string Meth1() { return "MyDerived-Meth1"; } // Explicitly hide the virtual method Meth2 using the new // keyword: public new string Meth2() { return "MyDer......

阅读全文(1049) | 评论:0

C# 程序员参考--库教程(2006-09-03 18:08:00)

摘要:本教程展示如何在 C# 中创建和使用库。
教程 本教程演示如何使用必要的编译器选项创建托管 DLL 文件,以及如何通过客户程序使用该库。 示例 本示例使用下列模块: DLL 库 (Functions.dll),从以下源文件生成: Factorial.cs:计算并返回某数的阶乘。 DigitCounter.cs:计算所传递的字符串中的数字位数。 使用该 DLL 的客户程序 (FunctionTest.exe),从源文件 FunctionClient.cs 进行编译。本程序显示输入参数的阶乘。 生成库 若要生成该库,请将 Functions 作为当前目录,并在命令提示处键入下列内容:csc /target:library /out:Functions.dll Factorial.cs DigitCounter.cs 其中: /target:library 指定输入文件是个 DLL,而不是可执行文件(这还将阻止编译器查找默认入口点)。 /out:Functions.dll 指定输出文件名是 Functions.dll。通常,输出名与命令行上的第一个 C# 源文件(本示例中为 Factorial)的名称相同。 Factorial.cs DigitCounter.cs 指定要编译并放入 DLL 的文件。 编译客户程序 若要编译该程序,请将 FunctionTest 作为当前目录,并在命令提示处键入下列内容:copy ..\Functions\Functions.dll . csc /out:FunctionTest.exe /R:Functions.DLL FunctionClient.cs 其中: /out:FunctionTest.exe 指定输出文件名为 FunctionTest.exe。 /R:Functions.DLL 指定解析引用时必须包括 Functions.DLL。该 DLL 必须位于当前目录中,或具有完全限定的路径。 FunctionClient.cs 指定客户源代码。 这将创建可执行文件 FunctionTest.exe。 文件 1:Factorial.cs 以下代码计算传递给此方法(与“库”示例不同,请将此方法编译到库中)的整数......

阅读全文(1108) | 评论:0

C# 程序员参考--数组教程三(2006-09-03 18:07:00)

摘要:本教程展示属性是如何成为 C# 编程语言必不可少的一个组成部分的。它阐释如何声明和使用属性。
教程 此教程包括两个示例。第一个示例展示如何声明和使用读/写属性。第二个示例演示抽象属性并展示如何在子类中重写这些属性。 示例 1 本示例展示一个 Person 类,它有两个属性:Name (string) 和 Age (int)。两个属性都是读/写属性。// person.cs using System; class Person { private string myName ="N/A"; private int myAge = 0; // Declare a Name property of type string: public string Name { get { return myName; } set { myName = value; } } // Declare an Age property of type int: public int Age { get { return myAge; } set { myAge = value; } } public override string ToString() { return "Name = " + Name + ", Age = " + Age; } public static void Main() { Console.WriteLine("Simple Properties"); // Create a new Person object: Person person = new Person(); ......

阅读全文(1614) | 评论:0

C# 程序员参考--数组教程三(2006-09-03 18:07:00)

摘要:多维数组
     int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
     string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
    可省略数组的大小,如下所示:      int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
     string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
    如果提供了初始值设定项,则还可以省略 new 运算符,如下所示:      int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
     string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };

交错的数组(数组的数组)
    可以像下例所示那样初始化交错的数组:      int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    可省略第一个数组的大小,如下所示:      int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    -或-      int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    请注意,对于交错数组的元素没有初始化语法。

访问数组成员
    访问数组成员可以直接进行,类似于在 C/C++ 中访问数组成员。例如,下面的代码创建一个名为 numbers 的数组,然后向该数组的第五个元素赋以 5:      int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
     number......

阅读全文(1268) | 评论:0

C# 程序员参考--数组教程二(2006-09-03 18:06:00)

摘要:示例
    下面是一个完整的 C# 程序,它声明并实例化上面所讨论的数组。
    // arrays.cs
    using System;
    class DeclareArraysSample
    {
      public static void Main()
      {
         // Single-dimensional array
         int[] numbers = new int[5];          // Multidimensional array
         string[,] names = new string[5,4];          // Array-of-arrays (jagged array)
         byte[][] scores = new byte[5][];          // Create the jagged array
         for (int i = 0; i < scores.Length; i++)
         {
            scores[i] = new byte[i+3];
         }          // Print length of each row
         for (int i = 0; i < scores.Length; i++)
         {
            Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
         }
       }
     }
输出
    Length of row 0 is 3
    Length of row 1 is 4
    Length of row 2 is 5
    Length of row 3 is 6
    Length of row 4 is 7
初始化数组
    C# 通过将初始值括在大括号 ({}) 内为在声明时初......

阅读全文(1390) | 评论:0

C# 程序员参考--数组教程(2006-09-03 18:05:00)

摘要: 本教程描述数组并展示它们在 C# 中的工作方式。
教程
    本教程分为下述几节:     1.数组概述
    2.声明数组
    3.初始化数组
    4.访问数组成员
    5.数组是对象
    6.对数组使用 foreach
数组概述
    C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。
     声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。
    int[] table; // not int table[];
    另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。这使您可以声明一个数组并向它分配 int 对象的任意数组,而不管数组长度如何。
    int[] numbers; // declare numbers as an int array of any size
    numbers = new int[10]; // numbers is a 10-element array
    numbers = new int[20]; // now it's a 20-element array
声明数组
    C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。下面的示例展示如何声明不同类型的数组:     一维数组:
     int[] numbers;

    多维数组:
    string[,] names;

    数组的数组(交错的):
    byte[][] scores;
    声明数组(如上所示)并不实际创建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:     一维数组:
    int[] numbers = new int[5];

    多维数组:
    string[,] names = new string[5,4];

......

阅读全文(1410) | 评论:0

C# 程序员参考--命令行参数教程(2006-09-03 18:05:00)

摘要:本教程展示如何访问命令行以及访问命令行参数数组的两种方法。
教程
    下面的示例展示使用传递给应用程序的命令行参数的两种不同方法。 示例 1
    本示例演示如何输出命令行参数。
    // cmdline1.cs
    // arguments: A B C
    using System;     public class CommandLine
    {
       public static void Main(string[] args)
       {
          // The Length property is used to obtain the length of the array.
          // Notice that Length is a read-only property:
          Console.WriteLine("Number of command line parameters = {0}",
             args.Length);
          for(int i = 0; i < args.Length; i++)
          {
             Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
          }
       }
    }
输出
    使用如下所示的一些参数运行程序:cmdline1 A B C。
     输出将为:
    Number of command line parameters = 3
    Arg[0] = [A]
    Arg[1] = [B]
    Arg[2] = [C]
示例 2
    循环访问数组的另一种方法是使用 foreach 语句,如本示例所示。foreach 语句可用于循环访问数组或“.NET Framework”集合类。它提供了一种简单的方法来循环访问集合。
    // cmdline2.cs
    // argume......

阅读全文(1377) | 评论:0

C# 程序员参考--Hello World(2006-09-03 18:05:00)

摘要: 本教程展示以 C# 编写的 Hello World 程序的几个版本。
教程
    下面的示例展示编写 C#“Hello World”(世界你好)程序的几种不同方法。
示例 1
     // Hello1.cs
    public class Hello1
    {
      public static void Main()
      {
         System.Console.WriteLine("Hello, World!");
      }
    }
输出:
    Hello, World!
代码讨论
    每个 Main 方法都必须包含在一个类内(此处为 Hello1)。
    System.Console 类包含一个 WriteLine 方法,可用于向控制台显示字符串。
示例 2
    为避免程序中到处都是完全限定的类,可以使用 using 指令,如下所示:
    // Hello2.cs
    using System;     public class Hello2
    {
      public static void Main()
      {
        Console.WriteLine("Hello, World!");
      }
    }
输出
    Hello, World!
示例 3
    如果需要访问传递到应用程序中的命令行参数,则只需更改 Main 方法的签名以包括这些参数,如下所示。本示例对命令行参数进行计数并显示这些参数。
    // Hello3.cs
    // arguments: A B C D
    using System;     public class Hello3
    {
      public static void Main(string[] args)
      {  
         Console.WriteLine("Hello, World!");
         Console.W......

阅读全文(1395) | 评论:0

C#语言初级入门(4)(2006-09-03 18:02:00)

摘要:我们创建的第一个类是Shape。这是一个抽象类,因为我们不想创建这个类的实例,我们要创建的是它的派生类的实例。我们从所有的形状(圆、长方形、正方形)提取出共同特征到Shape类。Shape类有一个实例变量color,它的变量声明中带有protected修饰符。protected修饰符表示这个变量只能在类的内部或者该类的派生类中访问。紧接这个变量声明的就是Shape类的构造函数和存取方法getColor(),这两个方法都没有什么新的东西。最后一个方法getArea()加上了abstract修饰符,这是因为每一种不同的形状都有不同的面积计算方法,因此这个方法必须由各种形状自己来定义。    接下来的三个类Circle、Rectangle和Square都从Shape类派生,它们都具有Shape所描述的特征。这可以从它们的定义中看出来,它们的声明中都带有“public class:Shape {”,这个“: Shape”就表示当前的类从Shape类派生。由于这三个类都从Shape派生,它们自动拥有Shape中定义的所有public或者protected实例变量,即Circle、 Rectangle和Square包含了实例变量color。    每一个Sharp的派生类都有自己的构造函数,负责调用父类Shape的构造函数设置公共的实例变量(color)以及设置自己特有的实例变量。例如“public Circle(string color, double radius) : base(color)”这个语句中,“: base(color)”就表示用参数color调用父类的构造函数。    最后我们来看一下getArea()方法,它是一个多态性的示范。所有形状都有getArea()方法,但是根据对象是圆、长方形还是正方形,具体调用的方法也不同。    要运行这个例子,先把所有文件保存到同一目录,然后执行下面的命令:
csc /target:library /out:Shapes.dll
Shapes.cs Circle.cs Rectangle.cs Square.cs
   然后执行:
csc /reference:Shapes.dll Example3.cs
   现在,如果我们运行Example3......

阅读全文(1482) | 评论:0