博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c#中typeof_C#中带有示例的typeof()运算符
阅读量:2528 次
发布时间:2019-05-11

本文共 5334 字,大约阅读时间需要 17 分钟。

c#中typeof

C#typeof()运算符 (C# typeof() Operator)

typeof() is an operator in C#, it is used to get the type (system type) of with class name of a given type. By using typeof() operator, we can get the name of the type, namespace name. It works with only compile-time known types. typeof() operator does not work with the variables or instances.

typeof()是C#中的运算符,用于获取具有给定类型的类名的类型(系统类型)。 通过使用typeof()运算符 ,我们可以获得类型的名称,即名称空间名称。 它仅适用于编译时已知的类型。 typeof()运算符不适用于变量或实例。

If you want to get the type of a variable, you can use GetType() method.

如果要获取变量的类型,可以使用GetType()方法。

There are main 3 properties to get the details about the type:

主要的3个属性可获取有关类型的详细信息:

  1. typeof(type).Name or this.GetType().Name – It returns the class name only.

    typeof(type).Name或this.GetType()。Name –仅返回类名称。

  2. typeof(type).FullName or this.GetType().FullName – It returns the class name along with the namespace.

    typeof(type).FullName或this.GetType()。FullName –返回类名和名称空间。

  3. typeof(type).Namespace or this.GetType().Namespace – It returns the namespace only.

    typeof(type).Namespace或this.GetType()。Namespace –仅返回名称空间。

Note: If we do not use any property, by default typeof(type) or this.GetType() returns the FullName.

注意:如果不使用任何属性,则默认为typeof(type)或this.GetType()返回FullName 。

Syntax:

句法:

System.type typeof(type);    or    System.type this.GetType();

Example:

例:

typeof(int)     - System.Int32    int a = 10;    a.GetType()     - System.Int32

Example 1: print the Name, FullName, Namespace name of the compile-time known types.

示例1:打印编译时已知类型的Name,FullName,Namespace名称。

using System;using System.Text;namespace Test{
class Program {
static void Main(string[] args) {
Console.WriteLine("for char type..."); Console.WriteLine("default: " + typeof(char)); Console.WriteLine("Name: " + typeof(char).Name); Console.WriteLine("FullName: " + typeof(char).FullName); Console.WriteLine("Namespace: " + typeof(char).Namespace); Console.WriteLine(); Console.WriteLine("for Int32 type..."); Console.WriteLine("default: " + typeof(Int32)); Console.WriteLine("Name: " + typeof(Int32).Name); Console.WriteLine("FullName: " + typeof(Int32).FullName); Console.WriteLine("Namespace: " + typeof(Int32).Namespace); Console.WriteLine(); Console.WriteLine("for bool type..."); Console.WriteLine("default: " + typeof(bool)); Console.WriteLine("Name: " + typeof(bool).Name); Console.WriteLine("FullName: " + typeof(bool).FullName); Console.WriteLine("Namespace: " + typeof(bool).Namespace); Console.WriteLine(); //hit ENTER to exit Console.ReadLine(); } }}

Output

输出量

for char type...default: System.CharName: CharFullName: System.CharNamespace: Systemfor Int32 type...default: System.Int32Name: Int32FullName: System.Int32Namespace: Systemfor bool type...default: System.BooleanName: BooleanFullName: System.BooleanNamespace: System

Example 2: print the Name, FullName, Namespace name of the variables.

示例2:打印变量的名称,全名,命名空间名称。

using System;using System.Text;namespace Test{
class Program {
//structure struct student {
private string name; private int age; }; static void Main(string[] args) {
char a = 'X'; int b = 10; bool c = false; Console.WriteLine("for variable \'a\'..."); Console.WriteLine("default: " + a.GetType()); Console.WriteLine("Name: " + a.GetType().Name); Console.WriteLine("FullName: " + a.GetType().FullName); Console.WriteLine("Namespace: " + a.GetType().Namespace); Console.WriteLine(); Console.WriteLine("for variable \'b\'..."); Console.WriteLine("default: " + b.GetType()); Console.WriteLine("Name: " + b.GetType().Name); Console.WriteLine("FullName: " + b.GetType().FullName); Console.WriteLine("Namespace: " + b.GetType().Namespace); Console.WriteLine(); Console.WriteLine("for variable \'c\'..."); Console.WriteLine("default: " + c.GetType()); Console.WriteLine("Name: " + c.GetType().Name); Console.WriteLine("FullName: " + c.GetType().FullName); Console.WriteLine("Namespace: " + c.GetType().Namespace); Console.WriteLine(); student std = new student(); Console.WriteLine("for structure \'std\'..."); Console.WriteLine("default: " + std.GetType()); Console.WriteLine("Name: " + std.GetType().Name); Console.WriteLine("FullName: " + std.GetType().FullName); Console.WriteLine("Namespace: " + std.GetType().Namespace); Console.WriteLine(); //hit ENTER to exit Console.ReadLine(); } }}

Output

输出量

for variable 'a'...default: System.CharName: CharFullName: System.CharNamespace: Systemfor variable 'b'...default: System.Int32Name: Int32FullName: System.Int32Namespace: Systemfor variable 'c'...default: System.BooleanName: BooleanFullName: System.BooleanNamespace: Systemfor structure 'std'...default: Test.Program+studentName: studentFullName: Test.Program+studentNamespace: Test

翻译自:

c#中typeof

转载地址:http://gsvzd.baihongyu.com/

你可能感兴趣的文章
给你的应用“一只”智慧的眼睛 —— Barcode常识普及以及识别信息处理
查看>>
PHP基础知识点汇总(三)
查看>>
排序算法
查看>>
sql server系统数据库,temp库的用途
查看>>
推荐一下干货-------为什么你的app不耐看
查看>>
05-数据类型转换
查看>>
[BZOJ1934/Luogu2057][SHOI2007]Vote 善意的投票 题解
查看>>
lightslider-支持移动触摸的轻量级jQuery幻灯片插件
查看>>
如何用纯 CSS 创作一个 3D 文字跑马灯特效
查看>>
assert.notDeepEqual()
查看>>
android获取textview的行数
查看>>
winsocket客户端编程以及jmeter外部调用
查看>>
PHP基础知识------页面静态化
查看>>
zoj 3747 dp递推
查看>>
POJ 3740
查看>>
41025 ISD Assignment 2 Autumn 2019
查看>>
JavaScript跨域调用基于JSON的RESTful API
查看>>
js 右击事件
查看>>
POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)
查看>>
今天突然出现了Property IsLocked is not available for Login '[sa]',我太阳,下面有绝招对付它!...
查看>>