您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 信息化管理 > 数据类型、数组和字符串
--数据类型、数组和字符串C#程序设计数据类型C#中数据类型代表了要在变量中存储的数据的种类,每一个变量或对象都必须申明为一种数据类型。C#数据类型可分为内置的类型(比如int,char,double,string等)用户自定义的类型(struct,class,interface等)C#内置类型byte、char、short、int、longfloat、double、decimalboolstringobjectIntegraltypesTypeRangeSuffixExampleSize(bytes)sbyte-128to127No1001byte0to255No1001charU+0000toU+ffffNo‘A’,’\n’,’\t’\u0041,\x00412short-32,768to32,767No1002ushort0to65,535no、u或U100u2int-2,147,483,648to2,147,483,647No1004uint0to4,294,967,295No、u或U100u4long-9,223,372,036,854,775,808to9,223,372,036,854,775,807No、l、L100、100L8ulong0to18,446,744,073,709,551,615No、u、U、l、L100U、100UL8IntegraltypesTypeRangeSuffixExampleSize(bytes)……………charU+0000toU+ffffNo‘A’,’\n’\u00412……………•charchar1=‘A';//Characterliteral•charchar2='\x0041';//Hexadecimal•charchar3=(char)65;//Castfromintegraltype•charchar4='\u0041';//UnicodeFloating-pointtypesTypeApproximaterangeSuffixExamplePrecisionfloat±1.5e−45to±3.4e38f或F1.5F7significantdigitsdouble±5.0e−324to±1.7e308d或D1.515-16digitsdecimal±1.0×10e−28to±7.9×10e28m或M1.5M28-29digits•floatx=3.5;//error•floatx=3.5F;//ok•floatx=(float)3.5;//ok•doubley=3;//okdecimalTypeApproximateRangeSuffixExamplePrecisiondecimal±1.0×10e−28to±7.9×10e28m或M300.5M28-29significantdigits•decimalmyMoney=300.5m;//ok•decimalmyMoney=300.5F;//errordecimalmyMoney=300.5;//error和浮点数之间不存在隐式转换•decimalmyMoney=(decimal)300.5;//ok•decimalmyMoney=300;//okboolbool类型只有两个取值(true和false)usingSystem;publicclassMyClass{staticvoidMain(){boolflag=true;Console.WriteLine(flag);charc=‘0’;//c的ASCII码的数值=?boolAlphabetic=(c64&&c123);Console.WriteLine(Alphabetic);}}boolintx=123;if(x)//InvalidinC#{printf(Thevalueofxisnonzero.);}intx=123;if(x!=0)//TheC#way{Console.Write(Thevalueofxisnonzero.);}数据类型从保存的数据形式来看,C#数据类型又可分为ValueTypes(值类型)ReferenceTypes(引用类型)ValueTypes(值类型)值类型的变量存储的是对象本身。intnum1;intnum2;num1=7;num2=num1;栈内存堆内存num1num277copyValueTypes(值类型)C#中的值类型byte、char、short、int、longfloat、double、decimalboolstruct(结构体)enum(枚举)struct(结构体)publicstructBook{publicdecimalprice;publicstringtitle;publicstringauthor;}Bookb,c;b.price=30.5Mb.title=“Gonewiththewind”;b.author=“Margaret”;堆内存b.authorb.title栈内存b.priceMargaretGonewiththewind30.5c.authorc.titlec.priceValueTypes(值类型)ReferenceTypes(引用类型)引用类型的变量存储的是对象地址,而不是对象本身。stringStr1=“Hello”;stringStr2=null;栈内存堆内存Str10x123FEHELLO012340x123FEnullStr2Str2=Str1;?ReferenceTypes(引用类型)引用类型的变量存储的是对象地址,而不是对象本身。stringStr1=“Hello”;stringStr2=null;栈内存堆内存Str10x123FEHELLO012340x123FE0x123FEStr2Str2=Str1;ReferenceTypes(引用类型)C#中的引用类型string?objectclassinterfacedelegateBankCustomerc;c=newBankCustomer();...c=newBankCustomer();...KimLee1000.00BobSmith500.31ReferenceTypes(引用类型)class栈内存堆内存方法中的参数C#中方法参数既可以是值类型也可是引用类型//PassingbyvaluestaticvoidSquare(intx){//code...}//PassingbyreferencestaticvoidSquare(refintx){//code...}方法中的参数Example1:参数传值类型classPassingValByVal{staticvoidSquareIt(intx){x*=x;System.Console.WriteLine(Thevalueinsidethemethod:{0},x);}staticvoidMain(){intn=5;System.Console.WriteLine(Thevaluebeforecallingthemethod:{0},n);SquareIt(n);//Passingthevariablebyvalue.System.Console.WriteLine(Thevalueaftercallingthemethod:{0},n);}}考虑程序运行的结果?方法中的参数Example2:参数传引用类型classPassingValByRef{staticvoidSquareIt(refintx){x*=x;System.Console.WriteLine(Thevalueinsidethemethod:{0},x);}staticvoidMain(){intn=5;System.Console.WriteLine(Thevaluebeforecallingthemethod:{0},n);SquareIt(refn);//Passingthevariablebyreference.System.Console.WriteLine(Thevalueaftercallingthemethod:{0},n);}}考虑程序运行的结果?方法中的参数思考题:写一方法交换两个字符串?classSwappingStrings{staticvoidSwapStrings(?s1,?s2){stringtemp=s1;s1=s2;s2=temp;System.Console.WriteLine(Insidethemethod:{0}{1},s1,s2);}}方法中的参数关键字out:参数也是传递引用,它与ref唯一不同是,ref在传参前必须初始化,而out不需要。classOutExample{staticvoidMethod(outinti){i=44;}staticvoidMain(){intvalue;//无需初始化Method(outvalue);//valueisnow44}}classOutExample{staticvoidMethod(refinti){i=44;}staticvoidMain(){intvalue=0;//必须Method(refvalue);//valueisnow44}}内置数据类型的别名C#TypeNETFrameworkTypeC#Type.NETFrameworkTypeboolSystem.BooleanuintSystem.UInt32byteSystem.BytelongSystem.Int64sbyteSystem.SByteulongSystem.UInt64charSystem.CharobjectSystem.ObjectdecimalSystem.DecimalshortSystem.Int16doubleSystem.DoubleushortSystem.UInt16floatSystem.SinglestringSystem.StringintSystem.Int32C#内置的数据类型在名字空间System中的别名将用户输入的数据转换成int类型intnum;num=Convert.ToInt32(Console.ReadLine());也可以:num=int.Parse(Console.ReadLine());Console.ReadLine()从用户那里接受输入返回一个字符串。类型间转换例:声明了Car类并为它创建了对象MyCar:usingSystem;classCar{stringEngine;intNoOfWheels;publicvoidAcceptDetails(){Console.WriteLine(EntertheEngineModel);Engine=Console.ReadLine();Console.WriteLine(EnterthenumberofWheels);NoOfWheels=Convert.ToInt32(Console.ReadLine());}publicvoidDisplayDetails(){Console.WriteLine(TheEngineModelis:{0},Engine);Console.WriteLine(wheelsare:{0},NoOfWheels);}}classExecuteClass{publicstaticvoidMain(string[]args){CarMyCar=newCar();MyCar.AcceptDetails();MyCar.DisplayDetails();}}1.将文本编辑器中编写的代码保存为扩展名为.cs的文件。2.要编译代码,需要转至VisualStudio2005命令提示符。选择开始所有程序VisualStudio2005VisualStudio工具VisualStudio2005命令提示符以编译该程序。3.在VisualStu
本文标题:数据类型、数组和字符串
链接地址:https://www.777doc.com/doc-4681073 .html