天天看點

C# 學習之函數

C#學習之函數總結

1:函數結構同C++,java類似,

<修飾符> <傳回類型> <函數名字> (參數清單)

{//函數體

}

2:參數中的特性--參數數組

C#中允許函數中指定一個特定的參數,這個參數必須是函數定義中的最後一個參數,毫無疑問這些參數必須是同類型的,這樣的參數叫做參數數組

格式:   <modify> <return type> <functionName>(type1,typ2,...paras typen[] <paraName>)

如:

static int sumAdd(paras int[] datas)
{
	int sum = 0;	
	foreach(int x in datas)
		sum += x;
	return sum;


}


static void main()
{
	sunAdd(1,2,3,4,5);
}
           

3: 參數的類型,ref

C#中有兩種類型的參數--傳值參數,傳引用參數,其中傳引用參數在調用的時候需要加上一個關鍵詞 ref,這個和C++還是有差別的

C#--

static void Main(string[] args)
        {
            Console.WriteLine("I love C&&");
            int val = 10;
            Console.WriteLine("val's value is:{0}",val);
            changeValue(ref val);
            Console.WriteLine("val's value is:{0}",val);
        }


        static int changeValue(ref int val)
        {
            return val *= 2;
        }
           

C++--

void changeValue(int &val)
{
	val *= 2;
}
void main()
{
	int val = 10;
	cout<<"val's value is:"<<val<<endl;
	changeValue(val);
	cout<<"val's value is:"<<val<<endl;
}
           

4: 輸出參數 out

這個是C++中沒有的,不過在HLSL中有類似的文法,out是一個關鍵字,其用法同ref,作用也類似ref,但是跟ref相比,它的不同之處是:在傳入時候,可以未指派.

static void outChange(int x, out int y)
{
     y = 2 * x;
}


static void Main(string[] args)
{
    int x = 100;
    int y;
    outChange(x,out y);
    Console.WriteLine("out y's value is:{0}",y);
}
           

5: Main函數

C#中的main函數格式

static void Main();

static void Main(string[] args);

static int Main();

static int Main(string[] args);

6: 委托特征

委托是C#相對C++的一種新的文法,委托類似于C/C++中的函數指針,但是相比指針,它更安全和強大。

委托的特點:

<1> 委托允許作為函數參數進行傳遞

<2> 委托可用于定義回調方法

<3> 委托可以連接配接在一起,對同一事件調用多個響應方法。

7:委托使用

namespace ConsoleApplication1
{
    class Program
    {
        public delegate void delFunc(string str);   // 委托的聲明


        public static void testDelOne(string str)   //定義委托對應的函數1
        {
            Console.WriteLine("This is delegate one:" + str);
        }


        public static void testDelTwo(string str)   //定義委托對應的函數2
        {
            Console.WriteLine("This is delegate two:" + str);
        }


        public static void testDelThree(string str)  //定義委托對應的函數3 
        {
            Console.WriteLine("This is delegate three:" + str);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("**************Test delegate use like C function pointer***************");
            delFunc fp = testDelOne;
            fp(" in 1.1");
            delFunc xxpp = new delFunc(testDelThree);
            xxpp("in 1.3");


            Console.WriteLine("****************************Test delegate +=**************************");
            fp += testDelTwo;
            fp(" in 2");


            Console.WriteLine("****************************Test delegate + **************************");
            delFunc fpp = testDelThree;
            //delFunc fpAll = fp + fpp;
            delFunc fpAll = fp + testDelThree;
            fpAll(" in 3");


            Console.WriteLine("****************************Test delegate -=*************************");
            fpp -= testDelTwo;
            fpp("in fpp 2");


            fpAll -= testDelTwo;
            fpAll("in fpAll2");


            Console.WriteLine("****************************Test delegate -**************************");
            fpAll = fpAll - testDelOne;
            fpAll(" in last");


        }


    }
}
           

8: 委托的執行個體化

delegate void del(string str);
void funcName(string str){//....}
           

<1> named method : del fp = funcName;

<2> anonymous method del fp = delegate(string str){//...};

<3> Lamda expression del fp = (x)->{//.....};