顾名思义,数据类型是您要存储在变量中的数据类型。数据类型用于建议编译器或解释器将要处理哪种数据以及该数据需要多少内存。
例如:int 是一种存储数值的数据类型,需要四个字节。
由于C#是强类型语言,因此在使用变量或常量之前需要声明其类型。适当使用数据类型可以节省内存并提高应用程序的性能。
语法:
datatype <variable_name> = value;
C# 数据类型示例:
1. int intVal = 55;在此示例中:int 是数据类型,intVal 是变量名称,55 是值。
2. char charVal = ‘A’;
3. string strVal = “你好世界!”;
4. float floatVal = 15.5f;
5. bool boolVal = true;
C# 数据类型分为三类:
C# 中有两种值数据类型:
C# 中有两种类型的引用数据类型:
指针中使用的符号:
以下是 C# 中不同数据类型的一些示例,
using System; public class ValueDataTypes { public static void Main() { //int - 32-bit signed integer type int i = 55; //char - 16-bit Unicode character char ch = 'A'; //short - 16-bit signed integer type short s = 56; //long - 64-bit signed integer type long l = 5564; //uint - 32-bit unsigned integer type uint ui = 100; //ushort - 16-bit unsigned integer type ushort us = 80; //ulong - 64-bit unsigned integer type ulong ul = 3625573; //double - 64-bit double precision floating point type double d = 6.358674532; //float - 32-bit single-precision floating point type //float needs 'f' or 'F' as suffix float f = 2.7330645f; //decimal - 128-bit precise decimal values with 28-29 significant digits //decimal needs 'm' or 'M' as suffix decimal dec = 339.5m; Console.WriteLine("Integer: " + i); Console.WriteLine("Char: " + ch); Console.WriteLine("Short: " + s); Console.WriteLine("Long: " + l); Console.WriteLine("Unsinged integer: " + ui); Console.WriteLine("Unsinged short: " + us); Console.WriteLine("Unsinged long: " + ul); Console.WriteLine("Double: " + d); Console.WriteLine("Float: " + f); Console.WriteLine("Decimal: " + dec); } }
输出:
结构体是一种复合类型,用于存储不同数据类型的相关数据。枚举用于为整型常量分配名称。
using System; public class BoolEnumStruct { //declaring enum enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday }; //declaring structure struct Student { public int Id; public string FirstName; public string LastName; public Student(int id, string fname, string lname) { Id = id; FirstName = fname; LastName = lname; } } public static void Main() { //boolean data type bool flag = true; if(flag) { Console.WriteLine("Bool value: "+flag); Console.WriteLine(); } //Accessing enum value for Friday Console.WriteLine("Enumeration:"); Console.WriteLine(Days.Friday); Console.WriteLine((int)Days.Friday); Console.WriteLine(); //passing values to structure members using constructor Student student = new Student(1, "Riya", "Sen"); Console.WriteLine("Structure Members:"); Console.WriteLine(student.Id); Console.WriteLine(student.FirstName); Console.WriteLine(student.LastName); } }
输出:
using System; public class StrObjDynamic { public static void Main() { string str = "C# "; str += "Data Types"; Console.WriteLine("String: "+str); Console.WriteLine(); //declaring object object obj; obj = 100; Console.WriteLine("Object: "+obj); //displaying type of object using GetType() Console.WriteLine(obj.GetType()); Console.WriteLine(); //declaring dynamic variables dynamic value1 = "Hello World!"; dynamic value2 = 5296; dynamic value3 = 6.5; //displaying actual type of dynamic variables using GetType() Console.WriteLine("Dynamic:"); Console.WriteLine("Type of value1: {0}", value1.GetType().ToString()); Console.WriteLine("Type of value2: {0}", value2.GetType().ToString()); Console.WriteLine("Type of value3: {0}", value3.GetType().ToString()); } }
输出:
接口可以将属性、方法、事件和索引器作为其成员。它仅包含其成员的声明。其成员的实现由隐式或显式实现它的类提供。
using System; interface Shape { void rectangle(); } public class Area : Shape { //implementing interface method public void rectangle() { Console.WriteLine("Area of rectangle is Length * Breadth"); } public static void Main(String[] args) { Area area = new Area(); area.rectangle(); } }
输出:
委托是一个保存方法引用的对象。
using System; public class DelegateDemo { // Declaring delegate public delegate void Sum(int a, int b); public void SumVal(int a, int b) { Console.WriteLine(a +"+"+ b+ " = {0}", a + b); } public static void Main(String[] args) { DelegateDemo delegateDemo = new DelegateDemo(); // Creating object of delegate Sum sum = new Sum(delegateDemo.SumVal); //Passing values to the method using delegate object sum(100, 100); } }
输出:
以上是C# 数据类型的详细内容。更多信息请关注PHP中文网其他相关文章!