当前位置: 网站首页 > .NET > c#

Visual C# .NET 入门:步骤 5. 使用数组

时间:1970-1-1 08:33:31来源: c#作者:admin 点击:2次 字体 [ С]

    在对从输入读取的行进行排序之前,程序需要将其存储到一个数组中。我们将简要讨论可实现对象数组的 .NET 基类的用法。

    修改源代码更改 C# 源文件 (class1.cs),如下面以斜体突出显示的代码所示。其他的差异(如类名)可忽略不计。

// Import namespaces
using System;
using System.Collections;
// Declare namespace
namespace MsdnAA
{
    // Declare application class
    class QuickSortApp
    {
        // Application initialization
        static void Main (string[] szArgs)
        {
            // Describe program function
            Console.WriteLine ("QuickSort C#.NET Sample Application\n");
            // Prompt user for filenames
            Console.Write ("Source: ");
            string szSrcFile = Console.ReadLine ();
            Console.Write ("Output: ");
            string szDestFile = Console.ReadLine ();
            // TODO: Read contents of source file
            ArrayList szContents = new ArrayList ();
        }
    }
}

    使用 ArrayList 类我们将导入 System.Collections 命名空间,这样我们就可以直接引用 ArrayList.此类实现大小可动态调整的对象数组。要插入新的元素,可以简单地将对象传递到 ArrayList 类的 Add() 方法。新的数组元素将引用原始的对象,而垃圾回收器将处理它的释放。

发表评论
验证码:
最新评论