c#源代码.docx

上传人:无*** 文档编号:68365765 上传时间:2022-12-27 格式:DOCX 页数:65 大小:103.43KB
返回 下载 相关 举报
c#源代码.docx_第1页
第1页 / 共65页
c#源代码.docx_第2页
第2页 / 共65页
点击查看更多>>
资源描述

《c#源代码.docx》由会员分享,可在线阅读,更多相关《c#源代码.docx(65页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、【实例2-1】using System;using System. Windows. Forms;namespace TestEnum(public partial class TestEnum : Form(/Visual Studio . Net自动生成的构造函数,后文示例将全部省略 public TestEnum()(InitializeComponent(); enum MyEnum a = 101, b, c, d = 201, e, f ; 声明枚举型private void TestEnum Load(object sender, EventArgs e)(MyEnum x =

2、MyEnum. f;使用枚举型MyEnum y = (MyEnum)202;string result=枚举数x的值为;result += (int)x;将x转换为整数result += n枚举数y代表枚举元素”+ y;IblShow. Text = result;)【实例2-2】using System;using System. Windows. Forms;namespace TestStru(public partial class TestStru : Form(struct Student声明结构型(声明结构型的数据成员public int no;pub Ii c string n

3、ame;pub1i c char sex;public int score;声明结构型的方法成员public string Answer()string result=“该学生的信息如下:result += n学号:+ noJn为换行符result += n姓名:“+ name;result += n性别:*+ sex;result += n成绩:+ score;return result;返回结果);private void TestEnum Load(object sender, EventArgs e)(Student s;使用结构型5. no = 101;s.name =黄海;5 .

4、sex 二男;6 .score = 540;IblShow. Text = s. Answer 0 ;显示该生信息IblShow. Text += nn*+DateTime. Now; 显示当前时间【实例2-3】using System;class TestConstant(static void Main(string args)有符号的32位整型常量 无符号的32位整型常量 64位的长整型常量 32位的浮点型常量 /64位的双精度型常量 128位的小数型常量 16位的字符型常量 字符串常量64位的双精度型常量 布尔型常量16位的字符型常量(Con so le. Wri teLine (0)

5、. GetType ();Console. WriteLine(OU). GetType();Console. WriteLine(0L). GetType();Console. WriteLine(OF). GetType();Console. WriteLine(0D). GetType();Console. WriteLine(0M). GetType();Console. Wri teLine ( 0). GetType ();Console. Wri teLine(*0*). GetType ();Console. WriteLine (0. 0). GetType ();Conso

6、le. WriteLine(true). GetType();Console. WriteLine( u004T ). GetType();【实例2-4】using System;class TestVariablestatic void Main(string args) int a = 12, b = 15, c, d, e;c = a + b;d = a - b;e = a * b;Console. WriteLine(*c= 0td=l te=2*, c, d, e);)【实例2-5using System;using System. Windows. Forms;namespace

7、TestVariablepublic partial class TestOperator ; Form(private void TestVariable Load(object sender, EventArgs e) (int i = 5, j = 5, p, q;P = (i+) + (i+) + (i+);q = (+j) + (+j) + (+j);string t =;1blShow. Text = i + t + j + t + p + t + q;【实例2-6】using System;using System. Windows. Forms;namespace TestVa

8、riable(public partial class TestOperator : Form(private void TestVariable_Load(object sender, EventArgs e) (int a, b = 5;char cl = A;a = cl;字符型转整型float x = 3;x += b;整型转浮点型1 bl Show. Text = *a=* + a;/整型转为字符串1 bl Show. Text += nx=+ x; 浮点型转为字符串【实例2-7】using System;using System. Windows. Forms;namespace

9、TestVariable(public partial class TestOperator : Formprivate void TestVariable Load(object sender, EventArgs e) (int i = 25, j = 12;bool k;string result = * i!=j的值为+ (i != j);result += n i!=j & i:j的值为+ (i != j & i = j);result += *n i !=j & i=j+20的值为+(i != j & i = j + 20);result += n k = i !=j & i=j的

10、值为+ (i != j & i = j);1blShow.rFext = result;【实例28】using System;using System. Windows. Forms;namespace Testinterface(public partial class Test Interface : Form(interface I Student声明接口(string Answer ();class Student : IStudent 声明类,以实现接n (public int no;public string name;public string Answer() (string

11、result =该学生信息如下:*;result += n学号:* + no;result + : .n姓名:” + name;return result;private void btnOk_Click(object sender, EventArgs e)Student a = new Student ();定义并初始化变量aa.no = Convert. ToInt32(txtStuID. Text);a. name = txtName. Text;IblShow. Text = a. Answer (); ) )【实例2-9using System;class HeiloWorld (

12、public string HeiloCN() (return 你好!我是罗福强,中国人。”;public string HeiloEN() (return Hi! I am Jackson, a American.class TestDelegatedelegate string MyDelegateO ;声明委托static void Main(string args) (Hei loWor 1 d hello = new He 11 oWor 1 d(); 创建对象li = new MyDelegati (hel HelloCN); 创建委托对象并指向,个方法Console. Write

13、Line(h();通过委托对象调用所指向的方法h = new MyDelegate(hello. HelloEN);Console. WriteLine (h ();)【实例2-10using System;class TestArray (static void Main(string args) int x,y;声明数组x = new int 5 1, 5, 3, 2, 4);初始化数组y = new int5;Array. Copy(x, y, 5);将数组x的5个元素复制到数组y中Console.WriteLine(成功地从数组x复制到数组y,数组y各元素值如下:); for (int

14、 i = 0; i y.Length; i+) (Console. Write( 0)t, yi);)Array. Sort(x);将数组x的元素排序Console.WriteLine(n经过排序后,数组x各元素值如下:); for (int i = 0; i x.Length; i+) (Console. Wri te ( 0 t, xi);)【实例2-11】using System;using System. Windows. Forms;using System. Text;namespace TestString (public partial class TestString : F

15、orm(private void TestStringLoad(object sender, EventArgs e) (string s;定义字符串变量StringBui Ider sb = new StringBui lder(): 创建可变字符串对象 sb. Append (北运);添加字符串sb. Insert(1,京奥”);插入字符串s = sb. ToStringO;把可变字符串对象转化为字符串s = s. Insert (s. Length, 2008);IblShow.Text = + s + 长度为+ s. Length;)【实例212】using System;using

16、System. Windows. Forms;namespace Test If(public partial class Testinterface : Form(private void btnOk_Click(object sender, EventArgs e)char c = Convert. ToChar (txtChar. Text); 字符串转换为字符型i f (Char. IsLetter(c)if (Char. I slower (c) (IblShow. Text =这是个小写字母。”;else if (Char. I slipper (c) (IblShow. Text

17、 =”这是大写字母。”; else (IblShow. Text这是中文字符。; ) else (IblShow. Text=”这不是语言文字。 ) )【实例2-13using System;class TestSwitch(static void Main()(Console.WriteLine(服装类别:1二休闲装2二西装3二皮衣”);Console. Write(请选择类别:);string s = Console. ReadLine();int n = Convert. Tolnt 16 (s);把数字形式的字符串转化为整型数int t, cost = 0;t用来记录数量,cost用来

18、记录金额switch (n) ( case 1:Console. Write (休闲装的套数:);s = Console. ReadLine();t = Convert. Tolnt16(s);cost = t * 150;break;case 2:Console. Write(西装的套数:); s = Console. ReadLine();t = Convert. Tol nt 16(s);cost = t * 300;break;case 3:Console. Write(皮衣的件数:);s = Console. ReadLineO ;t = Convert. Tolnt16(s);co

19、st = t * 600;break;default:Console. WriteLine(无效选择,请输入 1、2或 3!);break;if (cost != 0)(Console. WriteLine (应付款元.,cost);Console. WriteLine (谢谢您的惠顾!);)【实例2-14】using System;using System. Windows. Forms;namespace TestWhile (public partial class TestWhi1e : Form(public TestWhileO /Visual Studio . Net 自动生成的

20、构造函数 (InitializeComponent();private void TestWhile_Load(object sender, EventArgs e) (int i,sum;i=l:循环变量赋初值sum=0;while (i= A & c = a & c = z)(n+; while (c != n*);Console. WriteLine(该行中英文字母的个数为:0, n);1【实例216】using System;using System. Windows. Forms;namespace TestFor(public partial class TestFor : For

21、mpublic TestForO (Initial izeComponent (); private void TestWhile Load(object sender, EventArgs e) (int i;int t;long si, s2;si = t = 1;/百万富翁第一天给陌生人的钱为1分/s2 = 100000;/陌生人第一天给百万富翁的钱为十万元/for (i = 2; i = 30; i+)(t = t * 2;/百万富翁第i天给陌生人的钱/si = si + t;/百万富翁第i天后共给陌生人的钱/s2 = s2 + 100000;/陌生人第i天后共百万富翁的钱/si =

22、si / 100;/将百分富翁给陌生人的分币换成元/MessageBox.Show(百万富翁给陌生人+sl+元。n陌生人给百万富翁+s2+元。); 【实例2-17using System; class TestForeach (static void MainO (string names = new string5;Console. WriteLine(请输入五个人的姓名:“); for (int i = 0; i names. Length; i+) (names i =Consol e. ReadU ne ();Console.WriteLine(已输入的姓名如下,请核对:、 forea

23、ch (string name in names) Console. Write(*0t*, name); )【实例2-181using System; class TestForeach (static void MainO (int i, j, k;for (i = 0; i = 0; j-)j表示在第i行左边的第j个空白字符(Console. Write(* );for (k = 0; k 2 * i + 1; k+) k表示在第i行的第k个星号字符,Console. Write(*);Console. Write(*n*);【实例221】using System;class TestG

24、oto(static void MainO(char c;for(int i二;i80;i+)最多输入80个字符(c=(char)Console. ReadO ;if (c=) break; 一旦输入星号就结朿Console. Write(c);)【实例2-22using System;class TestContinue(static void MainO(char ch_old, ch_new;ch_old =.;Console. WriteLine(请输入一串字符,以句号结尾:); do (ch_new = (char)Console. ReadO ;i f(ch_new = ch_ol

25、d)continue;Console. Write(chnew);ch_old=ch_new;while(ch_new!=*.);Console. Write(*n*);)【实例3-1】using System;public class Person定义类的数据成员pub 1ic string Name; public int Age;定义类的方法成员public string Answer()(return string. Format(姓名:0年龄:1岁。,Name, Age);class TestClass(static void MainO(Person p = new Person

26、(); 声明并创建对象p. Name =黄飞鸿;修改对象的数据成员的值p. Age = 25;String s = p. Answer();调用对象的方法成员Console. WriteLine(s);【实例3-2using System;class Circle(定义私有常量和字段private const float pi = 3. 14F, xO = 0, yO = 0;private float r;private float x, y;定义属性Rpublic float R(get ( return r;set(if (value 0)r = 0;elsevalue;定义只读属性Lp

27、ublic float Lget(return 2 * pi * r;class TestClassMemberstatic void MainO(Circle c = new Circle0;Console. Write(圆半径:);c. R = Convert. ToSingle(Console. ReadLineO); /,数据终保存在.字段屮 f loat 1 = c. L;Console. WriteLine(“圆的周长为:0, 1);)【实例3-3using System;class Circle(定义私有常量和字段private const f 1 oat pi = 3. 14F

28、, x0=0, y0=0;private float r;定义属性Rpublic float R(get(return r;)set(if (value R I , y R) return 0;Point p = new Point (x, y)1嵌套类实例化return p. Di stance 0 J调用嵌套类的方法成员嵌套类,默认为private,只能被包含类Circle使用 class Point private float X;private float Y;构造函数public Point(float x, float y) (X=x;Y=y;计算圆面上的点到圆心的距离public

29、 double DistanceO(return Math. Sqrt(X*X+Y*Y);/在TeslClassMember类不能使用包含Circle中的嵌套类Poinl class TestClassMember(static void MainOCircle c = new Circle();Console. Write(圆半径:);c. R = Convert. ToS ingle (Console. ReadLine ();Console. WriteLine (请输入圆面上的点的坐标:); float x = Convert. ToSingle(Console. ReadLine()

30、;float y = Convert. ToSingle(Console. ReadLine();float d = (float)c. PointDistance(x, y);if (d != 0 & d=0; i) (char c = pathi 一 1; if (c = W I 丨 c =) break;)/提取路径和文件名dir = path. Substring(0, i-l); fi1 ename = path. Substring(i);)class TestMethod(static void MainO调用方,其中实参dir和file是输出参数( Analyzer a = n

31、ew Analyzer(); 创建对象 Console.WriteLine(请个文件的路径:; string path = Console. ReadLineO ;string dir, file;a. SplitPath(path, out dir, out file);调用方法Console. WriteLine (文件所位目录: n 文件名:1”, dir, file);)【实例3-7】using System;class Maxer求最大数,形参为普通数组,实参必须为数组 public int Maxi(int numbers)int k = 0;求最大数的索引for (int i =

32、 0; i numbers. Length; i+)(if(numbersknumbersi)k=i;return numbersk;)求最大数,形参为params数组,实参可使用数据列表 public int Max2(params int numbers)(int k = 0;求最大数的索引for (int i = 0; i numbers. Length; i+)(if(numbersknumbersi)k=i;)return numbersk;class TestMethod (static void Main()(Max er m = new Maxer ()J 创建对象 int a

33、 = new int 4, 7, 1, 3, 2, 8, 6, 5 ; int max = m. Max 1(a);调用方法,实参为己初始化的数组Console.WriteLine(使用第一种方法得最大数为:0, max);max = m.Max2(4, 7, 1, 3, 2, 8, 6, 5);/调用方法,实参为数据列表Console.WriteLine(使用第二种方法得最大数为:0), max);【实例38】using System;class Maxer求最大整数public int Max(params int datas)int k = 0;求最大数的索引for (int i = 0

34、; i datas. Length; i+)(if(dataskdatasi) k=i;return datask;)求最大浮点数public double Max(params doublet datas)(int k = 0;求最大数的索引for (int i = 0; i datas. Length; i+)(if (datask datasi) k = i;)return datask;求最长字符串public string Max(params string datas)(int k = 0;求最长字符串的索引for (int i = 0; i datas. Length; i+)(

35、if (datask. Length datasi. Length) k = i;return datask;)class TestMethod(static void MainOMaxer m = new Max er ();int imax=m. Max(4, 7, 1, 3, 2, 8, 6, 5);Console. WriteLine(最大的整数为:0), imax);double fmax = m. Max (4. 5, 7. 8, 1. 3, 2. 9, 8. 4, 5. 5); Console. WriteLine(最大的浮点数为:, fmax);string smax = m.

36、 Max ( Are , you , going , to , Scarborough , Fair ); Console.WriteLine(最长的字符串为:0, smax);)【实例3-9】using System;class Park (定义字段成员,其中有两个只读字段 public readonly string name;public readonly string address; public decimal price;重载构造函数,以初始化字段成员 public Park(string name, string address, decimal price) (this, n

37、ame = name;this, address = address; this, price = price;class TestConstructor (static void Main() Park p二new Park(成都胜利公园,成都市蜀都大道100号”,20);Console. WriteLine ( 0地址:1门票价格:2元。,p. name, p. address, p. price); p. ncime =成都新华公园”;/错误,不能修改只读字段的值/p. price = 5;Console. Wri teLine ( 0地址:1,门票最新价格:2元。,p. name, p

38、. address, p. price);)【实例3-10using System;class Park ( public readonly string name; public readonly string address; public decimal price;声明构造函数pub 1ic Park(string name, string address, decimal price)this, name = name;this, address = address;this, price = price;Console.WriteLine(构造函数已被执行,对象已创建成功!”);)声明析构函数Park () (Console.WriteLine(析构函数已被执行,该对象即将被销毁!”);)

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁