简体   繁体   English

C#中的“这个”是什么意思

[英]What is the meaning of “this” in C#

Could anyone please explain the meaning "this" in C#?谁能解释 C# 中“这个”的含义?

Such as:如:

// 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;
   }

The this keyword is a reference to the current instance of the class. this关键字是对 class 的当前实例的引用。

In your example, this is used to reference the current instance of the class Complex and it removes the ambiguity between int real in the signature of the constructor vs. the public int real;在您的示例中, this用于引用 class Complex的当前实例,它消除了构造函数签名中的 int int realpublic int real; in the class definition.在 class 定义中。

MSDN has some documentation on this as well which is worth checking out. MSDN 也有一些关于这方面的文档,值得一试。

Though not directly related to your question, there is another use of this as the first parameter in extension methods.尽管与您的问题没有直接关系,但在扩展方法中还有另一种用途是this作为第一个参数。 It is used as the first parameter which signifies the instance to use.它用作表示要使用的实例的第一个参数。 If one wanted to add a method to the String class you could simple write in any static class如果想向String class添加方法,您可以简单地写入任何 static 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); 
}

See also: http://msdn.microsoft.com/en-us/library/bb383977.aspx另请参阅: http://msdn.microsoft.com/en-us/library/bb383977.aspx

The this keyword refers to the current instance of the class and is also used as a modifier of the first parameter of an extension method. this 关键字指的是 class 的当前实例,也用作扩展方法的第一个参数的修饰符。

在此处输入图像描述

this (C# reference) - MSDN这个(C# 参考)- MSDN
C# Keywords - MSDN C# 关键字 - MSDN

As most answers are mentioning " the current instance of a class", the word "instance" may be difficult for newbies to understand.由于大多数答案都提到“类的当前实例”,因此“实例”这个词对于新手来说可能很难理解。 "the current instance of a class" means the this.varible is specifically used in the class where it is defined, not anywhere else. “类的当前实例”表示 this.varible 专门用于定义它的 class 中,而不是其他任何地方。 Therefore, if the variable name also showed up outside of the class, the developer doesn't need to worry about conflicts/confusions brought by using the same variable name multiple times.因此,如果变量名也出现在 class 之外,开发人员无需担心多次使用同一个变量名带来的冲突/混乱。

this references the instance of the class. this引用了 class 的实例。

Nate and d_r_w have the answer. Nate 和 d_r_w 有答案。 I just want to add that in your code specifically the this.我只是想在你的代码中特别添加这个。 does in deed refere to the member of the CLASS to distinguish from the arguments to the FUNCTION.实际上参考 CLASS 的成员,以区分 arguments 到 FUNCTION。 So, the line所以,线

this.real = real

means assign the value of the function (in this case, constructor) parameter 'real' to the class member 'real'.表示将 function(在本例中为构造函数)参数“real”的值分配给 class 成员“real”。 In general you'd use case as well to make the distinction clearer:通常,您也会使用案例来使区别更加清晰:

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

When the body of the method当方法的主体

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

is executing, it is executing on a specific instance of the struct Complex .正在执行,它在 struct Complex的特定实例上执行。 You can refer to the instance that the code is executing on by using the keyword this .您可以使用关键字this来引用正在执行代码的实例。 Therefore you can think of the body of the method因此你可以想到方法的主体

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

as reading作为阅读

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
}

There is always an implicit this so that the following are equivalent总是有一个隐含的this以便以下是等价的

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

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

However, locals take precedence over members so that但是,本地人优先于成员,因此

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

assigns 17 so the variable foo that is a parameter to the method.分配17所以变量foo是方法的参数。 If you want to assign to the instance member when you have a method where there is a local with the same name, you must use this to refer to it.如果要在有一个方法中存在同名本地的方法时分配给实例成员,则必须使用this来引用它。

this is a variable which represents the current instance of a class.这是一个变量,代表 class 的当前实例。 For example例如

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

In this example, this is used to set the "sample" property on someclass obj, to the current instance of SampleClass.在此示例中,这用于将 someclass obj 上的“sample”属性设置为 SampleClass 的当前实例。

Refers to current instance of class指 class 的当前实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM