在 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 floating-point number with two accuracy levels
'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