在 C# 中將字串轉換為型別
在這篇文章中,我將解釋如何將字串轉換為 type 或在 C# 中獲取資料型別。為了確定值型別,我們將使用 .GetType 函式。
在 C# 中獲取 String 值的 type
下面的示例獲取字串的 runtime 型別和其他值,然後繼續獲取每個值的型別。
建立一個名為 StringtoType 的類和一個 Main() 方法。
class StringtoType {
public static void Main() {}
}
接下來,建立一個名為 allvalues 的 Object[] 型別變數,併為其賦予一些值,例如 "Abc"(作為字串)和 89(作為位元組)。
object[] allvalues = { "Abc",
(long)193527,
"Happy Programming",
(byte)89,
'Z',
(sbyte)-11,
"Zeelandia is 8th continent",
27.9,
"I am a string line",
(int)20,
'7' };
然後,使用 foreach 迴圈,驗證陣列中的每個條目以確定其型別。
foreach (var data in allvalues) {
}
在 foreach 迴圈中初始化型別為 Type 的名為 t 的變數。變數 t 將在 data.GetType() 方法的幫助下儲存陣列 allvalues 中存在的每個值的資料型別。
Type t = data.GetType();
之後,我們將應用 if 條件來檢查每個值是否都是字串。
if (t.Equals(typeof(string)))
如果發現值是字串,則會顯示如下訊息。
'Happy Programming' is a String
在將字串轉換為 Type 和其他型別的資料後,如 byte、sbyte、int 和 double 等,藉助以下 else if 檢查:
else if (t.Equals(typeof(sbyte))) Console.WriteLine(" '{0}' is a Signed Byte", data);
else if (t.Equals(typeof(byte))) Console.WriteLine(" '{0}' is a Byte", data);
else if (t.Equals(typeof(int))) Console.WriteLine(" '{0}' is an Integer of 32-bit", data);
else if (t.Equals(typeof(long))) Console.WriteLine(" '{0}' is an Integer of 64-bit", data);
else if (t.Equals(typeof(double))) Console.WriteLine("'{0}' is a double ", data);
最後,如果在陣列 allvalues 中發現除了 byte、sbyte、int、double 和 long 型別的資料,它將簡單地顯示如下訊息:
'Z' is another type of data
完整程式碼示例:
using System;
using System.Diagnostics;
class StringtoType {
public static void Main() {
object[] allvalues = { "Abc",
(long)193527,
"Happy Programming",
(byte)89,
'Z',
(sbyte)-11,
"Zeelandia is 8th continent",
27.9,
"I am a string line",
(int)20,
'7' };
foreach (var data in allvalues) {
Type t = data.GetType();
if (t.Equals(typeof(string)))
Console.WriteLine(" '{0}' is a String", data);
else if (t.Equals(typeof(sbyte)))
Console.WriteLine(" '{0}' is a Signed Byte", data);
else if (t.Equals(typeof(byte)))
Console.WriteLine(" '{0}' is a Byte", data);
else if (t.Equals(typeof(int)))
Console.WriteLine(" '{0}' is an Integer of 32-bit", data);
else if (t.Equals(typeof(long)))
Console.WriteLine(" '{0}' is an Integer of 64-bit", data);
else if (t.Equals(typeof(double)))
Console.WriteLine("'{0}' is a double", data);
else
Console.WriteLine("'{0}' is another type of data", data);
}
}
}
輸出:
'Abc' is a String
'193527' is an Integer of 64-bit
'Happy Programming' is a String
'89' is a Byte
'Z' is another type of data
'-11' is a Signed Byte
'Zeelandia is 8th continent' is a String
'27.9' is a double
'I am a string line' is a String
'20' is an Integer of 32-bit
'7' is another type of data
比較 C# 中的 type 物件
具有型別的 Type 物件是不同的。如果兩個 Type 物件識別符號表示相同的型別,則它們對應於同一個物件。
它使 Type 物件能夠使用引用相等進行比較。下面的示例比較了包含多個整數值的 Type 物件,以檢視它們是否屬於同一型別。
首先,在 Main() 方法中初始化 int、double 和 long 型別的變數,併為它們分配一些值。
int Value1 = 2723;
double Value2 = 123.56;
int Value3 = 1747;
long Value4 = 123456789;
在變數 t 中初始化型別 Type 的變數 t,我們將通過 .GetType 方法儲存名為 Value1 的值 1 的資料型別。
Type t = Value1.GetType();
現在,藉助 Object.ReferenceEquals(t, Value2.GetType()) 函式,我們將所有物件的型別與 Value1 進行比較。
Console.WriteLine("Data type of Value1 and Value2 are equal: {0}",
Object.ReferenceEquals(t, Value2.GetType()));
Console.WriteLine("Data type of Value1 and Value3 are equal: {0}",
Object.ReferenceEquals(t, Value3.GetType()));
Console.WriteLine("Data type of Value1 and Value4 are equal: {0}",
Object.ReferenceEquals(t, Value4.GetType()));
完整程式碼示例:
using System;
using System.Diagnostics;
class CompareTypeObjects {
public static void Main() {
int Value1 = 2723;
double Value2 = 123.56;
int Value3 = 1747;
long Value4 = 123456789;
Type t = Value1.GetType();
Console.WriteLine("The data type of Value1 and Value2 are equal: {0}",
Object.ReferenceEquals(t, Value2.GetType()));
Console.WriteLine("The data type of Value1 and Value3 are equal: {0}",
Object.ReferenceEquals(t, Value3.GetType()));
Console.WriteLine("The data type of Value1 and Value4 are equal: {0}",
Object.ReferenceEquals(t, Value4.GetType()));
}
}
輸出:
The data type of Value1 and Value2 are equal: False
The data type of Value1 and Value3 are equal: True
The data type of Value1 and Value4 are equal: False
I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.
LinkedIn