首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用此关键字

使用此关键字
EN

Stack Overflow用户
提问于 2013-02-24 14:49:10
回答 5查看 245关注 0票数 2

我正在做一个窗口表格申请。当我查看文件Form1.Designer.cs时,在我看到的自动生成的代码中

代码语言:javascript
复制
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.button2);
        this.Controls.Add(this.button1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);

这个告诉我们什么?在c#中可以使用多少种方式?

EN

回答 5

Stack Overflow用户

发布于 2013-02-24 14:50:29

它引用类的当前实例。如果您正在使用像ReSharper这样的工具,它有时会被认为是多余的。

代码语言:javascript
复制
public class Test
{
        private string testVariable;

        public Test(string testVariable) 
        {
            this.testVariable = testVariable;
        }
}

在此上下文中,this.testVariable引用类中的私有字符串,而不是通过构造函数传入的testVariable

http://msdn.microsoft.com/en-gb/library/dk1507sz(v=vs.71).aspx

票数 6
EN

Stack Overflow用户

发布于 2013-02-24 14:52:27

this 关键字引用类的当前实例,因此在本例中是正在加载的Form1实例。

至于你为什么要使用它,它可以帮助区分变量。

代码语言:javascript
复制
 private string bar;

 private void Foo(string bar)
 {
   this.bar = bar;
 }

(尽管对于上面的代码,许多人会认为私有酒吧应该是_bar)

有关的更多信息

票数 4
EN

Stack Overflow用户

发布于 2013-02-24 14:52:18

this关键字引用class的当前实例,并用作扩展方法的第一个参数的修饰符。

代码语言:javascript
复制
public Employee(string name, string alias)
{
// Use this to qualify the fields, name and alias:
this.name = name;
this.alias = alias;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15052886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档