is keyword

กฤติเดช บรรพพงศา 650710522

- is keyword คือ?

ในภาษา C# IS keyword จะตรวจสอบว่าประเภทนิพจน์ หรือ ตัวแปร นั้นเข้ากันได้กับประเภท(type) ที่กำหนดหรือไม่

ลักษณะการใช้งาน is ใน C#:

  1. ใช้ is ตรวจสอบประเภท ในขณะ Run-time ว่าเข้ากันได้กับ ประเภทที่กำหนด หรือ ไม่

  2. ใช้ is ตรวจสอบความเข้ากันของประเภท object หาก object ที่กำหนดมีประเภทเดียวกัน จะคืนค่า true หากไม่ตรงกันจะคืนค่า false และ หาก object มีค่า null จะคืนค่า false เช่นกัน

using System;
public class Dog {
	
}
public class Cat {
	
}
public class test {

	public static void Main()
	{
		
		Dog jimmy = new Dog(); //สร้าง Jimmy จาก Dog
		
		Console.WriteLine(jimmy is Dog); //jimmy is Dog?? --> TRUE
		
		Console.WriteLine(jimmy is Cat); //jimmy is Cat?? --> FALSE	
	}
}

อธิบาย

  1. บรรทัดที่ 13 : สร้างออบเจ็กต์ jimmy จากคลาส Dog โดย jimmy เป็น instance ของ class Dog

  2. บรรทัดที่ 15 : ใช้ is ตรวจสอบว่า jimmy เป็น instance ของ Dog หรือไม่? --> คืนค่า true

  3. บรรทัดที่ 17 : ใช้ is ตรวจสอบว่า jimmy เป็น instance ของ Cat หรือไม่? --> คืนค่า false

- รูปแบบการใช้ is ใน c#

1. Type checking

ซึ่งการใช้ is ทำ Type-checking มีรูปแบบ

E is T
  • E ( expression ) เป็นนิพจน์ที่มีการคืนค่า

  • T (type) เป็นประเภทที่ต้องการเปรียบเทียบ

ตัวอย่างการใช้ is แบบปกติ กับเงื่อนไข

using System;
int a = 23;
int b = 7;
if (a is int  &&  b is int )
{
    Console.WriteLine(a + b); 
}

ตัวอย่างการใช้ is เปรียบเทียบความเข้ากันobject และ class

using System;

public class Car { }

public class Toyota : Car { }

public static class IsOperatorExample
{
    public static void Main()
    {
        object A = new Car();
        Console.WriteLine(A is Car);  // output: True
        Console.WriteLine(A is Toyota);  // output: False

        object B = new Toyota();
        Console.WriteLine(B is Car);  // output: True
        Console.WriteLine(B is Toyota); // output: True
    }
}

2. Type checking with Pattern matching

using System;
class test
{
    static void Main()
    {
        object greeting = "Hello World!";
        
        if (greeting is string message)
        {
            Console.WriteLine(message);
        }
    }
}

อธิบาย

บรรทัดที่ 8 : ใช้ is เพื่อเช็คว่า greeting นั้นเป็น string รึป่าว ถ้าใช่ ก็ จะกำหนดค่าของ greeting ให้ตัวแปร massage

บรรทัดที่ 10 : พิมพ์ค่า ของ massage ที่ถูกจับคู่กับ greeting : output -> Hello World!

3. Negation pattern

การใช้ is รูปแบบปฏิเสธ เช่น เช็คค่าที่ไม่เป็น null

if (input is not null)
{
    // ...
}

- C# is keyword ตัวอย่างเปรียบเทียบกับภาษา อื่นๆ

using System;
public class Dog {
	
}

public class Cat {
	
}
public class test {

	public static void Main()
	
	{
		Dog jimmy = new Dog(); //สร้าง Jimmy จาก Dog
		
		Console.WriteLine(jimmy is Dog); //jimmy is Dog?? --> TRUE
		
		Console.WriteLine(jimmy is Cat); //jimmy is Cat?? --> FALSE
	}
}

Output

True

False

- สรุปความแตกต่าง

Java

ใน java ไม่มี is แต่ใช้ instanceof keywordในการตรวจสอบวัตถุ (object) ว่าเป็นประเภท (type) หรือ instance ของ class ที่กำหนดหรือไม่ ผลลัพธ์จะเป็น True หรือ False

C++

ใน C++ ไม่มีคำสั่ง is เหมือนใน C# แต่มีการใช้ typeid( ) เพื่อตรวจสอบประเภทของ object คล้าย is ใน C#

Python

ใน Python มี is keyword แต่ is ใน python ใช้เพื่อตรวจสอบว่าสองตัวแปรอ้างอิงถึงวัตถุเดียวกันหรือไม่

ซึ่งจะคืนค่า True หากสองวัตถุเป็นวัตถุเดียวกัน และ จะคืนค่า False หากสองวัตถุไม่ใช่วัตถุเดียวกัน

ซึ่งในการทำ type checking ใน python จะ ใช้ isinstance( ) ที่มีการใช้คล้ายกับ is ใน c#

is keyword ใน Python ต่างจาก C#

ใน python ใช้ is เพื่อตรวจสอบว่าสองตัวแปรอ้างอิงถึงวัตถุเดียวกันหรือไม่

ซึ่งจะคืนค่า True หากสองวัตถุเป็นวัตถุเดียวกัน และ จะคืนค่า False หากสองวัตถุไม่ใช่วัตถุเดียวกัน

x = ["apple", "banana", "cherry"]

y = ["apple", "banana", "cherry"]

z = x

print(x is y)

print(x is z)

- Video

- Slide presentation

- References

เว็บไซต์ที่ใช้ศึกษา C# is keyword ความหมาย ลักษณะการใช้งานในรูปแบบต่าง การใช้ is

  1. Microsoft. (2023, November 14). is operator (C# reference). Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/is

  2. GeeksforGeeks. (n.d.).C# is operator keyword. GeeksforGeeks. https://www.geeksforgeeks.org/c-sharp-is-operator-keyword/

  3. Microsoft. (2023, April 8). Type-testing operators and cast expressions (C# reference). Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast

  4. NileBits. (2023, April 8). C# keywords tutorial part 49.is. NileBits. https://www.nilebits.com/blog/2023/04/c-keywords-tutorial-part-49-is/

เว็บไซต์ที่ใช้ศึกษา ความแตกต่างของการใช้ is keyword , การใช้ keyword ที่คล้ายกับการใช้ is keyword ใน C#

  1. java

W3Schools. (n.d.). Java instanceof keyword. W3Schools. https://www.w3schools.com/java/ref_keyword_instanceof.asp

  1. python

W3Schools. (n.d.). Python isinstance() function. W3Schools. https://www.w3schools.com/python/ref_func_isinstance.asp

W3Schools. (n.d.). Python is keyword. W3Schools. https://www.w3schools.com/python/ref_keyword_is.asp

  1. C++

Microsoft. (n.d.). typeid operator (C++). Microsoft Learn. https://learn.microsoft.com/th-th/cpp/cpp/typeid-operator?view=msvc-170

Last updated