c#实例源代码.docx
《c#实例源代码.docx》由会员分享,可在线阅读,更多相关《c#实例源代码.docx(20页珍藏版)》请在冰点文库上搜索。
c#实例源代码
【实例1-1】
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
namespace_
{
classProgram
{
staticvoidMain(string[]args)
{
System.Console.WriteLine("恭喜你,学会了C#编程!
");
System.Console.ReadLine();
}
}
}
【实例1-2】
privatevoidForm1_Load(objectsender,EventArgse)
{
this.Text="这是一窗口!
";
LabellbShow=newLabel();
lbShow.Location=newPoint(40,50);
lbShow.AutoSize=true;
lbShow.Text="恭喜你学会编程了!
";
this.Controls.Add(lbShow);
int[]x,y;
x=newint[5]{1,2,3,4,5};
y=newint[5];
y=x;
foreach(intainy)
{
lbShow.Text+=a.ToString();
}
this.Controls.Add(lbShow);
}
【实例2-1】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestEnum
{
publicpartialclassTestEnum:
Form
{
//VisualStudio.Net自动生成的构造函数,后文例如将全部省略
publicTestEnum()
{
InitializeComponent();
}
enumMyEnum{a=101,b,c,d=201,e,f};//声明枚举型
privatevoidTestEnum_Load(objectsender,EventArgse)
{
MyEnumx=MyEnum.f;//使用枚举型
MyEnumy=(MyEnum)202;
stringresult="枚举数x的值为";
result+=(int)x;//将x转换为整数
result+="\n枚举数y代表枚举元素"+y;
lblShow.Text=result;
}
}
}
【实例2-2】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestStru
{
publicpartialclassTestStru:
Form
{
structStudent//声明结构型
{
//声明结构型的数据成员
publicintno;
publicstringname;
publiccharsex;
publicintscore;
//声明结构型的方法成员
publicstringAnswer()
{
stringresult="该学生的信息如下:
";
result+="\n学号:
"+no;//"\n"为换行符
result+="\n姓名:
"+name;
result+="\n性别:
"+sex;
result+="\n成绩:
"+score;
returnresult;//返回结果
}
};
privatevoidTestEnum_Load(objectsender,EventArgse)
{
Students;//使用结构型
s.no=101;
s.name="黄海";
s.sex='男';
s.score=540;
lblShow.Text=s.Answer();//显示该生信息
lblShow.Text+="\n\n"+DateTime.Now;//显示当前时间
}
}
}
【实例2-3】
usingSystem;
classTestConstant
{
staticvoidMain(string[]args)
{
Console.WriteLine((0).GetType());//有符号的32位整型常量
Console.WriteLine((0U).GetType());//无符号的32位整型常量
Console.WriteLine((0L).GetType());//64位的长整型常量
Console.WriteLine((0F).GetType());//32位的浮点型常量
Console.WriteLine((0D).GetType());//64位的双精度型常量
Console.WriteLine((0M).GetType());//128位的小数型常量
Console.WriteLine(('0').GetType());//16位的字符型常量
Console.WriteLine(("0").GetType());//字符串常量
Console.WriteLine((0.0).GetType());//64位的双精度型常量
Console.WriteLine((true).GetType());//布尔型常量
Console.WriteLine(('\u0041').GetType());//16位的字符型常量
Console.ReadLine();
}
}
【实例2-4】
usingSystem;
classTestVariable
{
staticvoidMain(string[]args)
{
inta=12,b=15,c,d,e;
c=a+b;
d=a-b;
e=a*b;
Console.WriteLine("c={0}\td={1}\te={2}",c,d,e);
}
}
【实例2-5】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestVariable
{
publicpartialclassTestOperator:
Form
{
privatevoidTestVariable_Load(objectsender,EventArgse)
{
inti=5,j=5,p,q;
p=(i++)+(i++)+(i++);
q=(++j)+(++j)+(++j);
stringt="";
lblShow.Text=i+t+j+t+p+t+q;
}
}
}
【实例2-6】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestVariable
{
publicpartialclassTestOperator:
Form
{
privatevoidTestVariable_Load(objectsender,EventArgse)
{
inta,b=5;
charc1='A';
a=c1;//字符型转整型
floatx=3;
x+=b;//整型转浮点型
lblShow.Text="a="+a;//整型转为字符串
lblShow.Text+="\nx="+x;//浮点型转为字符串
}
}
}
【实例2-7】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestVariable
{
publicpartialclassTestOperator:
Form
{
privatevoidTestVariable_Load(objectsender,EventArgse)
{
inti=25,j=12;
boolk;
stringresult="i!
=j的值为"+(i!
=j);
result+="\ni!
=j&&i>=j的值为"+(i!
=j&&i>=j);
result+="\ni!
=j&&i>=j+20的值为"+(i!
=j&&i>=j+20);
result+="\nk=i!
=j&&i>=j的值为"+(i!
=j&&i>=j);
lblShow.Text=result;
}
}
}
【实例2-8】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestInterface
{
publicpartialclassTestInterface:
Form
{
interfaceIStudent//声明接口
{
stringAnswer();
}
classStudent:
IStudent//声明类,以实现接口
{
publicintno;
publicstringname;
publicstringAnswer()
{
stringresult="该学生信息如下:
";
result+="\n学号:
"+no;
result+="\n姓名:
"+name;
returnresult;
}
}
privatevoidbtnOk_Click(objectsender,EventArgse)
{
Studenta=newStudent();//定义并初始化变量a
a.no=Convert.ToInt32(txtStuID.Text);
a.name=txtName.Text;
lblShow.Text=a.Answer();
}
}
}
【实例2-9】
usingSystem;
classHelloWorld
{
publicstringHelloCN()
{
return"你好!
我是Jackson,中国人。
";
}
publicstringHelloEN()
{
return"Hi!
IamJackson,aAmerican.";
}
}
classTestDelegate
{
delegatestringMyDelegate();//声明委托
staticvoidMain(string[]args)
{
HelloWorldhello=newHelloWorld();//创立对象
MyDelegateh=newMyDelegate(hello.HelloCN);//创立委托对象并指向一个方法
Console.WriteLine(h());//通过委托对象调用所指向的方法
h=newMyDelegate(hello.HelloEN);
Console.WriteLine(h());
}
}
【实例2-10】
usingSystem;
classTestArray
{
staticvoidMain(string[]args)
{
int[]x,y;//声明数组
x=newint[5]{1,5,3,2,4};//初始化数组
y=newint[5];
Array.Copy(x,y,5);//将数组x的5个元素复制到数组y中
Console.WriteLine("成功地从数组x复制到数组y,数组y各元素值如下:
");
for(inti=0;i{
Console.Write("{0}\t",y[i]);
}
Array.Sort(x);//将数组x的元素排序
Console.WriteLine("\n经过排序后,数组x各元素值如下:
");
for(inti=0;i{
Console.Write("{0}\t",x[i]);
}
}
【实例2-11】
usingSystem;
usingSystem.Windows.Forms;
usingSystem.Text;
namespaceTestString
{
publicpartialclassTestString:
Form
{
privatevoidTestString_Load(objectsender,EventArgse)
{
strings;//定义字符串变量
StringBuildersb=newStringBuilder();//创立可变字符串对象
sb.Append("北运");//添加字符串
sb.Insert(1,"京奥");//插入字符串
s=sb.ToString();//把可变字符串对象转化为字符串
s=s.Insert(s.Length,"2008");
lblShow.Text="\""+s+"\"长度为"+s.Length;
}
}
}
【实例2-12】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestIf
{
publicpartialclassTestInterface:
Form
{
privatevoidbtnOk_Click(objectsender,EventArgse)
{
charc=Convert.ToChar(txtChar.Text);//字符串转换为字符型
if(Char.IsLetter(c))
{
if(Char.IsLower(c))
{
lblShow.Text="这是一个小写字母。
";
}
elseif(Char.IsUpper(c))
{
lblShow.Text="这是大写字母。
";
}
else
{
lblShow.Text="这是中文字符。
";
}
}
else
{
lblShow.Text="这不是语言文字。
";
}
}
}
}
【实例2-13】
usingSystem;
classTestSwitch
{
staticvoidMain()
{
Console.WriteLine("服装类别:
1=休闲装2=西装3=皮衣");
Console.Write("请选择类别:
");
strings=Console.ReadLine();
intn=Convert.ToInt16(s);//把数字形式的字符串转化为整型数
intt,cost=0;//t用来记录数量,cost用来记录金额
switch(n)
{
case1:
Console.Write("休闲装的套数:
");
s=Console.ReadLine();
t=Convert.ToInt16(s);
cost=t*150;
break;
case2:
Console.Write("西装的套数:
");
s=Console.ReadLine();
t=Convert.ToInt16(s);
cost=t*300;
break;
case3:
Console.Write("皮衣的件数:
");
s=Console.ReadLine();
t=Convert.ToInt16(s);
cost=t*600;
break;
default:
Console.WriteLine("无效选择,请输入1、2或3!
");
break;
}
if(cost!
=0)
{
Console.WriteLine("应付款{0}元.",cost);
}
Console.WriteLine("谢谢您的惠顾!
");
}
}
【实例2-14】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestWhile
{
publicpartialclassTestWhile:
Form
{
publicTestWhile()//VisualStudio.Net自动生成的构造函数
{
InitializeComponent();
}
privatevoidTestWhile_Load(objectsender,EventArgse)
{
inti,sum;
i=1;//循环变量赋初值
sum=0;
while(i<=100)//循环条件
{//循环体
sum=sum+i;
i++;//改变循环变量的值
}
MessageBox.Show("1到100的自然数之和="+sum);//显示计算结果
}
}
}
【实例2-15】
usingSystem;
classTestDoWhile
{
staticvoidMain()
{
charc;
intn=0;
Console.WriteLine("请输入一行字符:
");
do
{
c=(char)Console.Read();
if(c>='A'&&c<='Z'||c>='a'&&c<='z')
{
n++;
}
}while(c!
='\n');
Console.WriteLine("该行中英文字母的个数为:
{0}",n);
}
}
【实例2-16】
usingSystem;
usingSystem.Windows.Forms;
namespaceTestFor
{
publicpartialclassTestFor:
Form
{
publicTestFor()
{
InitializeComponent();
}
privatevoidTestWhile_Load(objectsender,EventArgse)
{
inti;
intt;
longs1,s2;
s1=t=1;/*百万富翁第一天给陌生人的钱为1分*/
s2=100000;/*陌生人第一天给百万富翁的钱为十万元*/
for(i=2;i<=30;i++)
{
t=t*2;/*百万富翁第i天给陌生人的钱*/
s1=s1+t;/*百万富翁第i天后共给陌生人的钱*/
s2=s2+100000;/*陌生人第i天后共百万富翁的钱*/
}
s1=s1/100;/*将百分富翁给陌生人的分币换成元*/
MessageBox.Show("百万富翁给陌生人"+s1+"元。
\n陌生人给百万富翁"+s2+"元。
");
}
}
}
【实例2-17】
usingSystem;
classTestForeach
{
staticvoidMain()
{
string[]names=newstring[5];
Console.WriteLine("请输入五个人的姓名:
");
for(inti=0;i{
names[i]=Console.ReadLine();
}
Console.WriteLine("已输入的姓名如下,请核对:
");
foreach(stringnameinnames)
{
Console.Write("{0}\t",name);
}
}
}
【实例2-18】
usingSystem;
classTestForeach
{
staticvoidMain()
{
inti,j,k;
for(i=0;i<7;i++)//i代表三角形的第i行
{
for(j=6-i;j>=0;j--)//j表示在第i行左边的第j个空白字符
{
Console.Write("");
}
for(k=0;k<2*i+1;k++)//k表示在第i行的第k个星号字符,
{
Console.Write("*");
}
Console.Write("\n");
}
}
}
【实例2-21】
usingSystem;
classTestGoto
{
staticvoidMain()
{
charc;
for(inti=0;i<80;i++)//最多输入80个字符
{
c=(char)Console.Read();
if(c=='*')break;//一旦输入星号就结束
Console.Write(c);
}
}
}
【实例2-22】
usingSystem;
classTestContinue
{
staticvoidMain()
{
charch_old,ch_new;
ch_old='.';
Console.WriteLine("请输入一串字符,以句号结尾:
");
do
{
ch_new=(char)Console.Read();
if(ch_new==ch_old)
continue;
Console.Write(ch_new);
ch_old=ch_new;
}while(ch_new!
='.');
Console.Write("\n");
}
}