ในภาษา C# this keyword คำว่า this เป็นคำที่ใช้ช่วยในการอ้างอิงถึง วัตถุ(object)ที่ถูกสร้างจากคลาสนั้นหรือเรียกว่า Instance คำว่า this สามารถใช้ได้ในหลายสถานการณ์ เช่น
SetNameWithoutThis: aomsin
SetNameWithThis: Dollar
อธิบาย CODE
จาก code ด้านบนเป็นการใช้this เพื่ออ้างอิง field name ของ instance คลาสปัจจุบัน โดยใช้Methodแสดงให้เห็นความต่างของการใช้ this และไม่ใช้ this เริ่มแรกเราให้ field name มีค่าเป็น aomsin อยากทำการเปลี่ยนเป็น Dollar ผ่านMethodทั้งสอง สังเกตุได้ว่าMethodที่ไม่มีการใช้ this ไม่มีผลต่อ field name ของคลาส Pet เพราะมันไม่ได้ใช้ this เพื่ออ้างอิง field ของ instance แต่ถ้ามีการใช้ this จะส่งผลทำให้ field name ของ instance ของคลาส Pet ที่ถูกสร้างขึ้นมาเปลี่ยนจาก aomsin เป็น Dollar
2. ใช้ this เพื่อเรียกใช้ constructor ตัวอื่นภายในคลาสเดียวกัน
Example
using System;
class Pet {
public string name;
public int age;
public Pet(string name)
{
this.name = name;
this.age = 1;
}
public Pet(string name, int age) : this(name)
{
this.age = age;
}
public void DisplayInfo()
{
Console.WriteLine("Name: " + name + ", Age: " + age);
}
}
class SetPet {
public static void Main()
{
Pet pet1 = new Pet("Aomsin");
pet1.DisplayInfo();
Pet pet2 = new Pet("Dollar", 5);
pet2.DisplayInfo();
}
}
OUTPUT
Name: Aomsin, Age: 1
Name: Dollar, Age: 5
อธิบาย CODE
ใน constructor แรกรับพารามิเตอร์ name กำหนดค่าให้กับ this.name และกำหนดค่า age เป็น 1 โดยอัตโนมัติเมื่อไม่ได้ระบุอายุมา ส่วน constructor ที่สองใช้ this(name)เพื่อเรียกใช้ constructor แรกที่มีพารามิเตอร์ name ก่อน จากนั้นจึงกำหนดค่า age แยกต่างหากภายใน constructor ที่สอง เมื่อส่ง name เข้าไปอย่างเดียวจะเรียกใช้ constructor ที้่รับชื่อเท่านั้น จึงทำให้มีอายุเป็น 1 อัตโนมัติ และหากส่งทั้ง name และ age จะเรียกใช้ constructor ทีี่รับทั้งชื่อและอายุ
3. ใช้ this เพื่อเรียกใช้เมธอดคลาสปัจจุบัน
Example
using System;
public class Pet
{
public string name;
public void SetName(string name)
{
this.name = name;
}
public void DisplayPetInfo()
{
Console.WriteLine("Pet's Name: " + this.name);
this.Hello();
}
public void Hello()
{
Console.WriteLine("Hello! I am " + name);
}
}
public class SetPet
{
public static void Main()
{
Pet obj = new Pet();
obj.SetName("Aomsin");
obj.DisplayPetInfo();
}
}
OUTPUT
Pet's Name: Aomsin
Hello! I am Aomsin
อธิบาย code
จาก code ด้านบนมีการเรียกใช้ Method Hello โดยใช้this.ตามฟิลด์ name เพื่อเรียกใช้เมธอดในคลาส Pet
4.การใช้คีย์เวิร์ด this เป็นพารามิเตอร์ในMethod
Example
using System;
class Pet {
public string name;
public Pet(string name)
{
this.name = name;
}
public void DisplayPetName()
{
Console.WriteLine("Pet name: " + this.name);
}
public void PrintPetDetails(Pet pet)
{
Console.WriteLine("Pet name from another method: " + pet.name);
}
public void ShowDetails()
{
this.PrintPetDetails(this);
}
}
class Program {
public static void Main()
{
Pet myPet = new Pet("Aomsin");
myPet.ShowDetails();
}
}
OUTPUT
Pet name from another method: Aomsin
อธิบาย code
ในเมธอด showdetail มีการเรียก method PrintPetDetails ในคลาส Pet และส่งคลาส Pet เป็นพารามิเตอร์ให้ method PrintPetDetail เพื่อที่จะใช้ตัวแปร Name ของคลาส Pet
5. การใช้ this เพื่อประกาศ Indexer
Example
using System;
using System.Collections.Generic;
class Pet {
private List<string> petNames = new List<string>();
public string this[int index]
{
get
{
if (index >= 0 && index < petNames.Count)
{
return petNames[index];
}
else
{
return "Index out of range";
}
}
set
{
if (index >= 0 && index < petNames.Count)
{
petNames[index] = value;
}
else if (index == petNames.Count)
{
petNames.Add(value);
}
}
}
public void AddPet(string name)
{
petNames.Add(name);
}
}
class SetPet {
public static void Main()
{
Pet obj = new Pet();
obj.AddPet("Aomsin");
obj.AddPet("Dollar");
Console.WriteLine("Pet at index 0: " + obj[0]);
Console.WriteLine("Pet at index 1: " + obj[1]);
obj[1] = "Gold";
Console.WriteLine("Updated Pet at index 1: " + obj[1]);
obj[2] = "Silver";
Console.WriteLine("Pet at index 2: " + obj[2]);
}
}
OUTPUT
Pet at index 0: Aomsin
Pet at index 1: Dollar
Updated Pet at index 1: Gold
Pet at index 2: Silver
คำอธิบาย code
Index declaration ประกอบด้วย get และ set ใน get เลขที่รับมามากกว่าเท่ากับ 0 และ เลขที่รับมามีข้อมูลในตำแหน่งนั้นจะทำการแสดงชื่อสัตว์เลี้ยงออกมา แต่ถ้าเลขที่รับมไม่มีข้อมูลในตำแหน่งนั้นจะไม่แสมาข้อความว่า Index out of range ส่วน set เลขที่รับมามากกว่าเท่ากับ 0 และ เลขที่รับมามีข้อมูลในตำแหน่งนั้นจะทำการเปลี่ยนค่าในตำแหน่งนั้นแทนหากเลขที่รับมาเกินจำนวนข้อมูลจะเป็นการเพิ่มข้ใมูลเข้าใน Array
ตัวอย่างการ Code ที่ใช้ this keyword
using System;
class Pet
{
private string name;
private string breed;
private decimal price;
public Pet(string name, string breed) : this(name, breed, 0.0m)
{
// Calls the three-parameter constructor
}
public Pet(string name, string breed, decimal price)
{
this.name = name;
this.breed = breed;
this.price = price;
}
public string Name
{
get { return this.name; }
}
public string Breed
{
get { return this.breed; }
}
public decimal Price
{
get { return this.price; }
set { this.price = value; }
}
public string this[int index]
{
get
{
if (index == 0) return this.name;
if (index == 1) return this.breed;
return "Invalid index";
}
set
{
if (index == 0) this.name = value;
if (index == 1) this.breed = value;
}
}
public void PrintPetInfo()
{
Console.WriteLine("Pet Name: {0}\nBreed: {1}\nPrice: {2:C}", this.name, this.breed, this.price);
}
public void ChangePrice(decimal newPrice)
{
this.price = newPrice;
this.PrintPriceUpdate();
}
private void PrintPriceUpdate()
{
Console.WriteLine("The price has been updated to {0:C}", this.price);
}
}
class MainClass
{
static void Main()
{
Pet pet1 = new Pet("Bella", "Golden Retriever", 12000.00m);
Pet pet2 = new Pet("Milo", "Siamese Cat");
pet1.PrintPetInfo();
Console.WriteLine();
pet2.PrintPetInfo();
Console.WriteLine();
pet1.ChangePrice(13000.00m);
pet2[0] = "Luna";
pet2[1] = "Persian Cat";
Console.WriteLine("\nUpdated pet2 info using indexer:");
pet2.PrintPetInfo();
}
}
OUTPUT
Pet Name: Bella
Breed: Golden Retriever
Price: ฿12,000.00
Pet Name: Milo
Breed: Siamese Cat
Price: ฿0.00
The price has been updated to ฿13,000.00
Pet Name: Bella
Breed: Golden Retriever
Price: ฿13,000.00
Updated pet2 info using indexer:
Pet Name: Luna
Breed: Persian Cat
Price: ฿0.0
คำอธิบาย
คลาส Pet ประกอบด้วยชื่อสัตว์เลี้ยง(name) สายพันธุ์(breed) และราคา(price) คลาสPetมี constructor 2 แบบ แบบแรกจะมีการกำหนดราคา(price) เป็น 0 โดยอัตโนมัติ แบบที่สองจะรับราคา(price)มาจากเราสามารถกำหนดเองได้ และมี property หรือ ตัวกลางของ name,breed,price โดย property ของ price สามารถเปลี่ยนได้ด้วยโครงสร้าง set ของ property ต่อมามีการใช้ this เพื่อประกาศ Indexer ทำให้สามารถเข้าถึงและ upload ข้อมูลได้ตามเลข index ในส่วนของ method ChangePrice มีการใช้ this เพื่อเข้าถึง instance ของคลาสปัจจุบัน และ ใช้ this เพื่อเรียก method ภายในคลาสเดียวกัน