函數(shù):
【函數(shù)就是方法,方法就是函數(shù)】【函數(shù)一定要寫在類里面】
函數(shù)的結(jié)構(gòu):
Static void 函數(shù)名 (數(shù)據(jù)類型 變量名) //Static為靜態(tài)的,PRivate只能在當(dāng)前類里面用;public是公共的,可以在整個(gè)命名空間使用
{
函數(shù)體;
}
函數(shù)的使用:找到包含該函數(shù)的類,定義一個(gè)變量初始化該類,由這個(gè)變量調(diào)用函數(shù)并實(shí)例化。
例如:
寫一個(gè)函數(shù)(在program類里面):
—————無返回值的函數(shù)
public void hanshu(int i) //可以在括號(hào)內(nèi)寫多個(gè)變量,用英文逗號(hào)隔開 { int he = 1; for (int j = 1; j <= i;j++ ) { he *= j; } Console.WriteLine("結(jié)果為:"+he); }
調(diào)用函數(shù):
Program p = new Program();p.hanshu(3);
—————有返回值的函數(shù)
Staticvoid函數(shù)名(數(shù)據(jù)類型變量名,out數(shù)據(jù)類型變量名)//Static后面除了void以外的類型就是帶返回值的,例如string、int、double,這種類型只能返回一 個(gè),要想返回多個(gè)就要在函數(shù)名的括號(hào)中 加out,要加在類型的前面,則返回該變量名
{
函數(shù)體;
}
例如:
Publicstringhanshu(inti,outintx) //此函數(shù)是帶返回值的,所以最后用return,如果最后接收的值大于兩個(gè),則在括號(hào)中類型前加out
{
x=0; //返回值x要進(jìn)行賦值
inthe=1;
for(intj=1;j<=i;j++)
{
he*=j;
}
x=he;
return"yes";
}
調(diào)用函數(shù):
staticvoidMain(string[]args)
{
Programa=newProgram();
intx=0; //函數(shù)中的x,和主函數(shù)中的x一樣,但也要定義x并賦值
strings=a.hanshu(6,outx);
Console.WriteLine(x+s);
Console.ReadLine();
函數(shù)前面加注釋:為了在以后用到此函數(shù)的時(shí)候不會(huì)忘記此函數(shù)的功能,需要加一些注釋,這樣在使用的時(shí)候鼠標(biāo)放到上面就會(huì)顯示寫好的注釋,使用的時(shí)候是在要寫注釋的函數(shù)上面輸入///,也就是三個(gè)斜杠,就會(huì)顯示如下 (在第二行輸入該函數(shù)的功能)
/// <summary> /// /// </summary> /// <param name="i"></param>
【例題】
#region==判斷郵箱== /// <summary> /// 判斷郵箱是否正確 /// </summary> /// <param name="semail"></param> /// <returns></returns> public bool yanzhengma(string semail) { string bidui = "abcdefghijklmnopqrstuvwxyz1234567890_.@"; int sant = 0; int sdian = 0; int zhengque = 0; for (int i = 0; i < semail.Length; i++) { string sc = semail.Substring(i, 1); bool b = bidui.Contains(sc); if (b) { if (sc == "@") { sant++; } if (sc == ".") { sdian++; } } else { //您的郵箱有特殊字符,請檢查輸入 zhengque++; return false; } } if (sant == 1 && sdian == 1 && zhengque == 0) { if (semail.Contains("@.") || semail.IndexOf("@") > semail.IndexOf(".")) { //@和.之間需要有郵箱地址代表字符,并且@必須在.前面 return false; } else { if (semail.EndsWith(".com") || semail.EndsWith(".cn")) { //輸入正確 return true; } else { //郵箱必須以.com或者.cn結(jié)尾 return false; } } } else { //注意郵箱中只能出現(xiàn)一個(gè)@和一個(gè). return false; } } #endregion
static void Main(string[] args) { Console.WriteLine("請輸入你的郵箱:"); string semail = Console.ReadLine(); semail = semail.ToLower(); Program qg = new Program(); bool b = qg.yanzhengma(semail); Console.WriteLine(b);
枚舉
枚舉enum
//枚舉里面是常量
enummeiju:uint//冒號(hào)后面指定索引的類型,只能是有符號(hào)無符號(hào)整型
{
one=3,//當(dāng)?shù)扔跀?shù)字的時(shí)候,是代表認(rèn)為指定索引
two=6,
three,
你好Word,
four=three,///當(dāng)?shù)扔谥坝械某A康臅r(shí)候,就是等于常量
}
(枚舉可以直接定義索引:定義one的索引是3,定義two的索引是6,則three的索引是7,后面的往下依次類推)
(數(shù)字不能放在枚舉中,枚舉就是放常量的字符串)
(準(zhǔn)許使用的枚舉類型有:byte、sbyte、short、ushort、int、uint、long或ulong)
(枚舉中定義完常量后用英文逗號(hào)結(jié)尾)
例如
enumhouxuanren
{
zhangsan=1,
lisi=2,
}
staticvoidMain(string[]args)
{
intzg=0,ls=0;
for(inti=1;i<=2;i++)
{
Console.Write("請投票:");
inttou=int.Parse(Console.ReadLine());
switch(tou)
{
case(int)houxuanren.zhangsan:zg++;break;
case(int)houxuanren.lisi:ls++;break;
}
}
Console.WriteLine("張三的投票結(jié)果是:"+zg+"、李四的投票結(jié)果是:"+ls);
Console.ReadKey();
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注