数据类型、数组和字符串ppt课件.ppt

上传人:牧羊曲112 文档编号:1939673 上传时间:2022-12-27 格式:PPT 页数:65 大小:284.50KB
返回 下载 相关 举报
数据类型、数组和字符串ppt课件.ppt_第1页
第1页 / 共65页
数据类型、数组和字符串ppt课件.ppt_第2页
第2页 / 共65页
数据类型、数组和字符串ppt课件.ppt_第3页
第3页 / 共65页
数据类型、数组和字符串ppt课件.ppt_第4页
第4页 / 共65页
数据类型、数组和字符串ppt课件.ppt_第5页
第5页 / 共65页
点击查看更多>>
资源描述

《数据类型、数组和字符串ppt课件.ppt》由会员分享,可在线阅读,更多相关《数据类型、数组和字符串ppt课件.ppt(65页珍藏版)》请在三一办公上搜索。

1、-数据类型、数组和字符串,C#程序设计,数据类型,C#中数据类型代表了要在变量中存储的数据的种类,每一个变量或对象都必须申明为一种数据类型。C#数据类型可分为内置的类型(比如int,char,double,string等)用户自定义的类型(struct,class,interface等),C#内置类型,byte、char、short、 int、 longfloat、double、decimalboolstringobject,Integral types,Integral types,char char1 = A; / Character literal char char2 = x0041;

2、 / Hexadecimal char char3 = (char)65; / Cast from integral type char char4 = u0041; / Unicode,Floating-point types,float x=3.5; /errorfloat x=3.5F; /okfloat x=(float)3.5; /okdouble y=3; /ok,decimal,decimal myMoney=300.5m; /okdecimal myMoney =300.5F; /error decimal myMoney=300.5; /error和浮点数之间不存在隐式转换

3、decimal myMoney=(decimal)300.5; /okdecimal myMoney=300; /ok,bool,bool类型只有两个取值(true和false),using System; public class MyClass static void Main() bool flag = true; Console.WriteLine(flag); char c = 0; /c的ASCII码的数值=? bool Alphabetic = (c 64 ,bool,int x = 123; if (x) / Invalid in C# printf(The value of

4、x is nonzero.); ,int x = 123; if (x!=0) / The C# way Console.Write(The value of x is nonzero.); ,数据类型,从保存的数据形式来看,C#数据类型又可分为Value Types(值类型)Reference Types(引用类型),Value Types(值类型),值类型的变量存储的是对象本身。,int num1;int num2;num1=7;num2=num1;,栈内存,堆内存,num1,num2,7,Value Types(值类型),C#中的值类型byte、char、short、 int、 long

5、float、double、decimalboolstruct(结构体)enum(枚举),struct(结构体),public struct Book public decimal price;public string title;public string author; Book b, c;b.price = 30.5Mb.title = “Gone with the wind”;b.author = “Margaret”;,堆内存,b.author,b.title,栈内存,b.price,Margaret,Gone with the wind,30.5,c.author,c.title,

6、c.price,Value Types(值类型),Reference Types(引用类型),引用类型的变量存储的是对象地址,而不是对象本身。,string Str1 = “Hello”;string Str2 = null;,栈内存,堆内存,Str1,0 x123FE,0 x123FE,null,Str2,Str2 = Str1; ?,Reference Types(引用类型),引用类型的变量存储的是对象地址,而不是对象本身。,string Str1 = “Hello”;string Str2 = null;,栈内存,堆内存,Str1,0 x123FE,0 x123FE,0 x123FE,S

7、tr2,Str2 = Str1;,Reference Types(引用类型),C#中的引用类型string ?objectclassinterfacedelegate,BankCustomer c;c = new BankCustomer();.c = new BankCustomer();.,Reference Types(引用类型),class,栈内存,堆内存,方法中的参数,C#中方法参数既可以是值类型也可是引用类型,/ Passing by value static void Square( int x) / code. ,/ Passing by referencestatic voi

8、d Square(ref int x) / code. ,方法中的参数,Example1:参数传值类型,class PassingValByVal static void SquareIt(int x) x *= x; System.Console.WriteLine(The value inside the method: 0, x); static void Main() int n = 5; System.Console.WriteLine(The value before calling the method: 0, n); SquareIt(n); / Passing the var

9、iable by value. System.Console.WriteLine(The value after calling the method: 0, n); 考虑程序运行的结果?,方法中的参数,Example2:参数传引用类型,class PassingValByRef static void SquareIt(ref int x) x *= x; System.Console.WriteLine(The value inside the method: 0, x); static void Main() int n = 5; System.Console.WriteLine(The

10、 value before calling the method: 0, n); SquareIt(ref n); / Passing the variable by reference. System.Console.WriteLine(The value after calling the method: 0, n); 考虑程序运行的结果?,方法中的参数,思考题:写一方法交换两个字符串?,class SwappingStrings static void SwapStrings( ? s1 , ? s2 ) string temp = s1; s1 = s2; s2 = temp; Sys

11、tem.Console.WriteLine(Inside the method: 0 1, s1, s2); ,方法中的参数,关键字out:参数也是传递引用,它与ref唯一不同是,ref在传参前必须初始化,而out不需要。,class OutExample static void Method(out int i) i = 44;static void Main() int value; /无需初始化 Method(out value); / value is now 44 ,class OutExample static void Method(ref int i) i = 44;stati

12、c void Main() int value = 0; /必须 Method(ref value); / value is now 44 ,内置数据类型的别名,C#内置的数据类型在名字空间System中的别名,将用户输入的数据转换成int类型,int num;num=Convert.ToInt32(Console.ReadLine();也可以:num=int.Parse (Console.ReadLine();,类型间转换,例:声明了Car 类并为它创建了对象 MyCar:,using System;class Carstring Engine;int NoOfWheels;public v

13、oid AcceptDetails()Console.WriteLine(Enter the Engine Model);Engine = Console.ReadLine();Console.WriteLine(Enter the number of Wheels);NoOfWheels = Convert.ToInt32(Console.ReadLine(); public void DisplayDetails()Console.WriteLine(The Engine Model is:0, Engine);Console.WriteLine(wheels are:0, NoOfWhe

14、els);,class ExecuteClasspublic static void Main(string args)Car MyCar = new Car();MyCar.AcceptDetails();MyCar.DisplayDetails();,1.将文本编辑器中编写的代码保存为扩展名为 .cs 的文件。2.要编译代码,需要转至Visual Studio 2005 命令提示符。选择开始所有程序 Visual Studio 2005 Visual Studio 工具 Visual Studio 2005 命令提示符以编译该程序。在 Visual Studio 2005 命令提示符窗口中

15、,转至保存该程序文件的位置。4. 使用以下命令编译该程序文件:csc ExecuteClass.cs5.要执行该代码,请在命令提示符下输入以下内容:ExecuteClass.exe,需要执行以下步骤来编译并执行C# 程序:,数组(Array),数组类型属于复合数据类型,它是由一组顺序存储的同一类型的元素组成的数据集合。特点: 元素类型相同; 元素有顺序; 所有元素共用一个名称。,数组的声明,格式: type arrayName;举例: int num; double array_double; string str; Point P;,数组的创建,在C#中,声明数组时不能指定它的长度,而是利用

16、new 运算符来为数组型变量分配内存空间,我们将其称之为创建数组。 num = new int3; array_double = new double1000; string str = new string5;数组创建后,系统自动为数组元素赋初值。,数组元素默认值,数组创建后的元素默认初值。,数组(Array),基本数据类型一维数组内存分配,栈内存,堆内存,num,0,0,0,0088:4400,0088:4400,new int3产生的对象,int num; /num=?num=new int3;,数组(Array),基本数据类型一维数组内存分配,栈内存,堆内存,num,0,0,0,nul

17、l,0088:4400,new int3产生的对象,int num;num=new int3;num=null;,数组(Array),基本数据类型一维数组初始化,class TestArrays public static void Main()int array1; array1=new int3; /默认值为0 int array2=new int3; int array3=5,9,10; int array4=new int35,9,10; int array5=array4; /两数组指向同一内存区,数组(Array),对象数组的内存分配,class Student private s

18、tring name; private int age; public Student(string name, int age) this.name = name; this.age = age; ,栈内存,堆内存,students,null,Student students;,数组(Array),对象数组的内存分配,堆内存,students,0088:4400,0088:4400,new students3产生的对象,null,null,Student students;students=new Student3;,null,栈内存,数组(Array),数组(Array),堆内存,stud

19、ents,0088:4400,0088:4400,new students3产生的对象,null,null,Student students;students=new Student3;students0=new Student(“lisi”,18);,0088:4660,栈内存,student0标识的Student对象,lisi,18,0088:4660,对数组元素访问,class TestArrays public static void Main()string friendsNames=“Tom”,”Mary”,”Yorck”;Console.WriteLine(“Here are 0

20、 of my friends”, friendsNames.Length);for(int i=0;ifriendsNames.Length;i+)Console.WriteLine(friendsNamesi);Console.ReadKey();,对数组元素访问,语法规则:foreach( in )/can use for each elements,用foreach循环访问数组,对数组元素访问,用foreach循环访问数组,class TestArrays public static void Main()string friendsNames=“Tom”,”Mary”,”Yorck”;

21、Console.WriteLine(“Here are 0 of my friends”, friendsNames.Length);foreach(string name in friendsNames)Console.WriteLine(name);,多维数组(矩形数组),C#可以创建多维数组,最常见的多维数组是二维数组(矩形数组) , 每行元素相同。,格式: type, arrayName;举例: int, num; double, array_double;,多维数组(矩形数组),Class TestArrays public static void Main()/ Declare a

22、 two dimensional array int, array1 = new int2, 3;/ Declare and set array element values int, array2 = new int2,3 1, 2, 3 , 4, 5, 6 ; int, array3 = 1, 2, 3 , 4, 5, 6 ; ,二维数组的定义和初始化,多维数组(矩形数组),Class TestArrays public static void Main()/ Declare and set array element values int, array2 = new int2,3 1,

23、2, 3 , 4, 5, 6 ; for (int i = 0; i 2; i+) for(int j = 0; j 3; j+) Console.WriteLine(arrayi , j); ,二维数组的访问(for循环),多维数组(矩形数组),Class TestArrays public static void Main()/ Declare and set array element values int, array2 = new int2,3 1, 2, 3 , 4, 5, 6 ; foreach(int elem in array2)Console.WriteLine(elem)

24、;,二维数组的访问(foreach循环),变长数组(数组的数组),Class TestArrays public static void Main()/ Declare a jagged array int jaggedArray = new int3;/ Set the values of the first array in the jagged array structure jaggedArray0 = new int5 1, 3, 5, 7, 9 ; jaggedArray1 = new int4 0, 2, 4, 6 ;jaggedArray2 = new int2 -1, 99

25、;,变长数组的定义和初始化,变长数组(数组的数组),Class TestArrays public static void Main()/ Declare and Initalize a jagged array int jaggedArray = new intnew int 1, 3, 5, 7, 9 ; new int 0, 2, 4, 6 ;new int -1, 99 ;,变长数组的定义和初始化,变长数组(数组的数组),/ Declare and Initalize a jagged array int jaggedArray = new intnew int 1, 3, 5, 7,

26、 9 ; new int 0, 2, 4, 6 ;new int -1, 99 ;for(int i = 0; i jaggedArray.Length; i+)for(int j = 0 j jaggedArrayi.Length; j+) Console.Write(jaggedArrayij);,变长数组访问(for循环),变长数组(数组的数组),/ Declare and Initalize a jagged array int jaggedArray = new intnew int 1, 3, 5, 7, 9 ; new int 0, 2, 4, 6 ;new int -1, 99

27、 ;foreach(int singleArray in jaggedArray )foreach(int elem in singleArray) Console.Write( elem );,变长数组访问(foreach循环),Array类,C#中,Array类是所有数组的基类Array 类提供能够使用数组的属性和方法。属性:下表介绍一些最常用的Array 类的属性,Array类,Array 类提供能够使用数组的属性和方法。方法:下表介绍一些最常用的Array 类的方法,字符串类型(string),C#中内置的数据类型string不是一个值类型,而是一个引用类型,它被用来表示字符序列。st

28、ring将字符串当作一个整体来处理,不能修改串中的字符元素,可看作一个字符串常量。如“This is a string.n”。,定义并初始化string s = “hello,world”;串联接与取子串,字符串类型(string),string s1 = orange; string s2 = red; s1 += s2; System.Console.WriteLine(s1); / outputs orangered s1 = s1.Substring(2, 5);System.Console.WriteLine(s1); / outputs anger,字符串的存储形式: int x=

29、7; int y=7; string str1 = “abc”; string str2 = “123;,字符串类型(string),str2 = str1; /赋值后存储器的映象又如何?,字符串类型(string),栈内存,堆内存,str1,str2,1,2,3,a,b,c,0088:4400,0088:4400,0088:4660,0088:4660,str1标识的对象,y,x,7,7,str2标识的对象,栈内存,堆内存,str1,str2,1,2,3,a,b,c,0088:4400,0088:4400,0088:4400,0088:4660,st2、str1标识的对象,y,x,7,7,s

30、tr1赋给str2后存储器的映象图,字符串类型(string),字符串的连接运算符+:,栈内存,堆内存,字符串类型(string),string str1 = “abc”;string str2 = “123”;string str3 = str1+str2;,str1+=str2; ?,123,abc,abc123,0088:4400,0088:5500,0088:6600,字符串的连接运算符+:,栈内存,堆内存,123,abc,abc123,str1,str2,0088:7700,0088:5500,str3,0088:6600,字符串类型(string),string str1 = “a

31、bc”;string str2 = “123”;string str3 = str1+str2;str1 += str2;,abc123,0088:4400,0088:5500,0088:6600,0088:7700,=运算符:比较两个字符串是否相等 串值相等(内容相同)或串址相等(同一对象) 。,字符串类型(string),string str1 = “abc”; string str2 = “a”;str2+=“bc”; if(str1= =str2)/串值相等if(object)str1= =(object)str2);,通过 运算符获取串中单个字符,字符串类型(string),stri

32、ng s5 = Printing backwards; char x= s54; /x= t;for (int i = 0; i s5.Length; i+) System.Console.Write(s5s5.Length - i - 1); / outputs sdrawkcab gnitnirP,用IndexOf()方法,查找子串,字符串类型(string),string s9 = Battle of Hastings, 1066; / outputs 10System.Console.WriteLine(s9.IndexOf(Hastings); / outputs -1System.

33、Console.WriteLine(s9.IndexOf(1967);,用Split()方法分解串中单词,字符串类型(string),char delimit = new char ; string s10 = The cat sat on the mat.;string words = s10.Split(delimit);foreach (string substr in words) System.Console.WriteLine(substr);,输出结果Thecatsatonthemat.,字符串类型(string),在 .NET Framework中string是类String的别名方法:下表介绍一些最常用的String 类的方法,1、将任意一字符串反序输出,并且每个字符都大写。2、求将某一字符串str中的所有子序列smod都删除后所得的新串newStr? 例如: str = “H234llo,Wo234rld234!”; smod = “234”;求新串 newStr ?,字符串类型(string),思考题?,Thank You!,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号