顧名思義,資料型別是您要儲存在變數中的資料型別。資料類型用於建議編譯器或解釋器將要處理哪種資料以及該資料需要多少記憶體。
例如: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中文網其他相關文章!