THIS关键字

来源:互联网 发布:辐射4卡顿优化补丁 编辑:程序博客网 时间:2024/04/30 00:49
his仅限于在构造函数、类的方法和类的实例中使用。
this 关键字引用类的当前实例

经常在构造函数或者类方法中,如果传入参数和类字段同名,一定需在类字段前加上this

using System;
class A
{
    private int x;
    public A(int x)
   {
        this.x = x;
   }


    public void PrintX()
    {
        x = 5;
        Console.WriteLine("The value of x is: {0}", x);
        Console.WriteLine("The value of this.x is: {0}", this.x);
    }


    public static void Main()
    {
        A a = new A();
        a.PrintX();
    }
}

因为this不是静态变量,因此它不能在静态方法中使用

0 0
原创粉丝点击