在C#中,是"这&Q​​UOT;必需的关键字? [英] In C#, is "this" keyword required?

查看:109
本文介绍了在C#中,是"这&Q​​UOT;必需的关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的构造,是需要'这个'关键字?我知道我可以删除它,它符合一切正常。如果我忽略'这个'将这项事业,我在路上的问题?是'这个'认为是不好的做法ommission?

  //构造函数:
    公务员(字符串名称,字符串别名)
    {
        //使用这个资格的字段,名称及别名:
        this.name =名称;
        this.alias =别名;
    }


解决方案

没有,这个在几乎所有情况下纯粹是可选的。它在你的例子是必需的唯一理由是局部变量和参数和成员变量碰巧有相同的标识符名称之间的歧义。

您可以通过任一参数或字段重命名为一些独特避免使用这个

In the following constructor, is the 'this' keyword required? I know I can remove it, it complies and everything is okay. If I omit 'this' will that cause problems for me down the road? Is ommission of 'this' considered bad practice?

    // Constructor:
    public Employee(string name, string alias)
    {
        // Use this to qualify the fields, name and alias:
        this.name = name;
        this.alias = alias;
    }

解决方案

No, this is purely optional in almost all cases. The only reason it is required in your example is to disambiguate between local variables and parameters and member variables that happened to have the same identifier name.

You could avoid the use of this by renaming either the parameter or the field to something unique.

这篇关于在C#中,是"这&Q​​UOT;必需的关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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