C#:数组, 命名空间, Main()方法

1. 数组(引用类型):

  • 一维数组

声明数组: t[] intrgers;

初始化特定大小的数组: int[] integers = new int[3];

为数组的每个元素赋值: int[] integers = new int[3] {1, 2, 3};

  • 多维数组

int[,] twoDim = new int[3, 3];

twoDim={{1,2,3},{2,3,4},{3,4,5}};

  • 锯齿数组:每一行可以有不同的大小

在初始化锯齿数组时,先设置该数组包含的行数,定义各行中元素个数的第二个括号设置为空,因为这类数组的每一行包含不同的元素数。之后,为每一行指定行中的元素个数:

 1             int[][] jagged = new int[3][];
 2             jagged[0] = new int[2] { 1, 2 };
 3             jagged[1] = new int[3] { 1, 2, 3 };
 4             jagged[2] = new int[4] { 2, 3, 4, 5 };
 5 
 6             for (int i = 0; i < jagged.Length; i++)
 7             {
 8                 for (int j = 0; j < jagged[i].Length; j++)
 9                 {
10                     Console.WriteLine("Row: {0}, Element: {1}, Value: {2}", i, j, jagged[i][j]);
11                     Console.ReadLine();
12                 }
13             }

 

2. 命名空间:

using关键字的一个用途: 给类和命名空间指定别名。语法如下:using alias= NamespaceName;

注意:命名空间别名的修饰符是“::”。

using System;

using Introduction = Wrox.ProCSharp.Basics;

class Test

{

public static int Main()

{

Introduction::NamespaceExample NSEx = new Introduction::NamespaceExample();

Console.WriteLine(NSEx.GetNamespace());

return 0;

}

namespace Wrox.ProCSharp.Basics

{

class NamespaceExample

{

public string GetNamespace()

{

return this.GetType().Namespace;

}

}

3. Main() 方法:

  •  Main方法必须是类或者结构的静态方法,并且其返回类型必须是int或void。
  • 多个Main()方法时, 用/main 指定Main方法的位置:/main:class

 编译 t2.cst3.cs,并指定将在 Test2 中找到 Main 方法:csc t2.cs t3.cs /main:Test2

Main() 方法的参数:

using System;

namespace wROX.ProCSharp.Basics

{

class AgrsExample

{

publicstaticint Main(string[] args)

{

for (int i = 0; i < args.Length; i++)

{

Console.WriteLine(args[i]);

}

Console.ReadLine();

return 0;

}

}

 在运行编译好的可执行文件时,可以在程序名的后面加上参数,例如:ArgsExample /a /b /c

则输出:/a

    /b

    /c

 

posted on 2012-10-23 17:32  LilianChen  阅读(1934)  评论(2编辑  收藏  举报

导航