能否"本"关键字值类型使用? [英] Can the "this" keyword be used with value types?

查看:163
本文介绍了能否"本"关键字值类型使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到这个有趣的使用本关键字,一边欣赏Int32.GetHashCode()在.net反射的反汇编代码:

 公共覆盖INT的GetHashCode()
{
返回这一点;
}



我一直以为这只是引用类型不是值类型使用。 ?在上面的代码,将拳击在每次尝试得到一个int的散列码时使用



从MSDN中的本关键字的文档:
- this关键字指的是类



问候


解决方案<当前实例/ DIV>

这个有效期为值类型。它不会导致拳击。虽然引用了价值型,它不是在同样为引用类型引用的引用 - 这是一个编译/开发时间的便利,而不是一个运行时的参考。



在C#规范的第7.5.7规定:





  • 当这是在$ b $使用b A结构的实例
    构造函数中基本表达式,它归类为一个变量
    。类型
    变量是实例类型
    (§10.3.1)B上发生的用法,而变量
    代表结构存在$ B $其内$ B $该结构b构成。一个结构

    实例构造函数的this变量的行为完全一样的结构类型,在$的出
    参数b $ B具体而言,这意味着
    变量必须被明确赋值在
    实例构造函数的每个执行路径





  • 在当前这是一个
    实例方法或实例内的基本表达式中使用
    一个结构的存取
    ,它被分类为
    变量。变量类型是
    在其中使用出现
    结构的实例类型(§10.3.1)。



    • 如果方法或访问不是
      迭代器(§10.14)时,这个变量
      代表结构为其
      方法或访问被调用,和
      的行为完全一样的结构类型的ref
      参数。


    • 如果在
      方法或访问是一个迭代器,在
      这个变量代表了
      结构的副本该方法或
      访问被调用,并且表现
      完全一样的结构类型的值参数




I noticed this interesting use of the "this" keyword while viewing the disassembled code of Int32.GetHashCode() in .NET Reflector:

public override int GetHashCode()
{
    return this;
}

I always thought "this" is only used with reference types not value types. In the code above, will boxing be used every time you try to get the hash code of an int?

From the documentation of the "this" keyword in MSDN: - The this keyword refers to the current instance of the class

Regards

解决方案

Yes, this is valid for value types. It does not lead to boxing. While it references the value-type, it is not a reference in the same vein as reference-type references - it is a compile/development time convenience rather than a runtime reference.

Section 7.5.7 of the C# Specification states:

  • When this is used in a primary-expression within an instance constructor of a struct, it is classified as a variable. The type of the variable is the instance type (§10.3.1) of the struct within which the usage occurs, and the variable represents the struct being constructed. The this variable of an instance constructor of a struct behaves exactly the same as an out parameter of the struct type—in particular, this means that the variable must be definitely assigned in every execution path of the instance constructor.

  • When this is used in a primary-expression within an instance method or instance accessor of a struct, it is classified as a variable. The type of the variable is the instance type (§10.3.1) of the struct within which the usage occurs.
    • If the method or accessor is not an iterator (§10.14), the this variable represents the struct for which the method or accessor was invoked, and behaves exactly the same as a ref parameter of the struct type.
    • If the method or accessor is an iterator, the this variable represents a copy of the struct for which the method or accessor was invoked, and behaves exactly the same as a value parameter of the struct type.

这篇关于能否&QUOT;本&QUOT;关键字值类型使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆