C# 何时何地使用;这";关键词

C# 何时何地使用;这";关键词,c#,this,keyword,C#,This,Keyword,可能重复: 大家好 我只是想知道何时何地必须使用关键字?因为有时候,如果我不说“这个”,甚至删除“这个”,程序就会正常运行。那么如果你不使用它会发生什么呢?或者如果你在错误的地方使用它 一些明确的例子是值得推荐的。调用静态成员时不能使用此选项。编译器不会让你这么做的。当您使用“this”时,您显式地调用了当前实例。我喜欢用“this”作为当前实例成员的前缀,即使这不是强制性的,但只是为了清楚起见。这样我就可以区分局部范围变量和成员 干杯呼叫静态成员时不能使用此选项。编译器不会让你这么做的。当您

可能重复:

大家好

我只是想知道何时何地必须使用关键字?因为有时候,如果我不说“这个”,甚至删除“这个”,程序就会正常运行。那么如果你不使用它会发生什么呢?或者如果你在错误的地方使用它


一些明确的例子是值得推荐的。

调用静态成员时不能使用此选项。编译器不会让你这么做的。当您使用“this”时,您显式地调用了当前实例。我喜欢用“this”作为当前实例成员的前缀,即使这不是强制性的,但只是为了清楚起见。这样我就可以区分局部范围变量和成员


干杯

呼叫静态成员时不能使用此选项。编译器不会让你这么做的。当您使用“this”时,您显式地调用了当前实例。我喜欢用“this”作为当前实例成员的前缀,即使这不是强制性的,但只是为了清楚起见。这样我就可以区分局部范围变量和成员


Cheers

关键字通常用于消除成员变量和局部变量之间的歧义:

...
{
    int variable = 10;

    this.variable = 1; // modifies a member variable.

    variable = 1; // modifies the local variable.
}
....
的另一个用途是传递对当前对象的引用,以便:

....
DoSomethingWithAnObject( this );
....
另一个用途(感谢)是在将方法参数分配给具有相同名称的成员变量时消除歧义:

void Initialise( int variable )
{ 
    this.variable = variable;
}

关键字通常用于消除成员变量和局部变量之间的歧义:

...
{
    int variable = 10;

    this.variable = 1; // modifies a member variable.

    variable = 1; // modifies the local variable.
}
....
的另一个用途是传递对当前对象的引用,以便:

....
DoSomethingWithAnObject( this );
....
另一个用途(感谢)是在将方法参数分配给具有相同名称的成员变量时消除歧义:

void Initialise( int variable )
{ 
    this.variable = variable;
}
您不必每次在成员方法中都使用“this”关键字,它在这种情况下很有用:

class human
{
    private int age;
...
    void func(int age)
    {
       this.age = age;
    }
...
}
它可以解决您所指的年龄混淆问题

您不必每次在成员方法中都使用“this”关键字,它在这种情况下很有用:

class human
{
    private int age;
...
    void func(int age)
    {
       this.age = age;
    }
...
}
public class People{
    private String name;
    private int age;
    public People(String name, int age){
        this.name = name;
        this.age = age; //use this to declare that the age is the field in People class 
    }
}
它可以解决你对年龄的困惑

public class People{
    private String name;
    private int age;
    public People(String name, int age){
        this.name = name;
        this.age = age; //use this to declare that the age is the field in People class 
    }
}
一种使用方法,希望能对你有所帮助


一种使用方法,希望它能帮助您。

Microsoft建议对成员变量使用camelCase,即

public class MyClass
{
     private int myInt;

     private void SetMyInt(int myInt)
     {
          this.myInt = myInt;
     }
}
因此,如果您没有'this'关键字,则私有成员和参数之间会出现混淆

就我个人而言,我更喜欢在我的私人成员前面加下划线,以避免这种混淆

private int _myInt;
因此,我发现它的唯一真正用途是将当前对象的引用传递给其他对象

MyStaticClass.MyStaticMethod(this);

Microsoft建议对成员变量使用camelCase,即

public class MyClass
{
     private int myInt;

     private void SetMyInt(int myInt)
     {
          this.myInt = myInt;
     }
}
因此,如果您没有'this'关键字,则私有成员和参数之间会出现混淆

就我个人而言,我更喜欢在我的私人成员前面加下划线,以避免这种混淆

private int _myInt;
因此,我发现它的唯一真正用途是将当前对象的引用传递给其他对象

MyStaticClass.MyStaticMethod(this);

当您在同一范围内有一个成员变量和一个同名的局部变量时,可以使用此选项,“this”将明确您指的是哪一个

考虑这一点:

class Person
{
    private string name;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    public Person(string name)
    {
        this.name = name;   // "this" is necessary here to disambiguate between the name passed in and the name field.
        this.Name = name;   // "this" is not necessary, as there is only one Name in scope.
        Name = name;        // See - works without "this".
    }
}

当您在同一范围内有一个成员变量和一个同名的局部变量时,可以使用此选项,“this”将明确您指的是哪一个

考虑这一点:

class Person
{
    private string name;

    public string Name
    {
        get
        {
            return name;
        }
        set
        {
            name = value;
        }
    }

    public Person(string name)
    {
        this.name = name;   // "this" is necessary here to disambiguate between the name passed in and the name field.
        this.Name = name;   // "this" is not necessary, as there is only one Name in scope.
        Name = name;        // See - works without "this".
    }
}

就我个人而言,每当我访问成员变量或方法时,我都会使用这个。99%的情况下,这是不必要的,但我认为它提高了清晰度,使代码更具可读性——这一质量非常值得额外努力键入4个字符和一个点。

就我个人而言,每当我访问成员变量或方法时,我都会使用这个。99%的情况下,这是不必要的,但我认为它提高了清晰度,使代码更具可读性——这是一个值得额外努力的质量,只需键入4个字符和一个点。

这有点主观,但如果您使用正确的命名约定(我对成员变量使用
\u camelCase
)您将永远不需要使用

除此例外:=)

如果要从扩展方法所用于的类内部调用扩展方法,请执行以下操作:

public class HomeController : Controller
{

    public ActionResult Index()
    {
        // won't work
        return MyCustomResult();
    }

    public ActionResult List()
    {
        // will work
        return this.MyCustomResult();
    }
}

public static class MyExtensions
{
    public static MyResult MyCustomResult(this Controller instance)
    {
         return new SomeResult(instance.ActionName);
    }
}

这有点主观,但如果您使用正确的命名约定(我对成员变量使用
\u camelCase
),您将永远不需要使用

除此例外:=)

如果要从扩展方法所用于的类内部调用扩展方法,请执行以下操作:

public class HomeController : Controller
{

    public ActionResult Index()
    {
        // won't work
        return MyCustomResult();
    }

    public ActionResult List()
    {
        // will work
        return this.MyCustomResult();
    }
}

public static class MyExtensions
{
    public static MyResult MyCustomResult(this Controller instance)
    {
         return new SomeResult(instance.ActionName);
    }
}

还用于将方法参数分配给具有相同名称的成员变量。我还想补充一点,有一个学派建议避免使用“this”进行排序的范围变量冲突,因为它可能导致混淆。我倾向于在严格的短期基础上使用它-例如,在构造函数上,当您要做的只是写“this.liveStatus=liveStatus”时,它是无害的还用于将方法参数分配给具有相同名称的成员变量。我还想补充一点,有一个学派建议避免使用“this”进行排序的范围变量冲突,因为它可能导致混淆。我倾向于在严格的短期基础上使用它-例如,在构造函数上,当您要做的只是写“this.liveStatus=liveStatus”时,它是无害的