当与管制的形式在C#中工作时,使用此关键字管制、关键字、形式、工作

由网友(酷帥的冷血范)分享简介:我还是远离掌握C#,但孩子对我是推动我继续提高我的编程与日俱增。当我做一个WinForms应用程序,我想改变,并使用大量的控制务实。当我需要使用 this.control 关键字,当我应该只使用控制我不明白的是。 示例:如果我想改变我的标签的文本,我可以写I am still far away from ma...

我还是远离掌握C#,但孩子对我是推动我继续提高我的编程与日俱增。 当我做一个WinForms应用程序,我想改变,并使用大量的控制务实。 当我需要使用 this.control 关键字,当我应该只使用控制我不明白的是。 示例: 如果我想改变我的标签的文本,我可以写

I am still far away from mastering C#, but the child in me is pushing me to continue improving my programming day by day. When I make a WinForms application I want to change and use lot of controls pragmatically. What I do not understand is when I need to use the this.control keyword and when I should use just control. Sample: If I want to change the text of my label I can write

mylabel.text = "Text for label"

this.mylabel.tex = "Text for label"

哪一项是正确的做法?有一个简单的解释时的WinForms(如数据网格,文字,表格等)?使用控件时使用关键字

推荐答案

在这种情况下,这些线路都是正确的。然而,使用的本这里不需要

In this case, both of those lines are "correct". However, the use of "this" is not needed here.

原因之一使用本是,如果您需要解决歧义。 这给你明确的访问类的成员。这里有一个例子:

One reason to use "this" is if you need to resolve an ambiguity. "this" gives you unambiguous access to the members of a class. Here's an example:

class Test
{
   public void SetNumber(int number)
   {
      this.number = number;
   }

   private int number;
}

在这个例子中,您必须使用这指代类成员数量,并分配给它的值在传入的参数具有相同的名称(数字)。

In this example, you must use "this" to refer to the class member "number" and assign to it the value in the passed in argument with the same name ("number").

当然,这将是更好的有prevents这个命名约定。我倾向于把下划线的私有成员数据(即_number)的前面。

Of course, it would be better to have a naming convention that prevents this. I tend to put an underscore in front of private member data (ie. _number).

阅读全文

相关推荐

最新文章