C#基础知识的总结

1.数组
数组是引用类型,system.Array是所有数组类型的抽象基类型。数组使用需要初始化。
一维数组  声明:int[] numbers;
初始化指定它的长度,有两种 方法(1)int[] test=new int[5];
                        方法(2) int[] test2={1,2,3};
多维数组: 声明一个二维数组int[,] ints;int[,,] ints;即声一个三维数组
int[,] array=new int[2,3];初始化一个2行3列的数组
string[,] strs={{"wr","it","e"},{"mi","s","s"},{"l","i","ke"}};一个三行三列的数组
多维数组的每一行的元素数必须相等的。
多维数组的访问
foreach (string n in strs)
            {
                Console.WriteLine(n.ToString);
            }
数组的数组:
声明 
声明一个有3行的数组的数组,double[][] arrayDou=new double[3][];
切记后面的中括号里不能值任值,因为它的列是多少是不固定的,这里所说行列只是形象上的说明,数组的数组,顾名思义,它的每一行都又是一个数组,所以子数组里面的元素数是可以不一样的。
上面只声明了一个数组的数组,在使用请必须对它进行初始化,如下
  arrayDou[0]=new double[]{1.0,2.0,3.1};
            arrayDou[1]=new double[]{12.0,15.2,13.3};
            arrayDou[2] = new double[]{ 15.2,15.6,5.5,15.2};
这样才完整。还有另一种方式
int[][] intIns={new int[]{1,2,3},new int[]{2,2,4,6}};
数组的数组的访问
 foreach (int[] n in arrayDou)
            {
                foreach (int ns in n)
                {
                    Console.Write("{0}",ns);
                }
        Console.WriteLine();
              
            }
当然可以使用 arrayDou[2][3] 不超过索引就好了
2.参数数组
就是说在一个方法中传进来的参数是不定的情况下用的,
用关键字params
 static void Main()
    {

        int[] myarray = new int[3] {10,11,12};
        UseParams(myarray);
    }
  public static void UseParams(params int[] list)
    {
        for (int i = 0 ; i < list.Length; i++)
        {
            Console.WriteLine(list[i]);
        }
        Console.WriteLine();
    }
3.匿名方法
只有方法体,没有方法名字的方法,少写很多代码,简化委托的使用。
如下所示:
 class STest
    {
        delegate int testDelegate(int a, int b);
        public string Choice(string choice,int a,int b )
        {
            testDelegate td=null;
            if (choice=="zhouchang")
            {
                td=delegate(int a1,int b1 ){return  (a1+b1)*2;  };
            }
            else if(choice=="mianji")
            {

                td = delegate(int a2, int b2) { return a2 * b2; };
            }

            return td(a,b).ToString();
        }

    }
调用方代码:
Test a = new Test();
string s=a.Choice("mianji",3,5)
输出结果:15

先说到这吧!
posted @ 2007-11-09 08:51  慧致澜馨  阅读(509)  评论(0编辑  收藏  举报