第3章 C#程序设计(5学时-自学为主).ppt
《第3章 C#程序设计(5学时-自学为主).ppt》由会员分享,可在线阅读,更多相关《第3章 C#程序设计(5学时-自学为主).ppt(100页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、.NET架构架构主讲教师 张智计算机学院.NET架构第3章 C#程序设计nC#是一种面向对象的编程语言。nC#源程序扩展名为.cs,.NET提供的编译器是csc.exe。nC#语言在类型安全性、版本转换、事件和垃圾回收等方面进行了相当大的改进和创新。.NET架构第3章 C#程序设计3.1 C#程序基本结构3.2 C#数据类型和运算符3.3 C#基本输入输出3.4 C#流程控制语句3.5 C#数组3.6 动态数组ArrayList.NET架构3.1 C#程序基本结构using System;namespace HelloWorld class Program static void Main(s
2、tring args)System.Console.WriteLine(Hello World);.NET提供许多功能类,以方便应用程序使用,将这些类依照功能分门别类形成命名空间。使用System命名空间定义命名空间HelloWorld(非必须)程序入口定义一个类.NET架构多个类的C#程序using System;namespace HelloWorld /命名空间class TestClass private int a;/成员变量 public TestClass(int x)a=x;/构造函数 public void show()Console.WriteLine(a);/成员函数(方
3、法)class Class1 static void Main(string args)TestClass A=new TestClass(100);/构造对象(实例化)A.show();.NET架构了解命名空间using System;namespace HelloWorld class TestClass private int a;public TestClass(int x)a=x;public void show()Console.WriteLine(a);namespace HelloWorld2 class Class1 static void Main(string args)
4、HelloWorld.TestClass A=new HelloWorld.TestClass(100);A.show();【返回】命名空间.NET架构3.2 C#数据类型和运算符1.数据类型和运算符2.用法示例【返回】.NET架构1.数据类型和运算符nC#两个基本类别 n值类型-int、double、char、枚举类型、结构体等-表示实际数据,只是将值存放在内存中 -值类型都存储在堆栈中n引用类型-类、接口、数组、字符串等-表示指向数据的指针或引用-包含内存堆中对象的地址 -为 null,则表示未引用任何对象.NET架构类类 别别说说 明明值类型简单类型有符号整型:sbyte,short,i
5、nt,long无符号整型:byte,ushort,uint,ulongUnicode 字符:charIEEE浮点型:float,double高精度小数:decimal布尔型:bool 枚举类型enum E.结构类型struct S.引用类型类类型所有其他类型的最终基类:objectclass C.接口类型interface I.Unicode字符串string数组类型一维和多维数组,例如 int 和 int,委托类型delegate T D(.)形式的用户定义的类型.NET架构类型说明类型说明sbyte有符号8位整数:-128 到 127byte无符号8位整数:0 到 255char16位Un
6、icode字符:0 到 65535short有符号16位整数:-32,768 到 32,767ushort无符号16位整数:0 到 65,535int 有符号32位整数:-2,147,483,648 到 2,147,483,647uint无符号32位整数:0 到 4,294,967,295long 有符号64位整数:-9,223,372,036,854,775,808到9,223,372,036,854,775,807ulong无符号64位整数:0 到 18,446,744,073,709,551,615Unicode是一种通用字符编码标准,它覆盖了多国语言和符号,兼容ASCII字符(前128
7、个相同)。.NET架构类型说明类型说明float 单精度32位浮点数(精度7位):1.5 1045 到 3.41038double 双精度64位浮点数(精度15-16位):5.0 10324 到 1.710308decimal128位数据类型(精度28-29位):1.0 1028到7.91028(财务/货币)bool 布尔变量(true 或 false),显示时为True或Falsestring 表示一个 Unicode 字符的字符串object可将任何类型的值赋给 object 类型的变量.NET架构n C#运算符运算符类别运算符类别运算符运算符算术+-*/%逻辑(布尔型和按位)&|!&|t
8、ruefalse字符串串联+递增、递减+-移位关系=!=赋值=+=-=*=/=%=&=|=成员访问.索引 转换()条件?:委托串联和移除+-间接寻址和地址*-&创建对象new类型信息sizeoftypeof asis溢出异常控制checkedunchecked【返回】.NET架构用法示例数值型static void Main(string args)bool flag=true;short a=19;int i=(int)3.0;float f=3.14F;string str=Tom;Console.WriteLine(布尔值 =+flag);Console.WriteLine(短整型值 =
9、+a);Console.WriteLine(整型值 =+i);Console.WriteLine(浮点值 =+f);Console.WriteLine(字符串值 =+str);int i=3.0;float f=3.14;double f=3.14;float f=(float)3.14;.NET架构用法示例数值型static void Main(string args)const float PI=3.14F;const float G=980;int length=40;double period=0;period=2*PI*System.Math.Sqrt(length/G);Conso
10、le.WriteLine(钟摆的周期为+period+秒);符号常量问题:如何设置输出格式输出结果:钟摆的周期为1.26875163834526秒.NET架构关于输出格式化和多个数据的输出 int i=123;double j=123.45;Console.WriteLine(i,j);Console.WriteLine(i=0,j=1,i,j);/结果 i=123,j=123.45 Console.WriteLine(i=0,4,j=1,-7,i,j);/结果 i=123,j=123.45 Console.WriteLine(j=0,7:f1,j);/j=123.5 Console.Writ
11、eLine(j=0:f1,j);/j=123.5 /f表示浮点数,1表示小数位数(四舍五入)Console.WriteLine(i=0,7:x,i);/i=7b Console.WriteLine(i=0:x,i);/i=7b /x表示16进制数4,7代表宽度,负号表示左对齐.NET架构用法示例bool型基本用法:bool b1=true;/不是Truebool b2=(x=1&x=5);示例:if(b2)else int x=0,y;if(x)y=0;else y=1;思考结果如何.NET架构注意int x=0;if(x)可用 if(x!=0)进行显式比较或者用转换函数if(System.C
12、onvert.ToBoolean(x)/非0为true无法将类型int隐式转换为bool.NET架构用法示例字符型char ch=A;char ch=101;/用8进制数表示ASCII字符,最多3位char ch=x41;/用2位16进制数表示ASCII字符char ch=x0041;/用低2位16进制数表示ASCII字符char ch=u0041;/Unicode字码,必须4位16进制数.NET架构注意问题ch=a;/无法将int隐式转换为char改正:char ch=(char)a;int a=A;/int a=65;char ch;ch=a;a=ch+;Console.WriteLine
13、(a);.NET架构程序练习using System;public class BoolTest public static void Main()Console.Write(Enter a character:);char c=Console.Read();/读入一个字符(返回值为int型)if()if()Console.WriteLine(小写字母.);else Console.WriteLine(大写字母.);else Console.WriteLine(不是字母.);.NET架构常用方法常用方法说明说明IsControl 字符是否属于控制字符类别。IsDigit 字符是否属于十进制数字
14、类别。IsLetter 字符是否属于字母类别。IsLetterOrDigit 字符是属于字母类别还是属于十进制数字类别。IsLower 字符是否属于小写字母类别。IsNumber 字符是否属于数字类别。IsPunctuation 字符是否属于标点符号类别。IsSeparator 字符是否属于分隔符类别。IsSymbol 字符是否属于符号字符类别。IsUpper 字符是否属于大写字母类别。IsWhiteSpace 字符是否属于空白类别。ToLower 将字符的值转换为它的小写等效项。ToUpper 将字符的值转换为它的大写等效项。用法:char.方法名(参数).NET架构参考答案using Sy
15、stem;public class BoolTest public static void Main()Console.Write(Enter a character:);char c=(char)Console.Read();/读入一个字符(返回值为int型)if(char.IsLetter(c)if(char.IsLower(c)Console.WriteLine(小写字母.);else Console.WriteLine(大写字母.);else Console.WriteLine(不是字母.);.NET架构用法示例string型string a=hello;string b=h;b+=e
16、llo;/+是连接字符串,b=hellostring c=good +morning;Console.WriteLine(a!=b);/输出结果是False Console.WriteLine(object)a=b);/结果是False(类型不一致了).NET架构编程练习n输入一行字符串,统计字母、数字、空格和其他字符个数。.NET架构public static void Main()int letters=0,digits=0,spaces=0,others=0;Console.WriteLine(请输入一个字符串:);_;for(_)if(_)letters+;else if(_)digi
17、ts+;else if(_)spaces+;else others+;Console.WriteLine(字母的个数为:0,letters);Console.WriteLine(数字的个数为:0,digits);Console.WriteLine(空格的个数为:0,spaces);Console.WriteLine(其他字符的个数为:0,others);.NET架构常用方法常用方法说明说明CompareTo字符串比较:s1.CompareTo(s2)-1(s1s2)Concat 字符串连接(string.Concat(s1,s2)Contains 返回true|false,指示指定的字符串是否
18、出现在此字符串中。Equals 确定两个字符串是否具有相同的值。IndexOf 获得一个或多个字符在此字符串中的第一个匹配项的索引值。Insert 在字符串中指定索引位置插入一个指定的字符串。LastIndexOf 获得字符或字符串在此字符串中的最后一个匹配项的索引值。Remove 从此字符串中删除指定个数的字符。Replace 将字符串所有匹配项替换为其他指定的字符或串。Substring检索子字符串。s1.Substring(startIndex,length)ToLower 字符串转换为小写形式。ToUpper 字符串转换为大写形式。Trim移除所有前导匹配项和尾部匹配项。.NET架构可
19、以用索引运算符 访问字符串中的各个字符:char x=test2;/x=s;序号从0开始string s=u0041;char c1=s0,c2=s2;/则c1,c2=?示例:string s=Printing backwards;/汉字能行吗?for(int i=0;i s.Length;i+)System.Console.Write(ss.Length-i-1);用法示例string型字符串的长度属性.NET架构用法示例string型字符串中可以包含转义符,如:string hello=HellonWorld!;练习:c:myFoldermyFile.txtstring s1=c:myFo
20、ldermyFile.txt;/string s2=c:myFoldermyFile.txt;/不易阅读改进:C#字符串可以开头,并用双引号引起来:string s3=c:myFoldermyFile.txt;.NET架构注意:若要在一个用 引起来的字符串中包括一个双引号,则应使用两个双引号:例如:You!cried the captain.则用:You!cried the captain.2个双引号才表示1个.NET架构字符串转换为其它型的方法1.Parse()方法 很重要2.Sytem.Convert.()方法.NET架构1.Parse()方法nint.Parse(string)nlong
21、.Parse(string)nfloat.Parse(string)ndouble.Parse(string)nbool.Parse(string)nchar.Parse(string)nDateTime.Parse(string)string s1=123;int a=int.Parse(s1);string s2=123.45;double f=double.Parse(s2);string s3=2008/03/15;DateTime dt=DateTime.Parse(s3);Console.WriteLine(dt.Year);.NET架构2.Sytem.Convert.()方法方法
22、描述ToInt32转换为 32 位有符号整数(int)。ToInt16转换为 16 位有符号整数。ToDouble转换为双精度浮点数字。ToSingle转换为单精度浮点数字。ToUInt32转换为 32 位无符号整数。ToUInt16转换为 16 位无符号整数。ToBoolean转换为等效的布尔值。ToChar转换为 Unicode 字符。ToDateTime将指定的值转换为 DateTime。.NET架构示例string s1=123;int a=System.Convert.ToInt32(s1);/a=123string s2=123.45;double f=Convert.ToDoub
23、le(s2);/f=123.45string s=true;/不区分大小写bool b=Convert.ToBoolean(s);string s=2008/03/15;/一种日期格式DateTime dt=Convert.ToDateTime(s);Console.WriteLine(dt.Year);using System;.NET架构补充n将数字转换为字符串时,需要使用ToString()方法,或者使用Convert.ToString()方法。n例如:int i=123;textBox1.Text=Convert.ToString(i);textBox2.Text=i.ToString
24、();【返回】.NET架构3.3 C#基本输入输出输出语句:System.Console.Write();System.Console.WriteLine();/有换行输入语句:System.Console.ReadLine();/读取一行字符System.Console.Read();/读取一个字符(返回值为int型)【返回】.NET架构Console.ReadLine()示例static void Main(string args)Console.Write(请输入姓名:);string a=Console.ReadLine();Console.WriteLine(欢迎 +a+光临);连接
25、符.NET架构static void Main(string args)Console.Write(请输入a=);string a=Console.ReadLine();Console.Write(请输入b=);string b=Console.ReadLine();Console.WriteLine(a+b=+(a+b);字符串的连接.NET架构static void Main(string args)Console.Write(请输入a=);string a=Console.ReadLine();Console.Write(请输入b=);string b=Console.ReadLine(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 第3章 C#程序设计5学时-自学为主 C# 程序设计 学时 自学 为主
限制150内