C# มีการใช้ is เป็น operator สำหรับการทำ type checking ว่า type ตรงกันหรือไม่ จะมีการคืนค่ากลับมาเป็น boolean (true or false) โดยจะทำการเปรียบเทียบการทำ type checking กับภาษา C#, Java, Python และ C++ ดังนี้
C# สามารถทำตรวจสอบความตรงกันหรือความเข้ากันได้ ด้วย is operator ได้ทั้ง primitive data type และ non-primitive data type โดยหน้า is สามารถเป็น instance หรือ variable ก็ได้ ด้านหลัง is เป็นได้ทั้งชื่อ data type และ class โดยคำนึงถึงหลักการ OOP
Java สามารถทำตรวจสอบความตรงกันหรือความเข้ากันได้ ด้วยการใช้ method isInstance ได้และกำหนด data type ที่ต้องการ check ไว้หน้า method ดังตัวอย่าง code ข้างต้น String.class.isInstance(variable)
Python สามารถทำตรวจสอบความตรงกันหรือความเข้ากันได้ โดยใช้ type()is ชื่อ class ว่า data type ตรงกันหรือไม่ โดยไม่คำนึงถึงหลักการ OOP และมี isinstance( , ) ในการ check data type ว่าตรงกันหรือไม่ โดยคำนึงถึงหลักการ OOP
C# สามารถทำการแปลง type แปลงได้เฉพาะ data type ที่อยู่ในตระกูลเดียวกันไม่ต้องเช็ค type ก่อนและคำนึงถึงหลักการ OOP นอกจากนี้ยังสามารถแปลงจาก null เป็น type อะไรก็ได้ ถ้าแปลงไม่ได้ก็แค่คืนค่า null
Operator C# - is,as : Microsoft 2024. (2023, August 4). Type-testing operators and cast expressions - is, as, typeof and casts [Microsoft Learn]. Microsoft Learn
Type Checking Python : Zack. (2022, April 13). type(), isinstance() เพื่อรับและกำหนดประเภทใน Python [From Local]. From-Local
Type Checking C++: Microsoft 2024. (2021, March 8). typeid Operator [Microsoft Learn]. Microsoft Learn
class Fruit{}
class Apple extends Fruit{}
class Banana extends Fruit{}
public class Project{
public static void main(String[] args)
{
Fruit f = new Fruit();
Apple a = new Apple();
Banana b = new Banana();
System.out.println(a instanceof Fruit);
int n = 5;
System.out.println(String.class.isInstance(n));
}
}
class Fruit:
pass
class Apple(Fruit):
pass
class Banana(Fruit):
pass
f = Fruit()
a = Apple()
b = Banana()
print(type(a) is Fruit)
print(type(5) is int)
print(isinstance(a, Fruit))
#include<iostream>
using namespace std;
class Fruit{};
class Apple : Fruit{};
class Banana :Fruit{};
int main(){
Fruit f;
Apple a;
Banana b;
cout << (typeid(a) == typeid(Apple)) << endl;
cout << (typeid(2) == typeid(string)) << endl;
return 0;
}
using System;
public class Fruit{}
public class Apple : Fruit{}
public class Banana :Fruit{}
public class Project
{
public static void Main(string[] args)
{
Fruit f = new Fruit();
Apple a = new Apple();
Banana b = new Banana();
object n = null;
Console.WriteLine(f as Banana); //output null
Console.WriteLine(b as Fruit);
Console.WriteLine(f as Fruit);
Console.WriteLine(n as Fruit); //output null
}
}
class Fruit {}
class Apple extends Fruit {}
class Banana extends Fruit {}
public class Project {
public static void main(String[] args) {
Fruit f = new Fruit();
Apple a = new Apple();
Banana b = new Banana();
int n = 5;
Double m = (double)n;
System.out.print(m);
if(f instanceof Apple){
Apple apple = (Apple) f;
System.out.println(apple);
}
else{
System.out.println("f isn't instanceof Apple");
}
}
}
class Fruit:
pass
class Apple(Fruit):
pass
class Banana(Fruit):
pass
f = Fruit()
a = Apple()
b = Banana()
if isinstance(f, Apple):
apple = f
print("f can do it")
if isinstance(b, Fruit):
fruit = b
print("b can do it")
int = 5
num_float = float(5)
print(num_float)