typeof运算符

typeof运算符

typeof运算符用于获取类型的System.Type对象。
typeof(x)中的x,必须是具体的类名、类型名称等,不可以是变量名称。
typeof运算符不能重载。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseTypeof
{
class Program
{
static void Main(string[] args)
{
//typeof运算符
System.Type byteType = typeof(byte);
Console.WriteLine("byte类型的对象是:{0}", byteType);
System.Type sbyteType = typeof(sbyte);
Console.WriteLine("sbyte类型的对象是:{0}", sbyteType);
System.Type intType = typeof(int);
Console.WriteLine("int类型的对象是:{0}",intType );
System.Type uintType = typeof(uint);
Console.WriteLine("uint类型的对象是:{0}", uintType);
System.Type shortType = typeof(short);
Console .WriteLine ("short类型的对象是:{0}",shortType );
System.Type ushortType = typeof(ushort);
Console.WriteLine("ushort类型的对象是:{0}", ushortType);
System.Type longType = typeof(long);
Console.WriteLine("long类型的对象是:{0}", longType);
System.Type ulongType = typeof(ulong);
Console.WriteLine("ulong类型的对象是:{0}", ulongType);
System.Type charType = typeof(char);
Console.WriteLine("char类型的对象是{0}", charType);
System.Type stringType = typeof(string);
Console.WriteLine("string类型的对象是:{0}", stringType);
System.Type floatType = typeof(float);
Console.WriteLine("float类型的对象是:{0}", floatType);
System.Type doubleType = typeof(double);
Console.WriteLine("double类型的对象是:{0}", doubleType);
System.Type decimalType = typeof(decimal);
Console.WriteLine("decimal类型的对象是:{0}", decimalType);
System.Type boolType = typeof(Boolean);
Console.WriteLine("bool类型的对象是:{0}", boolType);


A a1 = new A();
a1.Fun();
a1.GetType();
Console.WriteLine(a1.GetType());
}
}
class A
{
public void Fun()
{
Console.WriteLine("Fun()");
}
}
}

/*

Output:

byte类型的对象是:System.Byte
sbyte类型的对象是:System.SByte
int类型的对象是:System.Int32
uint类型的对象是:System.UInt32
short类型的对象是:System.Int16
ushort类型的对象是:System.UInt16
long类型的对象是:System.Int64
ulong类型的对象是:System.UInt64
char类型的对象是System.Char
string类型的对象是:System.String
float类型的对象是:System.Single
double类型的对象是:System.Double
decimal类型的对象是:System.Decimal
bool类型的对象是:System.Boolean
Fun()
UseTypeof.A

*/

 

posted @ 2015-01-21 15:49  melao2006  阅读(303)  评论(0编辑  收藏  举报