"这" C#中的关键字 [英] "this" keyword in C#

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

问题描述

大家好,

这可能是一个愚蠢的问题,但我很想知道这个。



我知道使用此关键字来限定以类似名称隐藏的成员。但现在我的问题是什么类型的隐藏成员?



我看到以下代码:



  public 员工(字符串名称,字符串 别名
{
.name =名称;
别名 = 别名;
}





但我仍然无法获得短语限定隐藏成员



所以,帮助摆脱这种愚蠢的困惑。



问候

Atul Khanduri

解决方案

让我试着解释这个合格的作品。



让我们说你在两个地方有一个相同的变量名,一个在一个类,一个在构造函数/方法一级。

使用文章中的例子 -



  public   class 员工
{
private string name = string .empty;
私有 字符串 别名 = string .empty;

public 员工(字符串名称,字符串 别名

// 使用它来限定字段,名称和别名:
this .name = name;
别名 = 别名;
}
}





有两个范围名称别名变量 - 一个在类,另一个在构造函数级别。

当我们说这个,我们将变量限定为第一级。



this.name = name;



类级别变量的值已设置为构造函数范围内传递的值。


这个关键字是为了限定班级的当前实例

我不知道你在哪里得到这个资格隐藏成员的事情。

这个(C#参考) [ ^ ]

Hello all,
This may be a silly question but i am curious to know this.

I know "this" keyword is used to qualify members hidden by similar name. But now my question is what type of hidden members??

I saw the following code:

public Employee(string name, string alias)
{
      this.name = name;
      this.alias = alias;
}



But still i am unable to get the phrase "qualify hidden members".

So, help to get out from this silly confusion.

Regards
Atul Khanduri

解决方案

Let me try and explain this "qualify" piece.

Lets say you have the same variable name in two places one at class and one at constructor / method level.
Using the example in the article itself -

public class Employee
{
private string name = string.empty;
private string alias = string.empty;

 public Employee(string name, string alias)

    // Use this to qualify the fields, name and alias:
    this.name = name;
    this.alias = alias;
 }
}



There are two scoped name and alias variables - one at class and another at constructor level.
When we say this, we qualify the variable to be the class level one.

this.name = name;

The value of the class level variable has been set to the value passed in the scope of the constructor.


this keyword is meant to qualify the current instance of the class.
I don't know where you got this "qualify hidden members" thing.
this (C# Reference)[^]


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

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