c# GetType()和typeof()的区别

c#   GetType()和typeof()的区别

 

C#中任何对象都具有GetType()方法返回Type类型的当前对象的类型。

GetType()是基类System.Object的方法,因此只有建立一个实例之后才能够被调用。

typeof(x)中的x,必须是具体的类名、类型名称等,不可以是变量名称;

The typeof operator is used to obtain the System.Type object for a type.

  1. typeof的参数只能是int,string,String,自定义类型,且不能是实例
  2. GetType()和typeof都返回System.Type的引用.
  3. typeof():得到一个Class的Type
  4. GetType():得到一个Class的实例的Type

猜你喜欢

转载自www.cnblogs.com/GoldenEllipsis/p/10356404.html
今日推荐