C# Keynote

C# Keynote

1、 Main 方法必须包含在一个类内,参数类型、返回值类型可以有多种变化。

 1 // Hello1.cs
 2 public class Hello1
 3 {
 4    public static void Main()
 5    {
 6       System.Console.WriteLine("Hello, World!");
 7    }
 8 }
 9 
10 // Hello3.cs
11 // arguments: A B C D
12 using System;
13 
14 public class Hello3
15 {
16    public static void Main(string[] args)
17    {
18       Console.WriteLine("Hello, World!");
19       Console.WriteLine("You entered the following {0} command line arguments:",
20          args.Length );
21       for (int i=0; i < args.Length; i++)
22       {
23          Console.WriteLine("{0}", args[i]); 
24       }
25    }
26 }
27 
28 // Hello4.cs
29 using System;
30 
31 public class Hello4
32 {
33    public static int Main(string[] args)
34    {
35       Console.WriteLine("Hello, World!");
36       return 0;
37    }
38 }
View Code

2、通过foreach语句来遍历容器。

 1 // cmdline2.cs
 2 // arguments: John Paul Mary
 3 using System;
 4 
 5 public class CommandLine2
 6 {
 7    public static void Main(string[] args)
 8    {
 9        Console.WriteLine("Number of command line parameters = {0}",
10           args.Length);
11        foreach(string s in args)
12        {
13           Console.WriteLine(s);
14        }
15    }
16 }
View Code

3、C# 数组从零开始建立索引,声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。

 

  另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。这使您可以生成Heap上的数组。

4、声明多维数组,以及数组的数组.

 1 //多维数组
 2 string[,] names;
 3 
 4 //数组的数组
 5 byte[][] scores;
 6 
 7 int[] numbers = new int[5];
 8 string[,] names = new string[5,4];
 9 
10 //初始化数组的数组
11 byte[][] scores = new byte[5][];
12 for (int x = 0; x < scores.Length; x++) 
13 {
14    scores[x] = new byte[4];
15 }
View Code

5、数组的多种初始化.

 1 // 初始化一维数组
 2 int[] numbers = new int[5] {1, 2, 3, 4, 5};
 3 string[] names = new string[3] {"Matt", "Joanne", "Robert"};
 4 
 5 int[] numbers = new int[] {1, 2, 3, 4, 5};
 6 string[] names = new string[] {"Matt", "Joanne", "Robert"};
 7 
 8 int[] numbers = {1, 2, 3, 4, 5};
 9 string[] names = {"Matt", "Joanne", "Robert"};
10 
11 // 初始化二维数组
12 int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
13 string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
14 
15 int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
16 string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
17 
18 int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
19 string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
20 
21 // 初始化交错数组
22 int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
23 
24 int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
25 
26 int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
View Code

6、访问数组成员.

1 int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
2 numbers[1, 1] = 5;
3 
4 // 访问交错数组
5 numbers[0][0] = 58;
6 numbers[1][1] = 667;
View Code

7、在 C# 中,数组实际上是对象。System.Array 是所有数组类型的抽象基类型。多维数组也可以使用foreach来访问.

1 int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
2 foreach(int i in numbers)
3 {
4    Console.Write("{0} ", i);
5 }
View Code

8、通过以下方式定义属性.

 1     private string myName ="N/A";
 2     private int myAge = 0;
 3 
 4     // Declare a Name property of type string:
 5     public string Name
 6     {
 7         get 
 8         {
 9            return myName; 
10         }
11         set 
12         {
13            myName = value; 
14         }
15     }
16 
17     // Declare an Age property of type int:
18     public int Age
19     {
20         get 
21         { 
22            return myAge; 
23         }
24         set 
25         { 
26            myAge = value; 
27         }
28     }
View Code

参考:http://msdn.microsoft.com/zh-cn/library/aa288453(v=vs.71).aspx

原文地址:https://www.cnblogs.com/tekkaman/p/3537851.html