this.textBox1.Text 和 textBox1.Text 两种写法哪一个更好?

解决方案 »

  1.   

    在这里用this体现不出this的价值!!
      

  2.   

    加上this是为了更好的区分本窗体的控件与其他窗体的控件,
    如果不需要区分这些控件,当然是后者好,简便
    其实执行效果是一样的
      

  3.   

    都加上this看代码的时候可以更好的区分哪些是当前类的成员 哪些不是
      

  4.   

    微软喜欢用前者,CodeRush喜欢用后者,各有各的喜好。如果你带上this,就意味着不能是静态类,必须实例化后才能使用,不带this则不受限制,所以CodeRush认为这个this是多余的,在它检查代码的时候会发出警告。
      

  5.   

    this.textBox1.Text 和 textBox1.Text 
    在这里没什么区别。
    在C#中,this关键字代表当前实例,我们可以用this.来调用当前实例的成员方法,变量,属性,字段等; 
    也可以用this来做为参数状当前实例做为参数传入方法. 
    还可以通过this[]来声明索引器下面是程序实例: 
    using System;              // 引入使命空间System 
    namespace CallConstructor // 声明命名空间CallConstructor 

    public class Car          // 声明类Car 

    int petalCount = 0;        //在Car类中声明一个非静态的整型变量petalCount,初始值为0 
                  未用Static声明的变量叫做静态变量,非静态成员属类的实例,我们只能在调用类的构
                  造函数对类进行实例化后才能通过所得的实例加"."来访问声明一个非静态的字符串变量s,                   
                  初始值为"null"; 注意:s = "null"与s = null是不同的                                                                                             
    String s = "null"; 
    Car(int petals)          // Car类的默认构造函数

    petalCount = petals;    // Car类的默认构造函数中为 petalCount 赋值为传入的参数petals的值 
    Console.WriteLine("Constructor w/int arg only,petalCount = " + petalCount); // 输出petalCount

    Car(String s, int petals)// 重载Car类的构造函数 Car(String s, int petals)的第二个参数
    : this(petals)         // : this(petals) 表示从当前类中调用petals变量的值来作为构造函数重载方法

    this.s = s; 
    Console.WriteLine("String & int args"); 

    // 在构造函数中为s赋值 
    // 非静态成员可以在构造函数或非静态方法中使用this.来调用或访问,也可以直接打变量的名字,因此这一句等效于s = s,
    //但是这时你会发类的变量s与传入的参数s同名,这里会造成二定义,所以要加个this.表示等号左边的s是当前类自己的变量
    Car()         // 重载构造函数,:
    : this("hi", 47) // this("hi", 47) 表示调Car(String s, int petals) 这个重载的构造函数,并直接传入变量"hi"和47

    Console.WriteLine("default constructor"); 

    public static void Main() 

    Car x = new Car(); 
    Console.Read(); 


    }
    比较使用:第一个this的意思是调用Car(int petals)方法的属性petals。 
    第二个this的意思是实例化Car(String s, int petals)方法中的参数s(this.s = s)。 
    第三个this是调用Car(String s, int petals)方法的两个参数并传参
      

  6.   

    不同的编程风格。写this.textBox1突出textBox1是类成员,不是局部变量,有助于程序阅读。
    如果类成员写成m_TextBox1 或 _textBox1,已经很清楚它们是类成员了,一般不写this。
      

  7.   

    程序运行上没有区别。
    编写上,this. 提供更友好的Intelisense。
      

  8.   

    只有在一种情况下是有区别的:
     public int myint = 1;
            public void test(int myint)//暂且不论我这个代码的实际意义,仅从语法上来看
            {
                myint = 1;//这个是给参数中那个myint赋值
                this.myint = 2;//这个是给成员变量myint赋值
            
            }
      

  9.   

    this首先基于当前的类操作的,你所说的体现不出有什么不同,都一样。当写的比较复杂时一般是要加“this”关键字的。
      

  10.   

    //默认情况下可以省略this
    public class Student{
    String name;
    public void study(){
        System.out.println(name+”在学习”);
      }
    }
    //当局部变量与成员属性的命名发生重名的时候,
    //根据就近原则会默认调用同名的局部变量
    public class BankCard{
    String cardNo;
    String cardPwd;
    public BankCard(String cardNo,String cardPwd){
       this.cardNo=cardNo;
    this.cardPwd=cardPwd;
      }
    public BankCard(String cardNo){
    this.cardNo=cardNo;
    cardPwd=“888888”;
    }
    }//如果在类中有多个构造方法,那么可以通过this在某一个构造方法中调用另一个构造方法,
    //但必须是在构造方法中的第一句,并且每个构造方法最多只调用一次,根据参数指定调用的构造方法
    public class BankCard{
    String cardNo,cardPwd;
    public BankCard(String cardNo,String cardPwd){
       this.cardNo=cardNo;
    this.cardPwd=cardPwd;
      }
    public BankCard(String cardNo){
    this(cardNo,”888888”);
    }
    }
    一共有三种用法
    有时非常重要