c#方法学习部分
1參數數組的使用?關鍵字params?類似于可變長數組和可選參數使用格式如下
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
?
namespace?ConsoleApplication4
{
????class?methodStample
????{
????????static?void?Main(string[]?args)
????????{
????????????methodStample?ms?=?new?methodStample();
????????????ms.DoSomething("a");
????????????ms.DoSomething("b",1);
????????????ms.DoSomething("c",1,2);
????????????int[]?array?=?{?1,?2,?3,?4?};
????????????ms.DoSomething("d",array);
???????????
???????????
????????}
????????public?void?DoSomething(string?str,?params?int[]?values)?{
????????????if?(values?!=?null?&&?values.Length?>?0)
????????????{
????????????????for?(int?i?=?0;?i?<?values.Length;?i++)
????????????????{
????????????????????Console.WriteLine("{0},{1}",?str,?values[i]);
????????????????}
?
????????????}
????????????else?{
????????????????Console.WriteLine(str);
????????????}
????????}
???????
???????
????????
????}
?
}
?
2可選參數,命名參數,方法重載,輸出參數,值傳遞參數,引用傳遞參數
3棧幀是編譯器用來實現方法調用的一種數據結構是方法調用時的表示方法的載體,函數調用的所有調用信息保存在對應的棧幀中,其中棧幀保存的信息主要有:
方法參數
方法中的局部變量
方法執行完后?的返回地址
獲取文件中包含的執行代碼的行號和列號
獲取執行的代碼?的文件名
、
。。。。。。
4遞歸
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
?
namespace?ConsoleApplication4
{
????class?methodStample
????{
????????static?void?Main(string[]?args)
????????{
????????????methodStample?ms?=?new?methodStample();
????????????int?result?=?ms.factorial(5);
????????????Console.WriteLine(result);
???????????
???????????
???????????
????????}
?
????????public?int?factorial(int?n)?{
????????????if?(n?==?1)
????????????{
????????????????return?1;
????????????}
????????????else?{
????????????????return?n?*?factorial(n-1);
????????????}
????????}
???????
???????
????????
????}
?
}
?
使用遞歸時必須要有明確的遞歸結束條件即遞歸出口否則造成死鎖
遞歸包含邊界條件,遞歸前進段,遞歸返回段,當邊界條件不滿足時遞歸前進,當邊界條件滿足時遞歸返回。遞歸每一次調用都會生成新的棧幀因此容易造成溢出內存,同時遞歸運行效率較低。
5方法的重載中包含參數順序的?不同,參數修飾符的不同但是返回值不同不能作為區別方法重載的條件
6靜態方法:
不屬于特定的對象屬于類方法,只能使用類名.靜態方法的形式調用靜態方法,不能使用對象調用靜態方法。
只能訪問靜態變量不能訪問實例變量
不用創建類的對象就可以訪問靜態方法
靜態方法中不能使用this關鍵字引用類的當前實例
非靜態方法可以訪問靜態成員變量也可以訪問非靜態成員變量同時可以調用靜態方法,在實例方法中調用靜態方法很常見類似于調用工具方法。
posted on 2012-11-21 11:34 moonfans 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/moonfans/archive/2012/11/21/2780414.html
總結
- 上一篇: Codeigniter 用户登录注册模块
- 下一篇: c# char unsigned_dll