在 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
相关文章
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:198 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:171 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中将 List<string>转换为字符串
发布时间:2024/03/16 浏览次数:187 分类:编程语言
-
在 C# 中,有两种主要方法可用于将 List
转换为字符串变量,Linq 方法和 String.Join()函数。
在 C# 中发出 HTTP POST Web 请求
发布时间:2024/02/04 浏览次数:131 分类:编程语言
-
在 C# 中,可以使用 3 种主要方法来发出 HTTP POST Web 请求:WebClient 类,HttpWebRequest 类和 HttpClient 类。本教程将讨论在 C# 中发出 HTTP POST Web 请求的方法。使用 C# 中的 WebClient 类发出 HTTP POST Web 请求
在 C# 中运行命令提示符命令
发布时间:2024/02/04 浏览次数:130 分类:编程语言
-
Process 类可用于在 C# 中运行命令提示符命令。在 C# 中使用 Process.Start() 函数运行命令提示符命令
在 C# 中调整图像大小
发布时间:2024/02/04 浏览次数:203 分类:编程语言
-
有两种主要方法可用于在 C# 中调整图像的大小,Bitmap 类构造函数和 graphics.DrawImage()函数。在本教程中,我们将讨论在C#中调整图像大小的方法。我们将带您完成整个过程,从加载原始图像到保
在 C# 中下载图片
发布时间:2024/02/04 浏览次数:138 分类:编程语言
-
有 3 种主要方法可用于下载 C# 中的图片,WebClient.DownloadFile()函数,Bitmap 类和 Image.FromStream()函数。在 C# 中使用 WebClient 类下载图片 WebClient 类提供了用于向 C# 中的 URL 发送数据和从 URL 接收数据
在 C# 中使用秒表
发布时间:2024/02/04 浏览次数:139 分类:编程语言
-
我们可以使用 Stopwatch 类来计算 C# 中的经过时间。使用 C# 中的秒表类计算经过时间 Stopwatch 类在 C# 中准确测量经过的时间。
在 C# 中获取可执行路径
发布时间:2024/02/04 浏览次数:200 分类:编程语言
-
有 3 种主要方法可用于获取 C# 中程序的可执行路径,即 Assembly 类,AppDomain 类和 Path 类。本教程将介绍获取 C# 代码的可执行路径的方法。使用 C# 中的 Assembly 类获取可执行路径