gpt4 book ai didi

c# - GetType() 是方法还是类?

转载 作者:太空狗 更新时间:2023-10-29 20:45:35 24 4
gpt4 key购买 nike

我遇到了这段代码:

int myInt = 0;
textBox1.Text = myInt.GetType().Name;

根据.NET documentation , GetType() 是一个方法,而不是一个类。

我的问题是,我如何才能将点与方法一起使用,例如 GetType().Name

最佳答案

一个方法可以返回一个类实例,这里是类的一个实例Type .在此对象上,您可以访问属性、其他方法等。

你的代码可以这样写:

int myInt = 0;
Type t = myInt.GetType();
textBox1.Text = t.Name;

也许这样更容易理解。

编辑:像 GetType() 这样的方法调用执行该方法,以及您在 . 之后所做的一切适用于方法的返回值,在本例中为 Type 类型的对象.

关于c# - GetType() 是方法还是类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14599843/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com