《c#入门常用代码实例.docx》由会员分享,可在线阅读,更多相关《c#入门常用代码实例.docx(39页珍藏版)》请在三一办公上搜索。
1、c#入门常用代码实例1.namespace ConsoleApplication1class Programstatic void Main(string args)Console.WriteLine(Hello);string s = Console.ReadLine;/string v= /ab/;Console.WriteLine(s);int i1 = 12, i2 = 13;Console.WriteLine(i1+i2=+(i1+i2);Console.WriteLine(0+1=2,i1,i2,i1+i2);Console.ReadKey;2.using System;using
2、 System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication2class Programstatic void Main(string args)Console.Write(Input:);string s1 = Console.ReadLine;int i = Convert.ToInt32(s1);Console.Write(0,i); Console.ReadKey;3.using System;using System.Collections.Generic;us
3、ing System.Linq;using System.Text;namespace ConsoleApplication4class Programstatic void Main(string args)int i=2;bool b=(i=1);Console.WriteLine(0, i = 1);Console.WriteLine(0, b);Console.ReadKey;4.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleAppli
4、cation6class Programstatic void Main(string args)/*string s = Im kungge.;s = s.Replace(kungge,wankun);/替代某个窜Console.WriteLine(s);*/ /*string s = ;string Website = s.Substring(7);Console.WriteLine(Website);string s1 = s.Substring(7,7);Console.WriteLine(s1);*/*string s = TMD Ill be strong!;if (s.Conta
5、ins(TMD) | s.Contains(strong)Console.WriteLine(The words are not good!please use them well!);*/* string s = ;if (s.StartsWith(http:/) | s.StartsWith(https:/)Console.WriteLine(Its a website!);/s.EndWith;*/string s = Im good enough!;int i = s.IndexOf(good);/字符串中的第几个位置int k = s.IndexOf(well);Console.Wr
6、iteLine(i);Console.WriteLine(k);Console.ReadKey;5.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication7class Programstatic void Main(string args)/*string s=Console.ReadLine;string words = s.Split( );for (int i = words.Length - 1; i >= 0; i-
7、)Console.Write( wordsi);*/*Console.WriteLine(Input:);string emil = Console.ReadLine;int a=emil.IndexOf();string uname = emil.Substring(0,a);string website = emil.Substring(a+1);Console.WriteLine(uname);Console.WriteLine(website);*/*string lines = System.IO.File.ReadAllLines(d:test.txt,Encoding.Defau
8、lt);/注意这种用法foreach (string line in lines)string strs = line.Split(new char ,StringSplitOptions.RemoveEmptyEntries);/注意这种用法string title=strs0;string author=strs1;title = title.Substring(0,Math.Min(17,title.Length);title = title + .;Console.WriteLine(0|1,title,author);*/string value = G(d:w.ini,姓名);Co
9、nsole.WriteLine(value);Console.ReadKey;static string G(string filename, string itemname)/?注意这个用法string lines = System.IO.File.ReadAllLines(filename, Encoding.Default);foreach (string line in lines)string strs = line.Split(=);string name =strs0;string value =strs1;if (name.Trim = itemname)return valu
10、e.Trim;return wrong;6.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication8class Programstatic void Main(string args)/*int age = 21;/A(ref age);A(out age);Console.Write(age);*/int i;string str=Console.ReadLine; if(int.TryParse(str,out i)Conso
11、le.WriteLine(Successfull 0,i);elseConsole.WriteLine(Wrong);Console.ReadKey;/*static void A(ref int age)/!注意ref和out的区别age+;*/*static void A(out int age)age = 30;age+;*/7.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 常量和静态成员class Programpublic const double
12、 pi = 3.1415926;/常量static void Main(string args)Person.Totalcount = 20;/Person.Age = 20;/错误,非静态的字段、方法或属性“常量和静态成员.Person.Age”要求对象引用Doit;Console.WriteLine(Person.Totalcount);Dog d = new Dog;d.Jiao;Person.People;Person p = new Person;p.Age = 19;p.Hello;/ ConsoleHelper h=nwe ConsoleHelper;/静态类不能被实例化,其一般
13、用来实现数据库ConsoleHelper.ReadInt;Console.ReadKey;public static void DoitConsole.WriteLine(sdfsd);Console.WriteLine(Person.Totalcount);public class Personpublic static int Totalcount;public int Age;public static void PeopleConsole.WriteLine(The people:0,Person .Totalcount);/Console.WriteLine(The people:0
14、 Age: 1, Totalcount,Age);/在static成员中不能调用非static成员public void HelloConsole.WriteLine(People:0 Age:1,Totalcount ,Age);/在非static成员中可以调用static成员public class Dogpublic void JiaoConsole.WriteLine(Wangw:0, Person.Totalcount);static class ConsoleHelper/这是一个静态类public static int ReadIntstring str = Console.Re
15、adLine;return Convert.ToInt32(str);8.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Programstatic void Main(string args)Console.WriteLine(input two nums:);string str1 = Console.ReadLine;string str2 = Console.ReadLine;while (str1 !
16、= admin)Console.WriteLine(用户名不存在,请重新输入:);str1 = Console.ReadLine;str2 = Console.ReadLine;while (str2 != 888888)Console.WriteLine(密码错误,请重新输入:);str2 = Console.ReadLine;if (str1 = admin & str2 = 888888)Console.WriteLine(欢迎!);Console.ReadKey;9.using System;using System.Collections.Generic;using System.L
17、inq;using System.Text;namespace ConsoleApplication5class Programstatic void Main(string args)int max = 0;while (true)Console.WriteLine(请输入一个数,结束时输入end:);string str = Console.ReadLine;if (str = end)Console.WriteLine(最大数:0, max);return;int i = Convert.ToInt32(str);if (i > max)max = i;Console.ReadKey
18、;10.continue的用法using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Programstatic void Main(string args)int i=1;int sum = 0;while (i <= 100)if (i % 7 = 0)i+;continue;sum = sum + i;i+; Console.WriteLine(sum);Console.ReadKey;11.枚举的用法:us
19、ing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5enum Gender=male,female,unknown;class Programstatic void Main(string args)Gender g1=Gender .female;Gender g2=Gender.male ;Gender g3=Gender .unknown ;Console.ReadKey;12.数组:namespace ConsoleApp
20、lication5class Programstatic void Main(string args)/*string strs=new string5;strs0 = one;strs1=two;*/string strs = one,two,three ;string s = ;for (int i = 0; i < strs.Length - 1; i+)s = s+strsi+ |;s=s+strsstrs.Length -1;Console.WriteLine(s);Console.ReadKey;13.using System;using System.Collections.
21、Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Programstatic void Main(string args)int t = 1,2,3,4 ;string strs = new stringt.Length;for (int i = 0; i < t.Length; i+)strsi = Convert.ToString(ti);for (int i = 0; i < t.Length; i+)Console.WriteLine(strsi);Console.Rea
22、dKey; 14.foreach 循环的用法:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Programstatic void Main(string args)string strs = one,two,three ;foreach (string str in strs)Console.WriteLine(0,str);Console.ReadKey; 15.函数案例1:using System;usi
23、ng System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Programstatic void Main(string args)string strs = one,two,three ;Console.WriteLine(S(strs,|);Console.ReadKey; static string S(string strs,string sep)string s=;for(int i=0;i<strs.Length -1;i+)s = s
24、+ strsi + sep;if (strs.Length > 0)s = s + strsstrs.Length - 1;return s;16.函数的可变参数:关键字paramsusing System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Programstatic void Main(string args)V(one,two,three,four);Vt(Kungge, ping, swimming, progr
25、amming);Console.ReadKey; /static void V(string strs)/不加关键字params就会出错static void V(params string strs)foreach (string str in strs)Console.WriteLine(str);return;static void Vt(string name, params string strs)foreach (string str in strs)Console.WriteLine(My name is 0,My hobby:1, name, str);return;17.函数
26、重载:相同的函数名必须保证参数不同18.字符串处理:string s=Hello;s.Length;s2;重要特性:不可变性(指的是内存中的字符串不可变) 只能通过索引来读取指定的位置的char,不能对指定的位置的char进行修改如果对char进行修改就需要创建一个新的字符串,用s.ToCharArray方法得到字符串的char数组.using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Programstatic v
27、oid Main(string args)string s = hello;char chars = s.ToCharArray;chars0 = H;string s2 = new string(chars);Console.WriteLine(s);Console.WriteLine(s2);Console.ReadKey; 19.string常用函数:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication5class Pro
28、gramstatic void Main(string args)string s = Hello; string s2 = s.ToLower;/转化为小写/string s2 = s.ToUpper;string s3 = hello ;string s4 = s3.Trim;/trim去掉两边的空格Console.WriteLine(s3);Console.WriteLine(s4);Console.WriteLine(s);Console.WriteLine(s2);bool b = (abc=ABC);bool b2 = abc.Equals(ABC,StringComparison
29、.OrdinalIgnoreCase);int i = abc.CompareTo(ABC);Console.WriteLine(i);Console.WriteLine(b2);string s5 = aa,bb,cc;string strs = s5.Split(,);/这里面也可以定义多个分隔符foreach (string str in strs)Console.WriteLine(str);string s6 = aa,bb,cc,dd,ee;string strs2 = s6.Split(new char,StringSplitOptions .RemoveEmptyEntries
30、);/将字符串按照指定的char分隔符分割成字符串数组/opinion取RemoveEmptyEntries是移除结果中的空白字符串foreach (string str in strs2)Console.WriteLine(str);string s7 = 我爱祖国我爱人民我爱和平;string strs3 = s7.Split(new string 我爱 , StringSplitOptions.RemoveEmptyEntries);/将字符串按照指定的string分隔符分割成字符串数组foreach (string str in strs3)Console.WriteLine(str)
31、;Console.ReadKey; 20.练习一:namespace ConsoleApplication5class Programstatic void Main(string args)string lines = System.IO.File.ReadAllLines(e:test.txt, Encoding.Default);foreach (string str in lines)Console.WriteLine(str);Console.ReadKey; 21.接19:其它的什么IndexOf等上面有实例代码,这里不做介绍了22.练习二:using System;using S
32、ystem.Collections.Generic;using System.Linq;using System.Text;namespace _11class Programstatic void Main(string args)string lines = System.IO.File.ReadAllLines(E:test2.txt, Encoding.Default);foreach (string line in lines)/string str = line.Split( );string str = line.Split(new char ,StringSplitOption
33、s.RemoveEmptyEntries);string title = str0;string writer=str1;title = title.Substring(0,Math .Min(17,title.Length );if (title.Length > 17)/ 为什么这个无法实现title = title + .;Console.WriteLine(Title:0 Writer;1,title,writer );Console.ReadKey;23.练习三:using System;using System.Collections.Generic;using System.
34、Linq;using System.Text;namespace _12class Programstatic void Main(string args)string value= G(e:test3.ini,Cname);Console.WriteLine(value);Console.ReadKey;static string G(string filename, string itemname)string lines = System.IO.File.ReadAllLines(filename, Encoding.Default);foreach (string line in li
35、nes)/string str = line.Split(new char = , StringSplitOptions.RemoveEmptyEntries);/这个可以用一下string str = line.Split(=);string name = str0;string value = str1;if (name.Trim = itemname)return value.Trim ;/return value;return Wrong;24.练习四:简易聊天版机器人using System;using System.Collections.Generic;using System.
36、Linq;using System.Text;namespace _13class Programstatic void Main(string args)int full=3;Console.WriteLine(你好,我是机器人);while(true)string str=Console.ReadLine ;if (str.Contains(你) & (str.Contains(名字)|str .Contains (姓名)Console.WriteLine(我是K1,请多多指教);else if(str.Contains (你)&str.Contains (女朋友)Console .Wri
37、teLine (我还没有了,要不你给我介绍一个);else if(str.Contains (今天)&str.Contains (天气)Console .WriteLine (天气很好);else if (str.Contains(你) & str.Contains(吃饭)Console.WriteLine(我已经吃过了);else if (str.Contains(再见) | str.Contains(拜)|str.Contains (8)Console.WriteLine(拜,下次聊);return;elseConsole.WriteLine(听不懂);full-;if(full<0)
38、Console.WriteLine (我饿了,给我点东西吃!);Console.WriteLine(请输入数字给我喂食);int i=Convert .ToInt32 (Console.ReadLine );if(i>5&i<10)full=full+i;Console.WriteLine (好饱啊);else if (i < 0)Console.WriteLine(小子,你玩我的吧);return;elsefull = full + i;Console.WriteLine(可以了);Console.ReadKey;25.ref和out参数:using System;using S
39、ystem.Collections.Generic;using System.Linq;using System.Text;namespace _11class Programstatic void Main(string args)int i = 20;Increase(ref i);Inc(out i);Console.WriteLine(i);int b;string str = Console.ReadLine;Console.WriteLine(int.TryParse (str,out b);/注意这个用法Console.ReadKey;static void Increase(ref int a)a+;Console.WriteLine(a);static void Inc(out int b)b = 30;b+;Console.WriteLine(b);/注:这是在.Net传智播客视频教学中学C#基础时做的课堂笔记整理 今发布于此只为方便大家学习 仅供参考 由于本人也是初学者 如有错误请见谅 请自行更正!
链接地址:https://www.31ppt.com/p-3153668.html