.NET程序设计C#编程语言(4.0)自我介绍1990年中科院硕士研究生毕业从1994年做管理软件从2001年使用.NET和C#主要著作:《NET软件设计新思维--像搭积木一样搭建软件》(电子工业出版社出版)。(=20665127)微软MSDN特邀讲师().NET的内容ConsoleWindowsFormsADO.NETASP.NETWebServiceXMLLINQ(LINQtoobject、LINQtoSQL、LINQtoDataSet、LINQtoXML)WindowsPresentationFoundation(WPF)WindowsCommunicationFoundation(WCF)WindowsWorkflowFoundation(WF)WindowsCardSpaceSilverlight3.0ASP.NET3.5ExtensionsBlend。。。。。。C#与.NET的关系公共语言运行库CommonLanguageRuntime.NETFramework类库公共语言规范CommonLanguageSpecificationVB.NETC#VC++.NET其他VisualStudio.NETHelloWorld--您的第一个程序usingSystem;//AHelloWorld!programinC#namespaceHelloWorld{classHello{staticvoidMain(){System.Console.WriteLine(HelloWorld!);}}}Main()和命令行参数classCommandLine2{staticvoidMain(string[]args){System.Console.WriteLine(Numberofcommandlineparameters={0},args.Length);foreach(stringsinargs){System.Console.WriteLine(s);}}}Numberofcommandlineparameters=3JohnPaulMary类型数据类型值类型简单类型结构类型枚举类型引用类型类接口委托(代理)数组值类型结构类型:structPhoneBook{publicstringname;publicstringphone;publicstringaddress;}PhoneBookpb;pb.name=“wang”;pb.phone=“88102324”;pb.address=“青年大街126号”;值类型枚举类型:用户定义的整数(int、short、long、byte)enumWeekDay{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Satursay};WeekDaywd;wd=WeekDay.Monday;类型转换隐式数值类型转换:自动转换成高精度的数值inti=5;intj=8;longl=i+j;sbyeshort、int、long、float、double、decimalbyteshort、ushort、int、uint、long、ulong、float、double、decimalshortint、long、float、double、decimalushortint、uint、long、ulong、float、double、decimalintlong、float、double、decimaluintlong、ulong、float、double、decimallongfloat、double、decimalulongfloat、double、decimalfloatdoublecharushort、int、uint、long、ulong、float、double、decimal类型转换隐式引用类型转换:任何对象可以转换为object派生类(接口)可以转换为父类(接口)任何数组类型可以转换为System.Array任何委托(代理)类型可以转换为System.Delegate任何数组类型可以转换为System.ICloneablenull类型可以可以转换为任何引用类型类型转换显式数值转换inti=10;intj=30;longl=(long)(i+j);intg=(int)l;装箱(Boxing)与拆箱(Unboxing)装箱转换(Boxing):把值类型转换为object类型或接口类型intI=10;objectobj=i;拆箱转换(Unboxing):把对象类型或接口类型转换为值类型inti=10;objectobj=i;intj=(int)obj;类型转换类ConvertSample\CSharp\Convert\Convert数组//Single-dimensionalarrays.int[]myArray1=newint[5];string[]myArray2=newstring[6];//Multidimensionalarrays.int[,]myArray3=newint[4,2];初始化数组//Single-dimensionalarray(numbers).int[]n1=newint[4]{2,4,6,8};int[]n2=newint[]{2,4,6,8};int[]n3={2,4,6,8};//Single-dimensionalarray(strings).string[]s1=newstring[3]{John,Paul,Mary};string[]s2=newstring[]{John,Paul,Mary};string[]s3={John,Paul,Mary};//Multidimensionalarray.int[,]n4=newint[3,2]{{1,2},{3,4},{5,6}};int[,]n5=newint[,]{{1,2},{3,4},{5,6}};int[,]n6={{1,2},{3,4},{5,6}};//Jaggedarray.int[][]n7=newint[2][]{newint[]{2,4,6},newint[]{1,3,5,7,9}};int[][]n8=newint[][]{newint[]{2,4,6},newint[]{1,3,5,7,9}};int[][]n9={newint[]{2,4,6},newint[]{1,3,5,7,9}};字符串strings1=Hello;strings2=s1;s1+=andgoodbye.;Console.WriteLine(s2);//outputsHello字符串原义字符串:@符号@符号会告知字符串构造函数忽略转义符和分行符。因此,以下两个字符串是完全相同的:stringp1=\\\\MyDocuments\\MyFiles\\;stringp2=@\\MyDocuments\MyFiles\;字符串访问各个字符strings4=Hello,World;char[]arr=s4.ToCharArray(0,s4.Length);foreach(charcinarr){System.Console.Write(c);//outputsHello,World}字符串练习在一篇问章中,挑出单词来.字符串将字符串拆分为子字符串使用Split方法分析字符串classTestStringSplit{staticvoidMain(){char[]delimiterChars={'',',','.',':','\t'};stringtext=one\ttwothree:four,fivesixseven;System.Console.WriteLine(Originaltext:'{0}',text);string[]words=text.Split(delimiterChars);System.Console.WriteLine({0}wordsintext:,words.Length);foreach(stringsinwords){System.Console.WriteLine(s);}}}字符串StartsWith()EndWith()IndexOf()Trim()ToLower()Substring()练习应用这些方法自己做一些小程序字符串classStringSearch{staticvoidMain(){stringstr=Extensionmethodshaveallthecapabilitiesofregularstaticmethods.;//WritethestringandincludethequotationmarksSystem.Console.WriteLine(\{0}\,str);booltest1=str.StartsWith(extension);System.Console.WriteLine(startswith\extension\?{0},test1);booltest2=str.StartsWith(extension,System.StringComparison.OrdinalIgnoreCase);System.Console.WriteLine(startswith\extension\?{0}(ignoringcase),test2);booltest3=str.EndsWith(.);System.Console.WriteLine(endswith'.'?{0},test3);intfirst=str.IndexOf(method);intlast=str.LastIndexOf(method);stringstr2=str.Substring(first,last-first);System.Console.WriteLine(betweentwo\method\words:'{0}',str2);//KeeptheconsolewindowopenindebugmodeSystem.Console.WriteLine(Pressanykeytoexit.);System.Console.ReadKey();}}使用正则表达式搜索字符串classTestRegularExpressions{staticvoidMain(){string[]sentences={cowoverthemoon,BetsytheCow,coweringinthecorner,nomatchhere};stringsPattern=cow;foreach(stringsinsentences){System.Console.Write({0,24},s);if(System.Text.RegularExpressions.Regex.IsMatch(s,sPattern,System.Text.RegularExpressions.RegexOptions.IgnoreCase)){System.Console.WriteLine((matchfor'{0}'found),sPattern);}else{System.Console.WriteLine();}}}}cowoverthemoon(matchfor'co