当前位置:文档之家› c实例源代码

c实例源代码

【实例1-1】using System;using System、Collections、Generic;using System、Text;namespace _{class Program{static void Main(string[] args){System、Console、Wriine("恭喜您,学会了C#编程!");System、Console、ReadLine();}}}【实例1-2】private void Form1_Load(object sender, EventArgs e){this、Text="这就是一窗口!";Label lbShow = new Label();lbShow、Location = new Point(40,50);lbShow、AutoSize = true;lbShow、Text = "恭喜您学会编程了!";this、Controls、Add(lbShow);int[] x, y;x = new int[5] { 1,2,3,4,5};y = new int[5];y = x;foreach (int a in y){lbShow、Text += a、ToString();}this、Controls、Add(lbShow);}【实例2-1】using System;using System、Windows、Forms;namespace TestEnum{public partial class TestEnum : Form{//Visual Studio 、Net自动生成得构造函数,后文示例将全部省略public TestEnum(){Initializeponent();}enum MyEnum { a = 101, b, c, d = 201, e, f }; //声明枚举型private void TestEnum_Load(object sender, EventArgs e) {MyEnum x = MyEnum、f; //使用枚举型MyEnum y = (MyEnum)202;string result ="枚举数x得值为";result += (int)x; //将x转换为整数result += "\n枚举数y代表枚举元素" + y;lblShow、Text = result;}}}【实例2-2】using System;using System、Windows、Forms;namespace Test Stru{public partial class TestStru : Form{struct Student//声明结构型{//声明结构型得数据成员public int no;public string name;public char sex;public int score;//声明结构型得方法成员public string Answer(){string result="该学生得信息如下:";result += "\n学号:" + no; //"\n"为换行符result += "\n姓名:"+ name;result += "\n性别:"+ sex;result += "\n成绩:"+ score;return result; //返回结果}};private void TestEnum_Load(object sender, EventArgs e) {Student s; //使用结构型 s、no = 101;s、name = "黄海";s、sex = '男';s、score = 540;lblShow、Text = s、Answer(); //显示该生信息lblShow、Text += "\n\n"+DateTime、Now; //显示当前时间 }}}【实例2-3】using System;class TestConstant{static void Main(string[] args){Console、Wriine((0)、GetType()); //有符号得32位整型常量Console、Wriine((0U)、GetType()); //无符号得32位整型常量Console、Wriine((0L)、GetType()); //64位得长整型常量Console、Wriine((0F)、GetType()); //32位得浮点型常量Console、Wriine((0D)、GetType()); //64位得双精度型常量Console、Wriine((0M)、GetType()); //128位得小数型常量Console、Wriine(('0')、GetType()); //16位得字符型常量Console、Wriine(("0")、GetType()); //字符串常量Console、Wriine((0、0)、GetType()); //64位得双精度型常量Console、Wriine((true)、GetType()); //布尔型常量Console、Wriine(('\u0041')、GetType()); //16位得字符型常量Console、ReadLine();}}【实例2-4】using System;class TestVariable{static void Main(string[] args){int a = 12, b = 15, c, d, e;c = a + b;d = a - b;e = a * b;Console、Wriine("c={0}\td={1}\te={2}", c, d, e);}}【实例2-5】using System;using System、Windows、Forms;namespace TestVariable{public 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 = " ";lblShow、Text = i + t + j + t + p + t + q;}}}【实例2-6】using System;using System、Windows、Forms;namespace TestVariable{public partial class TestOperator : Form{private void TestVariable_Load(object sender, EventArgs e) {int a, b = 5;char c1 = 'A';a = c1; //字符型转整型float x = 3;x += b; //整型转浮点型lblShow、Text = "a=" + a; //整型转为字符串lblShow、Text += "\nx=" + x; //浮点型转为字符串}}}【实例2-7】using System;using System、Windows、Forms;namespace TestVariable{public partial class TestOperator : Form{private 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得值为" + (i != j && i >= j);lblShow、Text = result;}}}【实例2-8】using System;using System、Windows、Forms;namespace TestInterface{public partial class TestInterface : Form{interface IStudent //声明接口{string Answer();}class Student : IStudent //声明类,以实现接口{public int no;public string name;public string Answer(){string 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;lblShow、Text = a、Answer();}}}【实例2-9】using System;class HelloWorld{public string HelloCN(){return"您好!我就是Jackson,中国人。

";}public string HelloEN(){return"Hi! I am Jackson, a American、";}}class TestDelegate{delegate string MyDelegate(); //声明委托static void Main(string[] args){HelloWorld hello = new HelloWorld(); //创建对象MyDelegate h = new MyDelegate(hello、HelloCN); //创建委托对象并指向一个方法Console、Wriine(h()); //通过委托对象调用所指向得方法h = new MyDelegate(hello、HelloEN);Console、Wriine(h());}}【实例2-10】using System;class TestArray{static void Main(string[] args){int[] x,y; //声明数组x = new int[5] { 1,5,3,2,4}; //初始化数组y = new int[5];Array、Copy(x, y, 5); //将数组x得5个元素复制到数组y中Console、Wriine("成功地从数组x复制到数组y,数组y各元素值如下:");for (int i = 0; i < y、Length; i++){Console、Write("{0}\t", y[i]);}Array、Sort(x); //将数组x得元素排序Console、Wriine("\n经过排序后,数组x各元素值如下:");for (int i = 0; i < x、Length; i++){Console、Write("{0}\t", x[i]);}}【实例2-11】using System;using System、Windows、Forms;using System、Text;namespace TestString{public partial class TestString : Form{private void TestString_Load(object sender, EventArgs e){string s; //定义字符串变量StringBuilder sb = new StringBuilder(); //创建可变字符串对象 sb、Append("北运"); //添加字符串sb、Insert(1, "京奥"); //插入字符串s = sb、ToString(); //把可变字符串对象转化为字符串s = s、Insert(s、Length, "2008");lblShow、Text ="\"" + s + "\"长度为" + s、Length;}}}【实例2-12】using System;using System、Windows、Forms;namespace TestIf{public partial class TestInterface : Form{private void btnOk_Click(object sender, EventArgs e){char c = Convert、ToChar(txtChar、Text); //字符串转换为字符型if (Char、IsLetter(c)){if (Char、IsLower(c)){lblShow、Text = "这就是一个小写字母。

相关主题