首頁  >  文章  >  後端開發  >  C# 資料型別

C# 資料型別

WBOY
WBOY原創
2024-09-03 15:02:381021瀏覽

顧名思義,資料型別是您要儲存在變數中的資料型別。資料類型用於建議編譯器或解釋器將要處理哪種資料以及該資料需要多少記憶體。

例如: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;

前 3 個 C# 資料型別

C# 資料型別分為三類:

C# 資料型別

1.值類型

  • 直接將變數的值儲存在記憶體中。
  • 接受有符號和無符號文字。

C# 中有兩種值資料型態:

  1. 預先定義資料型,如 int、char、bool 等
  2. 使用者定義的資料型別,如列舉、結構等

2.參考類型

  • 它們儲存變數的位址,即它們包含對變數的引用。
  • 如果一個變數更改了數據,另一個變數將自動取得更新後的值。

C# 中有兩種類型的引用資料型態:

  1. 預先定義類型,如物件、字串
  2. 使用者定義的類型,如類別、介面

3.指標型

  • 它們包含變數的記憶體位址。

指標中使用的符號:

  1. &(&):位址運算符,決定變數的位址
  2. *(星號):間接運算符,存取位址的值

不同資料類型的範例

以下是 C# 中不同資料類型的一些範例,

範例#1:一些值類型

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);
}
}

輸出:

C# 資料型別

範例 #2:Bool、Enum 和 Struct 資料型別

結構體是一種複合類型,用於儲存不同資料類型的相關資料。枚舉用於為整數常數指派名稱。

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);
}
}

輸出:

C# 資料型別

範例#3:參考資料型別

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());
}
}

輸出:

C# 資料型別

範例 #4:介面

介面可以將屬性、方法、事件和索引器作為其成員。它僅包含其成員的聲明。其成員的實作由隱式或顯式實作它的類別提供。

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();
}
}

輸出:

C# 資料型別

範例#5:委託

委託是一個保存方法引用的物件。

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# 資料型別

結論

  • 值類型儲存在堆疊中。
  • 引用型別儲存在堆中。
  • 當值型別轉換為參考型別時,稱為裝箱(隱式轉換過程).
  • 當引用型別轉換為值型別時,稱為拆箱(明確轉換過程).

以上是C# 資料型別的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:C# 版本下一篇:C# 版本