【问题标题】:What is the meaning of "this" in C#C#中的“this”是什么意思
【发布时间】:2011-09-10 09:02:35
【问题描述】:

谁能解释一下 C# 中“this”的含义?

如:

// complex.cs
using System;

public struct Complex 
{
   public int real;
   public int imaginary;

   public Complex(int real, int imaginary) 
   {
      this.real = real;
      this.imaginary = imaginary;
   }

【问题讨论】:

  • 何不抬头看看MSDN? "this 关键字是指类的当前实例。"
  • vlad - 为什么不在 stackoverflow 上发帖?
  • @Vlad - 这个问题对许多针对 SO 提出的问题都有效。 SO的重点是成为一个可以作为参考的地方。首先,不是每个人都知道MSDN(所以指出他们是个好主意)。其次,OP给出了上下文。有效问题 IMO。
  • 搜索“this”这样的常用词非常困难。你真的去过 MSDN 网站并输入“this”来查看搜索结果吗?这和找出什么一样困难?和 ??在 if-then 语句中表示,或者在 lambda 表达式中表示 =>。

标签: c#


【解决方案1】:

this 关键字是对当前类实例的引用。

在您的示例中,this 用于引用类Complex 的当前实例,它消除了构造函数签名中的int real 与类定义中的public int real; 之间的歧义。

MSDN has some documentation 这个也值得一看。

虽然与您的问题没有直接关系,但this 作为扩展方法中的第一个参数还有另一种用法。它用作表示要使用的实例的第一个参数。如果想向String class 添加方法,您可以简单地在任何静态类中编写

public static string Left(this string input, int length)
{
    // maybe do some error checking if you actually use this
    return input.Substring(0, length); 
}

另见:http://msdn.microsoft.com/en-us/library/bb383977.aspx

【讨论】:

    【解决方案2】:

    当方法体

    public Complex(int real, int imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }
    

    正在执行,它正在结构Complex 的特定实例上执行。您可以使用关键字this 引用正在执行代码的实例。因此你可以想到方法的主体

    public Complex(int real, int imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }
    

    作为阅读

    public Complex(int real, int imaginary) {
        assign the parameter real to the field real for this instance
        assign the parameter imaginary to the field imaginary for this instance
    }
    

    总是有一个隐含的this,所以下面是等价的

    class Foo {
        int foo;
        public Foo() {
            foo = 17;
        }
    }
    
    class Foo {
        int foo;
        public Foo() {
            this.foo = 17;
        }
    }
    

    但是,本地人优先于成员,因此

    class Foo {
        int foo;
        public Foo(int foo) {
            foo = 17;
        }
    }
    

    分配17 所以变量foo 是方法的参数。当你有一个方法存在同名本地时,如果你想分配给实例成员,你必须使用this来引用它。

    【讨论】:

      【解决方案3】:

      this关键字指的是当前 类的实例,也被使用 作为第一个参数的修饰符 的扩展方法。

      this (C# reference) - MSDN
      C# Keywords - MSDN

      【讨论】:

        【解决方案4】:

        Nate 和 d_r_w 有答案。我只是想在你的代码中特别添加这个。确实引用了 CLASS 的成员,以区别于 FUNCTION 的参数。所以,这条线

        this.real = real
        

        表示将函数(在本例中为构造函数)参数“real”的值分配给类成员“real”。通常,您也会使用用例来使区分更加清晰:

        public struct Complex
        {
            public int Real;
            public int Imaginary;
            public Complex(int real, int imaginary)
            {
                this.Real = real;
                this.Imaginary = imaginary;
            }
        }
        

        【讨论】:

        • 如果您的参数名称与字段名称不同,您实际上并不需要 this。但对于 OP 的示例,使用 this 是强制性的。
        【解决方案5】:

        this 引用类的实例。

        【讨论】:

          【解决方案6】:

          由于大多数答案都提到“类的当前实例”,因此“实例”这个词对于新手来说可能很难理解。 “类的当前实例”意味着 this.varible 专门用于定义它的类中,而不是其他任何地方。因此,如果变量名也出现在类外,开发者无需担心多次使用同一个变量名带来的冲突/混乱。

          【讨论】:

          • 你得到了那个真正缺失的差距。
          【解决方案7】:

          这是一个代表当前类实例的变量。例如

          class SampleClass {
          public SampleClass(someclass obj) {
          obj.sample = this;
          }
          }
          

          在本例中,这用于将 someclass obj 上的“sample”属性设置为 SampleClass 的当前实例。

          【讨论】:

            【解决方案8】:

            引用当前类的实例

            【讨论】:

              猜你喜欢
              • 2014-05-20
              • 1970-01-01
              • 2015-05-01
              • 2011-06-20
              • 2011-08-22
              • 1970-01-01
              • 2018-10-29
              • 2012-05-04
              • 2013-03-29
              相关资源
              最近更新 更多