我怎样才能获得在C#中的变量的数据类型? [英] How can I get the data type of a variable in C#?
问题描述
我怎样才能找出数据类型局部变量持有? (例如int,字符串,字符等)
我有这样的事情现在:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
命名空间测试
{
类节目
{
静态无效的主要()
{
的人的人=新的Person( );
someone.setName(22);
INT N = someone.getName();
Console.WriteLine(n.typeOf());
}
}
类Person
{
公众诠释名称;
公共无效setname可以(INT名)
{
this.name =名称;
}
公众诠释的getName()
{
返回this.name;
}
}
}
其他答案提供这个问题的好帮手,但是,他们没有直接解决的一个重要而微妙的问题。有在C#中考虑类型的方法有两种:静态类型和运行时类型的
静态类型一个变量在源代码中的类型。因此,它是一个编译时间的概念。这是您在提示时看到悬停在你的开发环境的变量或属性的类型。
运行时类型是内存中的对象的类型。因此,它是一个运行时间概念。这是由的GetType()
方法返回的类型。
这是对象的运行时类型是经常不同静态变量类型,属性或持有或返回它的方法。例如,你可以有这样的代码:
对象o =一些字符串;
静态类型的变量是对象
,但在运行时,变量的的指涉的是字符串类型
。因此,下一行会打印System.String到控制台:
Console.WriteLine(o.GetType()) ;
不过,如果悬停在变量 0
在开发环境中,你会看到键入 System.Object的
(或等值对象
关键字)。
有关值类型的变量,如 INT
,双击
,<为code>的System.Guid ,你知道运行时类型将永远是一样的静态类型,因为值类型不能作为另一种类型的基类;值类型是保证在其继承链最底层派生型。这也是密封的引用类型正确的:如果静态类型是一个密封的引用类型,运行时间值必须是该类型的实例或空
。 p>
相反,如果静态类型的变量是一个抽象类的话,就保证了静态类型和运行时类型会有所不同。
要说明的代码:
// int是值类型
INT I = 0;
//打印true为我的任意值
Console.WriteLine()i.GetType(typeof运算==(INT));
// string是一个密封的引用类型
字符串s =富;对于s
Console.WriteLine的任意值(S == NULL || s.GetType()== typeof运算(字符串))
//打印true;
//对象是未密封的引用类型
对象o =新的FileInfo(C:\\f.txt);
//打印假的,但可能是邻
Console.WriteLine的一些真实值(O == NULL || o.GetType()== typeof运算(对象));
// FileSystemInfo是一个抽象类
FileSystemInfo FSI =新的DirectoryInfo(C:\\);
//打印假的FSI
Console.WriteLine所有非空值(FSI == NULL || fsi.GetType()== typeof运算(FileSystemInfo));
How can I find out what data type some variable is holding? (e.g. int, string, char, etc.)
I have something like this now:
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;
}
}
}
Other answers offer good help with this question, but there is an important and subtle issue that none of them addresses directly. There are two ways of considering type in C#: static type and run-time type.
Static type is the type of a variable in your source code. It is therefore a compile-time concept. This is the type that you see in a tooltip when you hover over a variable or property in your development environment.
Run-time type is the type of an object in memory. It is therefore a run-time concept. This is the type returned by the GetType()
method.
An object's run-time type is frequently different from the static type of the variable, property, or method that holds or returns it. For example, you can have code like this:
object o = "Some string";
The static type of the variable is object
, but at run time, the type of the variable's referent is string
. Therefore, the next line will print "System.String" to the console:
Console.WriteLine(o.GetType());
But, if you hover over the variable o
in your development environment, you'll see the type System.Object
(or the equivalent object
keyword).
For value-type variables, such as int
, double
, System.Guid
, you know that the run-time type will always be the same as the static type, because value types cannot serve as the base class for another type; the value type is guaranteed to be the most-derived type in its inheritance chain. This is also true for sealed reference types: if the static type is a sealed reference type, the run-time value must either be an instance of that type or null
.
Conversely, if the static type of the variable is an abstract type, then it is guaranteed that the static type and the runtime type will be different.
To illustrate that in code:
// 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));
这篇关于我怎样才能获得在C#中的变量的数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!