this Keyword
ธนพัฒน์ สถิตย์กุลรัตน์ 650710548
ในภาษา C# this keyword คำว่า this เป็นคำที่ใช้ช่วยในการอ้างอิงถึง วัตถุ(object)ที่ถูกสร้างจากคลาสนั้นหรือเรียกว่า Instance คำว่า this สามารถใช้ได้ในหลายสถานการณ์ เช่น
1. ใช้ this เพื่ออ้างถึง Instance ของคลาส
Example
using System;
class Pet {
public string name = "Aomsin";
public void SetNameWithoutThis(string name)
{
name = name;
}
public void SetNameWithThis(string name)
{
this.name = name;
}
public string GetName()
{
return name;
}
}
class SetPet {
public static void Main()
{
Pet obj = new Pet();
obj.SetNameWithoutThis("Dollar");
Console.WriteLine("SetNameWithoutThis: " + obj.GetName());
obj.SetNameWithThis("Dollar");
Console.WriteLine("SetNameWithThis: " + obj.GetName());
}
}OUTPUT
อธิบาย CODE
2. ใช้ this เพื่อเรียกใช้ constructor ตัวอื่นภายในคลาสเดียวกัน
Example
OUTPUT
อธิบาย CODE
3. ใช้ this เพื่อเรียกใช้เมธอดคลาสปัจจุบัน
Example
OUTPUT
อธิบาย code
4.การใช้คีย์เวิร์ด this เป็นพารามิเตอร์ในMethod
Example
OUTPUT
อธิบาย code
5. การใช้ this เพื่อประกาศ Indexer
Example
OUTPUT
คำอธิบาย code
ตัวอย่างการ Code ที่ใช้ this keyword
OUTPUT
คำอธิบาย
นำมาเขียนด้วยภาษา C
ความแตกต่างระหว่างภาษา C และภาษา C#
สรุป
นำมาเขียนด้วยภาษา Java
ความแตกต่างระหว่าง Java และ C#
ตัวอย่างเปรียบเทียบ Property ใน C# กับ Java (เพิ่มเติมจาก code ด้านบน)
Code ProperTy Syntax In C#
Code In Java
นำมาเขียนด้วย Python
ความต่างระหว่าง C# กับ Python
Reference
Last updated

