C#基础(二)

1)const和static readonly区别:两者都是只读属性,通过类进行访问,初始化后不可以修改。两者的区别是const是在编译期初始化,static readonly是在运行期初始化。

2)数据类型分类:数据类型分为值类型和引用类型;值类型包括整数类型,布尔类型,实数类型,字符类型,结构类型和枚举类型。引用类型包括类,代表,数组和接口类型。值类型的变量总是直接包含着自身的数据,引用类型的变量是指向实际数据的地址。

3)装箱和拆箱:装箱和拆箱操作使得任何值类型和引用类型能够和对象类型之间进行转换;装箱是把一个值类型隐式地转换成为一个对象类型,被装箱的类型的值是作为一个拷贝赋给对象的。拆箱是把一个对象类型显式地转换成一个值类型。

4)is和as操作符:is操作符取两个操作数,左边是对一个对象的引用,右边是一个类型名称。如果左边的对象是右边的类型,返回true。as操作符取一个对象和一个类型作为其左边和右边的操作数,如果转换成功,就返回转换成功的结果,如果转换失败,则返回Null。

5)foreach的用法:

View Code
 1 using System;
2 class Test
3 {
4 static void Main()
5 {
6 int[] arr = new int[] { 0, 1, 2 };
7 foreach (int i in arr)
8 Console.WriteLine(i);
9 Console.ReadLine();
10 }
11 }

6)不固定长度数组的输入:

View Code
 1 using System;
2 class Test
3 {
4 static void PrintArr(int ArrLength)
5 {
6 int[] arr = new int[ArrLength];
7 for (int i = 0; i < arr.Length; i++)
8 arr[i] = i;
9 Console.WriteLine("Print Array's value");
10 for (int i = 0; i < arr.Length; i++)
11 Console.WriteLine("arr[{0}]={1}", i, arr[i]);
12 }
13 static void Main()
14 {
15 int i=1;
16 while(i>0)
17 {
18 Console.WriteLine("please enter the array's length:");
19 i=Int32.Parse(Console.ReadLine());
20 PrintArr(i);
21 }
22 }
23 }

7)ArrayList的使用:

View Code
 1 using System;
2 using System.Collections;
3 class ArrList
4 {
5 static void Main()
6 {
7 ArrayList arr = new ArrayList();
8 string str1;
9 while (true)
10 {
11 Console.WriteLine("Please add a string to ArrayList:");
12 str1 = Console.ReadLine();
13 if (str1 == "end")
14 break;
15 arr.Add(str1);
16 Console.WriteLine();
17 for (int i = 0; i < arr.Count; i++)
18 Console.Write("{0}", arr[i]);
19 Console.WriteLine("\n");
20 }
21 }
22 }

8)二维数组的使用:

View Code
 1 using System;
2 class Matrix
3 {
4 static void Main()
5 {
6 int[,] arr = new int[4, 6];
7 for (int i = 0; i < 4; i++)
8 {
9 for (int j = 0; j < 6; j++)
10 {
11 arr[i, j] = (i + 1) * 10 + j + 1;
12 }
13 }
14 for (int i = 0; i < 4; i++)
15 {
16 for (int j = 0; j < 6; j++)
17 {
18 Console.Write("{0} ", arr[i, j]);
19 }
20 Console.WriteLine();
21
22 }
23 Console.ReadLine();
24 }
25 }

9)命名空间的使用:

View Code
 1 using System;
2 namespace CG
3 {
4 class Test
5 {
6 static void Main()
7 {
8 A.PrintName a = new A.PrintName();
9 a.intro();
10 B.PrintName b = new B.PrintName();
11 b.intro();
12 Console.ReadLine();
13 }
14 }
15 }
16 namespace A
17 {
18 public class PrintName
19 {
20 public void intro()
21 {
22 Console.WriteLine("My name is A");
23 }
24 }
25 }
26 namespace B
27 {
28 public class PrintName
29 {
30 public void intro()
31 {
32 Console.WriteLine("My Name is B");
33 }
34 }
35 }

10)字符串的读取:

View Code
 1 using System;
2 class Method
3 {
4 static void Main()
5 {
6 string TempName = "";
7 while (TempName != "end")
8 {
9 TempName = Console.ReadLine();
10 MyMethod(TempName);
11 }
12 }
13 public static void MyMethod(string AName)
14 {
15 Console.WriteLine("The Name is " + AName + "\n");
16
17 }
18 }

11)值类型,引用类型,输出类型的参数输出:

View Code
 1 using System;
2 class Method
3 {
4 public static void ValueMethod(int i)
5 {
6 i++;
7 }
8 public static void ReferenceMethod(ref int i)
9 {
10 i++;
11 }
12 public static void OutputMethod(out int i)
13 {
14 i = 1;
15 i++;
16 }
17 static void Main()
18 {
19 int i = 0;
20 ValueMethod(i);
21 Console.WriteLine("i=" + i);
22 int j = 0;
23 ReferenceMethod(ref j);
24 Console.WriteLine("j=" + j);
25 int k;
26 OutputMethod(out k);
27 Console.WriteLine("k=" + k);
28 Console.ReadLine();
29 }
30 }

12)不定参数求和:

View Code
 1 using System;
2 class Method
3 {
4 static int addi(params int[] values)
5 {
6 int sum=0;
7 foreach(int i in values)
8 sum+=i;
9 return sum;
10 }
11 static void Main()
12 {
13 Console.WriteLine(addi(1,2,3,4));
14 Console.ReadLine();
15 }
16 }

13)显示ascII表:

View Code
 1 using System;
2 class Test
3 {
4 static void Main()
5 {
6 for (int i = 0; i < 128; i++)
7 {
8 if (i % 10 == 0)
9 {
10 Console.WriteLine();
11 }
12 Console.Write("{0,3}:{1,-3}", i, (char)i);
13
14 }
15 Console.ReadLine();
16 }
17 }
posted @ 2011-08-04 17:19  zhuhaorain  阅读(303)  评论(0编辑  收藏  举报