数据类型数组和字符串.ppt
《数据类型数组和字符串.ppt》由会员分享,可在线阅读,更多相关《数据类型数组和字符串.ppt(60页珍藏版)》请在三一办公上搜索。
1、-数据类型、数组和字符串,C#程序设计,数据类型,C#中数据类型代表了要在变量中存储的数据的种类,每一个变量或对象都必须申明为一种数据类型。C#数据类型可分为基本类型(比如int,char,double等)用户自定义的类型(struct,class,interface等),C#基本类型,byte、char、short、int、longfloat、double、decimalboolstring,Integral types,Integral types,char char1=A;/Character literal char char2=x0041;/Hexadecimal char char
2、3=(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.5;/errordecimal myMoney=(decimal)300.5;/okdecimal myMoney=300;/ok,bool,bool类型只有两个取值(true和fal
3、se),using System;public class MyClass static void Main()bool flag=true;Console.WriteLine(flag);char c=0;bool Alphabetic=(c 64,bool,int x=123;if(x)/Invalid in C#printf(The value of x is nonzero.);,int x=123;if(x!=0)/The C#way Console.Write(The value of x is nonzero.);,string,定义并初始化 string s1=“Welcome
4、”;string s2=“To”;string s3=s1+s2+”C#”;,用户自定义类型,struct,class,struct Book public decimal price;public string title;public string author;Book b;b.price=30.5Mb.title=“Gone with the wind”;b.author=“Margaret”;,class Book public decimal price;public string title;public string author;Book b;b=new Book();b.p
5、rice=30.5Mb.title=“Gone with the wind”;b.author=“Margaret”;,值类型和引用类型,从保存的数据形式来看,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、longfloat、double、decimalboolstruct(结
6、构体)enum(枚举),struct(结构体),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”;c=b;,堆内存,b.author,b.title,栈内存,b.price,Margaret,Gone with the wind,30.5,c.author,c.title,c.price,Value Types(值类型),Reference Types(引用
7、类型),引用类型的变量存储的是对象地址,而不是对象本身。,string str1=“Hello”;sting str2;,栈内存,堆内存,str1,0 x123FE,0 x123FE,null,str2,Reference Types(引用类型),引用类型的变量存储的是对象地址,而不是对象本身。,string str1=“Hello”;string str2=null;,栈内存,堆内存,Str1,0 x123FE,0 x123FE,null,Str2,str2=str1;?,Reference Types(引用类型),引用类型的变量存储的是对象地址,而不是对象本身。,string str1=“
8、Hello”;string str2=null;,栈内存,堆内存,Str1,0 x123FE,0 x123FE,0 x123FE,Str2,str2=str1;,Reference Types(引用类型),C#中的引用类型stringclassinterfacedelegate,BankCustomer c;c=new BankCustomer();.c=new BankCustomer();.,Reference Types(引用类型),class,栈内存,堆内存,基本数据类型的别名,C#基本数据类型在名字空间System中的别名,将用户输入的数据转换成int类型,int num;num=C
9、onvert.ToInt32(Console.ReadLine();或:num=int.Parse(Console.ReadLine();,Console.ReadLine()用于从用户那里接受输入作为一个字符串。,类型间转换,字符串类型(string),C#中内置的数据类型string不是一个值类型,而是一个引用类型,它被用来表示字符序列。string将字符串当作一个整体来处理,不能修改串中的字符元素,可看作一个字符串常量。如“This is a string.n”。,定义并初始化 string s;s=“hello,world”;或:string s=“hello,world”;,字符串类
10、型(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标识的对象,字符串的存储形式:int x=7;int y=7;string str1=“abc”;string str2=“123;,栈内存,堆内存,str1,str2,1,2,3,a,b,c,0088:4400,0088:4400,0088:4400,0088:4660,st2、str1标识的对象,y,x,7,7,
11、str1赋给str2后存储器的映象图,字符串类型(string),字符串的连接运算符+:,栈内存,堆内存,3,123,abc,abc123,字符串类型(string),string str1=“abc”;string str2=“123”;string str3=str1+str2;,str1+=str2;?,字符串的连接运算符+:,栈内存,堆内存,3,123,abc,abc123,str1,str2,0088:4400,0088:4400,str3,0088:6600,字符串类型(string),string str1=“abc”;string str2=“123”;string str3=
12、str1+str2;str1+=str2;,abc123,=运算符 它是用来比较两个字符串的串值或串址是否相等。,字符串类型(string),string str1=“abc”;string str2=“a”;str2+=“bc”;if(str1=str2)if(object)str1=(object)str2);,取子串,字符串类型(string),string s1=orange;string s2=red;s1+=s2;System.Console.WriteLine(s1);/outputs orangered s1=s1.Substring(2,5);System.Console.W
13、riteLine(s1);/outputs anger,通过 运算符获取串中单个字符,字符串类型(string),string str=Printing backwards;char x=str4;/x=t;for(int i=0;i str.Length;i+)System.Console.Write(strstr.Length-i-1);/outputs sdrawkcab gnitnirP,用IndexOf()方法,查找子串,字符串类型(string),string s9=Battle of Hastings,1066;/outputs 10System.Console.WriteLin
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据类型 数组 字符串

链接地址:https://www.31ppt.com/p-6296787.html