c# 下面的构造函数中的两个 this 关键字有什么区别? [英] c# What makes the difference between the two this keywords in the constructor below?

查看:34
本文介绍了c# 下面的构造函数中的两个 this 关键字有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白以下内容:在下面找到的代码中,Visual Studio 告诉我,我可以通过删除构造函数中的第二个 this 关键字来简化我的代码.

I don't understand the following: In the code found below, Visual Studio tells me, that I can simplify my code by deleting the second this keyword in the constructor.

但是为什么我不能去掉第一个 this 关键字?这两个变量都是在类中的构造函数之外声明的,因此对于实例,它们都将被覆盖"*.

But why can't then I do away with the first this keyword? Both variables were declared outside the constructor, in the class, so both will be "overridden"* for the instance.

如果我删除两个 this 关键字,那么 VS 会抱怨第一个赋值是对同一个变量进行的,但不是第二个.对我来说唯一明显的区别是第二个变量是一个数组,但我不明白这会有什么不同?

If I remove both this keywords, then VS will complain that the first assignment is made to same variable, but not for the second. The only obvious difference to me is the second variable is an array, but I do not understand how would that make it any different?

*我怀疑这里的覆盖不是正确的术语.

*I suspect override is not the correct term here.

public class CelestialObject {

    CelestialBody[] celestialBodies;
    int celestialBodyCount;

    public CelestialObject(int celestialBodyCount = 2) {
        this.celestialBodyCount = celestialBodyCount;
        this.celestialBodies = new CelestialBody[celestialBodyCount];
    }
}

推荐答案

你不能去掉第一个 this 关键字,因为你有一个与你的字段同名的参数 celestialBodyCount:

You cannot do away with the first this keyword because you have a parameter with the same name as your field celestialBodyCount:

int celestialBodyCount; // The field

public CelestialObject(int celestialBodyCount = 2) { // The parameter.

this 关键字是必需的,以表明该字段被分配给.否则参数优先于字段,如3.7.1 姓名隐藏:

The this keyword is required to indicate that the field is being assigned to. Otherwise the parameter takes precedence over the field, as explained in 3.7.1 Name hiding:

... 实体的范围可能包括引入包含同名实体的新声明空间的声明.此类声明会导致原始实体隐藏.

... the scope of an entity may include declarations that introduce new declaration spaces containing entities of the same name. Such declarations cause the original entity to become hidden.

然后3.7.1.1通过嵌套隐藏:

通过嵌套隐藏名称的原因可能是在命名空间内嵌套命名空间或类型、在类或结构内嵌套类型、以及参数和局部变量声明.

Name hiding through nesting can occur as a result of nesting namespaces or types within namespaces, as a result of nesting types within classes or structs, and as a result of parameter and local variable declarations.

在这种情况下,参数 celestialBodyCount 隐藏了成员 celestialBodyCount.

In this case the parameter celestialBodyCount hides the member celestialBodyCount.

当然,如果你do删除this,编译器会给出以下警告:

Of course, if you do remove the this the compiler will helpfully give you the following warning:

Assignment made to same variable; did you mean to assign something else?

此警告几乎总是表示存在错误,应始终进行清理.

This warning almost always indicates a bug and should always be cleaned up.

这篇关于c# 下面的构造函数中的两个 this 关键字有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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