资源描述:
《C#知识点代码范例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、lambada表达式classProgram{staticvoidMain(string[]args){int[]num=newint[]{3,5,6,7,8,9,67,58,72,87};vareven=num.Where(p=>p%2==0).Select(p=>p).OrderByDescending(P=>P);//foreach(varitemineven)//{//Console.WriteLine(item);//}//Programpro=newProgram();//Console.WriteLine(pro.m(10,10));//Console.WriteLine(pr
2、o.x(10,10));varn1=fromnumsinnumwherenums%2==0orderbynumsdescendingselectnums;varn2=num.Where(p=>p%2==0).OrderByDescending(p=>p).Select(p=>p);//foreach(variteminn2)//{//Console.WriteLine(item);//}varn3=num.Skip(5).Take(3);foreach(variteminn3){Console.WriteLine(item);}Console.Read();}delegateintplum(
3、inta,intb);privateintm(inta1,inta2){//匿名方法plump=delegate(inta,intb){returna+b;};returnp(a1,a2);}privateintx(intn1,intn2){plump=(a,b)=>(a*b);returnp(n1,n2);}}观察者模式publicdelegatevoidsleep();publicinterfaceIjiao{voidfm();intnumber{get;set;}}classCat{publiceventsleephow;publicvoidjiao(){Form4f4=newForm
4、4();f4.Show();if(how!=null){how();}//被观察者Cat触发所有观察者的方法showForm();}//创建一个接口对象集合privateList_list=newList();//让所有观察者执行接口中的方法publicvoidshowForm(){foreach(Ijiaoitemin_list){item.fm();}}//把实现接口的对象添加到集合中,即添加观察者publicvoidaddItem(Ijiaoij){this._list.Add(ij);}}publicclassDog:Ijiao{publicvoidjia
5、o(){Form2f2=newForm2();f2.Show();}publicvoidfm(){jiao();}publicintnumber{get{thrownewNotImplementedException();}set{thrownewNotImplementedException();}}}publicclassMen:Ijiao{publicvoidwake(){Form3f3=newForm3();f3.Show();}publicvoidfm(){wake();}publicintnumber{get{thrownewNotImplementedException();}
6、set{thrownewNotImplementedException();}}}privatevoidbutton2_Click(objectsender,EventArgse){Catca=newCat();Dogdg=newDog();Menme=newMen();//订阅事件//ca.how+=newsleep(dg.jiao);//ca.how+=newsleep(me.wake);//ca.jiao();//添加观察者ca.addItem(dg);ca.addItem(me);ca.jiao();}单例模式privatestaticForm2fm;publicstaticForm
7、2createFm(){if(fm==null){fm=newForm2();}returnfm;}privatevoidbutton1_Click(objectsender,EventArgse){Form2fm2=createFm();fm2.Show();}读取XMLpublicvoidxmlread(){XmlReaderxr=XmlReader.Create(@"f:myxml.xml");whi