📜  C#|这个关键字

📅  最后修改于: 2021-05-29 23:20:56             🧑  作者: Mango

关键字用于引用该类的当前实例。它用于从构造函数,实例方法和实例访问器访问成员。关键字还用于跟踪实例,该实例被调用以执行一些与该实例相关的计算或进一步处理。以下是在C#中使用’this’关键字的不同方法:

程序1:使用“ this”关键字引用当前的类实例成员

// C# program for using 'this' keyword to
// refer current class instance members
using System;
namespace geeksforgeeks {
  
class Geeks {
  
    // instance member
    public string Name; 
  
    public string GetName()
    {
        return Name;
    }
  
  
    public void SetName(string Name)
    {
         // referring to current instance
         //"this.Name" refers to class member
         this.Name = Name; 
    }
}
  
class program { 
  
    // Main Method
    public static void Main()
    {
        Geeks obj = new Geeks();
        obj.SetName("Geeksforgeeks");
        Console.WriteLine(obj.GetName());
    }
}
}
输出:
Geeksforgeeks

程序2:使用this()调用同一类中的构造函数

// C# program for using 'this' keyword to 
// invoke the constructor in same class
using System;
namespace geeksforgeeks {
  
class Geeks {
  
    // calling another constructor
    // calls Geeks(string Name) 
    // before Geeks()
    public Geeks() : this("geeks")
    {
        Console.WriteLine("Non-Parameter Constructer Called");
    }
  
    // Declare Parameter Constructer
    public Geeks(string Name)
    {
        Console.WriteLine("Parameter Constructer Called");
    }
}
  
class program {
      
    // Main Method
    public static void Main()
    {
            Geeks obj = new Geeks();
            // Warning: obj never used..
    }
}
}
输出:
Parameter Constructer Called
Non-Parameter Constructer Called

程序3:使用“ this”关键字调用当前的类方法

// C# code for using this to invoke current 
// class method
using System;
class Test {
  
    void display()
    {
        // calling function show()
        this.show();
      
        Console.WriteLine("Inside display function");
    }
      
    void show() 
    {
        Console.WriteLine("Inside show funcion");
    }
      
    // Main Method
    public static void Main(String []args) 
    {
        Test t1 = new Test();
        t1.display();
    }
}
输出:
Inside show funcion
Inside display function

程序4:使用“ this”关键字作为方法参数

// C# code for using 'this' 
// keyword as method parameter
using System;
class Test
{
    int a;
    int b;
      
    // Default constructor
    Test()
    {
        a = 10;
        b = 20;
    }
      
    // Method that receives 'this' 
    // keyword as parameter
    void display(Test obj)
    {
        Console.WriteLine("a = " + a + " b = " + b);
    }
  
    // Method that returns current
    // class instance
    void get()
    {
        display(this);
    }
  
    // Main Method
    public static void Main(String[] args)
    {
        Test obj = new Test();
        obj.get();
    }
}
输出:
a = 10 b = 20

程序5:使用此关键字声明索引器

// C# code for using 'this' 
// keyword to declare indexer
using System;
namespace geeksforgeeks {
      
class Geeks {
      
    private string[] days = new string[7];
  
    // declaring an indexer
    public string this[int index]
    {
        get 
        {
            return days[index];
        }
  
        set
        { 
            days[index] = value;
        }
    }
}
  
  
class Program {
      
    // Main Method
    public static void Main()
    {
        Geeks g = new Geeks();
        g[0] = "Sun";
        g[1] = "Mon";
        g[2] = "Tue";
        g[3] = "Wed";
        g[4] = "Thu";
        g[5] = "Fri";
        g[6] = "Sat";
        for (int i = 0; i < 7; i++)
            Console.Write(g[i] + " ");
    }
}
}
输出:
Sun Mon Tue Wed Thu Fri Sat