The difference between GetType () and typeof () is as follows:

Source: Internet
Author: User

Difference between GetType () and typeof ()

GetType () returns the Class Name of the object.
Case 1:

int i = 5;
Console.WriteLine(i.GetType());//System.Int32
var x = 127.25m;
Console.WriteLine(x.GetType());//System.Decimal

Case 2:

namespace _2011._12._15
{
class Program
{
static void Main(string[] args)
{
Test testone = new Test();
string s = testone.GetType().ToString();
Console.WriteLine(s);//_2011._12._15.Test 命名空间的Test类
}
}
class Test
{
}
}



Typeof () returns the object of the class name. You can also return the class name or the methods and fields inside the specified class.

namespace _2011._12._15
{
class Program
{
static void Main(string[] args)
{
Test testone = new Test();
string s = testone.GetType().ToString();
Console.WriteLine("GetType():");
Console.WriteLine(s);//_2011._12._15.Test 命名空间的Test类

Type type = typeof(Test);
Console.WriteLine("Typeof():");
Console.WriteLine(type);//_2011._12._15.Test 命名空间的Test类
Console.WriteLine();

MethodInfo[] methodinfo = type.GetMethods();

Console.WriteLine(methodinfo.GetType());//System.Reflection.MethodInfo[]
foreach (var i in methodinfo)
{
Console.WriteLine(i);//输出Test类的所有方法及继承Object的实例方法
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
MemberInfo[] memberinfo = type.GetMembers();
Console.WriteLine(memberinfo.GetType());
foreach(var i in memberinfo)
{
Console.WriteLine(i);//输出Test类字段和System.type类型
}
}


}

class Test
{

private int age;
public string name;
public void speaking()
{
Console.WriteLine("Welcome to cnblog!");
}


public void writing()
{
Console.WriteLine("Please writing something!");
}
}
}

Running result:

GetType():
_2011._12._15.Test
Typeof():
_2011._12._15.Test

System.Reflection.MethodInfo[]
Void speaking()
Void writing()
System.Type GetType()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()




System.Reflection.MemberInfo[]
Void speaking()
Void writing()
System.Type GetType()
System.String ToString()
Boolean Equals(System.Object)
Int32 GetHashCode()
Void .ctor()
System.String name

The difference between GetType () and typeof () is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.