如何找出某个变量所持有的数据类型? (例如int,string,char等)
我现在有这样的事情:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Testing
{
class Program
{
static void Main()
{
Person someone = new Person();
someone.setName(22);
int n = someone.getName();
Console.WriteLine(n.typeOf());
}
}
class Person
{
public int name;
public void setName(int name)
{
this.name = name;
}
public int getName()
{
return this.name;
}
}
}
答案 0 :(得分:94)
其他答案对这个问题提供了很好的帮助,但是有一个重要且微妙的问题,它们都没有直接解决。在C#中有两种考虑类型的方法:静态类型和运行时类型。
静态类型是源代码中变量的类型。因此它是一个编译时的概念。当您将鼠标悬停在开发环境中的变量或属性上时,这是您在工具提示中看到的类型。
运行时类型是内存中对象的类型。因此它是一个运行时概念。这是GetType()
方法返回的类型。
对象的运行时类型通常不同于保存或返回它的变量,属性或方法的静态类型。例如,您可以使用以下代码:
object o = "Some string";
变量的静态类型为object
,但在运行时,变量 referent 的类型为string
。因此,下一行将“System.String”打印到控制台:
Console.WriteLine(o.GetType());
但是,如果您将鼠标悬停在开发环境中的变量o
上,则会看到类型System.Object
(或等效的object
关键字)。
对于值类型变量,例如int
,double
,System.Guid
,您知道运行时类型将始终与静态类型相同,因为值类型不能作为另一种类型的基类;值类型保证是其继承链中派生最多的类型。对于密封引用类型也是如此:如果静态类型是密封引用类型,则运行时值必须是该类型的实例或null
。
相反,如果变量的静态类型是抽象类型,则保证静态类型和运行时类型不同。
在代码中说明:
// int is a value type
int i = 0;
// Prints True for any value of i
Console.WriteLine(i.GetType() == typeof(int));
// string is a sealed reference type
string s = "Foo";
// Prints True for any value of s
Console.WriteLine(s == null || s.GetType() == typeof(string));
// object is an unsealed reference type
object o = new FileInfo("C:\\f.txt");
// Prints False, but could be true for some values of o
Console.WriteLine(o == null || o.GetType() == typeof(object));
// FileSystemInfo is an abstract type
FileSystemInfo fsi = new DirectoryInfo("C:\\");
// Prints False for all non-null values of fsi
Console.WriteLine(fsi == null || fsi.GetType() == typeof(FileSystemInfo));
答案 1 :(得分:16)
一般来说,除非你用反射或界面做某事,否则几乎不需要进行类型比较。尽管如此:
如果您知道要与之比较的类型,请使用is
或as
运营商:
if( unknownObject is TypeIKnow ) { // run code here
as
运算符执行强制转换,如果失败而不是异常,则返回null:
TypeIKnow typed = unknownObject as TypeIKnow;
如果您不知道类型并且只想要运行时类型信息,请使用.GetType()方法:
Type typeInformation = unknownObject.GetType();
在较新版本的C#中,您可以使用is
运算符声明变量,而无需使用as
:
if( unknownObject is TypeIKnow knownObject ) {
knownObject.SomeMember();
}
以前你必须这样做:
TypeIKnow knownObject;
if( (knownObject = unknownObject as TypeIKnow) != null ) {
knownObject.SomeMember();
}
答案 2 :(得分:9)
非常简单
variable.GetType().Name
它将返回变量的数据类型
答案 3 :(得分:4)
答案 4 :(得分:4)
只需将光标放在您感兴趣的成员上,然后查看工具提示 - 它将显示memeber的类型:
答案 5 :(得分:1)
GetType()
方法
int n=34;
Console.WriteLine(n.GetType());
string name="Smome";
Console.WriteLine(name.GetType());
答案 6 :(得分:1)
PHP和C#在语法上是相关但完全不同,虽然我可以用面值来回答这个问题(参见这篇文章http://msdn.microsoft.com/en-us/library/58918ffs(v=vs.71).aspx)我强烈建议你通过C#(第三或第二)获得一份CLR副本由Jeffrey Richter撰写并阅读。它是与编程相关的最好的书,我认为我曾经阅读过,几乎可以回答所有与类型相关的问题,并让您对引擎盖下的内容有深入的了解!
答案 7 :(得分:1)
一种选择是使用如下的辅助扩展方法:
public static class MyExtensions
{
public static System.Type Type<T>(this T v)=>typeof(T);
}
var i=0;
console.WriteLine(i.Type().FullName);
答案 8 :(得分:0)
查看一个简单的方法来执行此操作
// Read string from console
string line = Console.ReadLine();
int valueInt;
float valueFloat;
if (int.TryParse(line, out valueInt)) // Try to parse the string as an integer
{
Console.Write("This input is of type Integer.");
}
else if (float.TryParse(line, out valueFloat))
{
Console.Write("This input is of type Float.");
}
else
{
Console.WriteLine("This input is of type string.");
}