Is vs As

กวิสรา ประสิทธิ์นุ้ย 650710524

is Operator ?

C# มีการใช้ is เป็น operator สำหรับการทำ type checking ว่า type ตรงกันหรือไม่ จะมีการคืนค่ากลับมาเป็น boolean (true or false) โดยจะทำการเปรียบเทียบการทำ type checking กับภาษา C#, Java, Python และ C++ ดังนี้

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();
        Console.WriteLine(a is Fruit);
        
        int n = 5;
        Console.WriteLine(n is string);
    }
}

Output

True

False

  • 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++ สามารถทำตรวจสอบความตรงกันหรือความเข้ากันได้ โดยใช้ typeid() == typeid ถ้าตรงกันก็จะคืนค่าเป็น 1 คือ true และถ้าไม่ใช่ก็จะคืนค่าเป็น 0 ซึ่งก็คือ false โดยคำนึงถึงหลักการ OOP


as Operator ?

C# มีการใช้ as เป็น operator การแปลงโดยไม่ต้อง throws exception ถ้าไม่สามารถแปลงได้ จะคืนค่ามาเป็น null โดยจะมีการเปรียบเทียบการแปลงกับภาษา C#, Java และ Python ดังนี้

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
    }
}

Output

Banana

Fruit

  • C# สามารถทำการแปลง type แปลงได้เฉพาะ data type ที่อยู่ในตระกูลเดียวกันไม่ต้องเช็ค type ก่อนและคำนึงถึงหลักการ OOP นอกจากนี้ยังสามารถแปลงจาก null เป็น type อะไรก็ได้ ถ้าแปลงไม่ได้ก็แค่คืนค่า null

  • Java, Python ในการแปลงต้องมีการ throws exception เพื่อป้องกันการ error และเช็ค type ก่อนการแปลงด้วย ในกรณีที่ไม่สามารถทำการแปลงได้


Is vs As

จากตัวอย่างโค้ดข้างต้นจะเห็นถึงความแตกต่างของ operator is - as ดังนี้

is
as

ใช้เพื่อเช็คความตรงกันหรือความเข้ากันได้ของ object หรือ variable

ใช้เพื่อการแปลง (casting)

คืนค่าเป็น boolean (true or false)

ถ้าสามารถแปลงได้จะคืนค่าเป็น object ที่แปลงได้ แต่ถ้าไม่สามารถทำการแปลงได้จะคืนค่าเป็น null


Presentation

ISvsASOperatorinC#_650710524.pdf

Video Clip

Youtube : ISvsAsOperatorInC#_650710524


Reference

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

as operator Keyword : ลาภลอย วานิชอังกูร. (2556). เรียนรู้ด้วยตนเอง OOP C# ASP.NET. กรุงเทพฯ : ซีเอ็ดยูเคชั่น. (ISBN(e-book) : 978-616-08-0678-2)

Is vs As : Ankita_saini. (2019, January 21). Is vs As operator keyword in C# [greekforgreek]. greekforgreek

Last updated