访问C#中的成员时,`this`关键字是可选的吗?

访问C#中的成员时,`this`关键字是可选的吗?,c#,.net,this,member,C#,.net,This,Member,我注意到,如果类中有一个私有成员,则可以通过引用其名称在类方法中访问它。您不需要说this.memberName,只要memberName就行了。那么,在成员访问上下文中,this关键字是可选的吗 我确实认为,当您想要澄清范围时——当您有两个同名的变量时,它是有用的。访问成员时是否有其他理由使用它?是的,它是可选的。只有当您有一个隐藏成员变量的局部变量,或者您想引用一个。是,这意味着“this”。它有时有助于澄清问题。调用方法和引用当前类也是必需的。您可以选择从实例成员内部使用this实例成员访

我注意到,如果类中有一个私有成员,则可以通过引用其名称在类方法中访问它。您不需要说
this.memberName
,只要
memberName
就行了。那么,在成员访问上下文中,this关键字是可选的吗


我确实认为,当您想要澄清范围时——当您有两个同名的变量时,它是有用的。访问成员时是否有其他理由使用它?

是的,它是可选的。只有当您有一个隐藏成员变量的局部变量,或者您想引用一个。

是,这意味着“this”。它有时有助于澄清问题。调用方法和引用当前类也是必需的。

您可以选择从实例成员内部使用
this
实例成员访问,就像实例方法或属性一样,因为无论何时调用实例方法
this
(指代当前对象)作为不可见参数自动传入

您不能从静态成员中使用
来访问实例成员。。。如果x和y是实例成员,则不能从静态方法或属性中使用
this.x
this.y
(甚至简单的x和y)。这是因为在静态成员调用中未定义此。静态成员属于整个类。。。它不知道此所指的是哪个实例。这是因为当您调用静态方法或属性时,调用的格式是
ClassName.MethodName()
因此静态方法不知道该将引用什么对象

也不是可选的(必须使用),作为扩展方法的参数列表中的第一个修饰符。实际上,
这就是将静态方法识别为扩展方法的原因。现在这里
这个
将第一个参数标识为扩展方法工作的实例

    using System;



    class Class_name
    {


        static  string static_variable="static";

        string instance_variable="instance";


        static void Main()
        {

            Class_name object_name = new Class_name();

            Console.WriteLine("Printing out instance and static variables from within Main() body :");

            Console.WriteLine(object_name.instance_variable);
            Console.WriteLine(Class_name.static_variable);

            /* Note that we cannot say either of the following :

                    object_name.static_variable 
                    Class_name.instance_variable


            */

            Console.WriteLine();




            // now lets call the static and instance methods

            object_name.Instance_method(); // Now this is the key call which 
            // passes "this" as an invisible parameter 
            // to the Instance_method. "this" refers to  
            //  object_name


            Class_name.Static_method();//  "this" is NOT passed to Static_method() because now 
            // the call is made on Class_name ... so there is nothing
            // to be represented by "this"


            Console.ReadLine();

        }



        void Instance_method()
        { 

            // here we receive "this" as an invisible parameter referring 
            // to the object on which  Instance_method is called (i.e. object_name)...
            // ... see the Main() method for comments at the call site. 


            Console.Write("Instace method called ... " +
                            "prints out instance variable twice, with and without 'this': ");

            // the following two calls mean exactly the same.

            Console.Write(this.instance_variable);
            Console.WriteLine (instance_variable);



            // one little additional point is that static members are 
            // accessible from within instance members


            Console.WriteLine();

            Console.Write("static variables can also be accessed from within Instance_method: ");
            Console.WriteLine(static_variable);

            Console.WriteLine();
            Console.WriteLine();




        }


        static void Static_method()
        {

            // See the Main() method body for the call Class_name.Static_method()
            // Notice that this method is called on Class_name and not object_name
            // which means that there is no invisibly passed-in "this" parameter available
            // in this method. 

            // we can also not access the instance_variable in this method 
            // as instance variables are always part of some object. This method
            // is not called on any object, its called on Class_name.

            // Console.WriteLine(instance_variable); // Compiler error

            Console.WriteLine("Static method called ... prints out static variable: ");
            Console.WriteLine(static_variable);




        }






    }
当您调用当前类的成员时,“This”几乎总是可选的

但是,它用于其他目的,例如将当前类实例作为参数传递,或将属性、字段或变量设置为当前实例

必须使用它来调用方法的唯一情况是调用扩展方法时:

class AClass {
    void CallIt() {
        Test();                     //not valid
        this.Test();                //valid 
    }
}
static class AnExtension {
    public static void Test(this AClass source) {

    }
}

如果编译器没有抱怨的话,我认为它是六个半打的另一个。是的,它是可选的。在我看来,过度使用“this”会使代码变得混乱。可能重复的可能重复的可能重复的如果你想调用一个扩展方法。这是真的,但不会有那么多情况下你想这样做。如果您可以访问该类,为什么要在其上定义扩展方法?我能想到的唯一原因可能是在一个继承自IEnumerable的类中,您希望使用Enumerable中定义的扩展方法之一。我希望添加的代码将澄清这个概念并消除混淆。这段代码是专门设计来说明从实例和静态成员内部访问实例和静态成员之间的区别,以及“this”的流程。你错了。我不必键入“this.”来从“this”类调用方法。我的意思是,如果您想调用引用回调用类成员的方法(即“引用当前类”)。不仅仅是打电话。