Rumah >pembangunan bahagian belakang >Tutorial C#.Net >Apakah kegunaan kata kunci 'is' dalam C#?

Apakah kegunaan kata kunci 'is' dalam C#?

WBOY
WBOYke hadapan
2023-09-04 19:09:03875semak imbas

C# 中“is”关键字有什么用?

"is" kata kunci digunakan untuk menyemak sama ada objek boleh ditukar kepada jenis tertentu. Jenis pengembalian operasi ialah Boolean.

Contoh

using System;
namespace DemoApplication{
   class Program{
      static void Main(){
         Employee emp = new PermanentEmployee{
            ID = 1,
            Name = "Martin"
         };
         // Returns true as the derived type can be converted to base type.
         if (emp is Employee){
            Console.WriteLine(emp.Name + " is Employee");
         }
         else{
            Console.WriteLine(emp.Name + " is not Employee");
         }
         //Returns true, as the actual object is of type PermanentEmployee.
         if (emp is PermanentEmployee){
            Console.WriteLine(emp.Name + " is PermanentEmployee");
         }
         else{
            Console.WriteLine(emp.Name + " is not PermanentEmployee");
         }
         //Returns false, as PermanentEmployee object cannot be converted to
         //ContractEmployee.
         if (emp is ContractEmployee){
            Console.WriteLine(emp.Name + " is ContractEmployee");
         }
         else{
            Console.WriteLine(emp.Name + " is not ContractEmployee");
         }
      }
   }
   class Employee{
      public int ID { get; set; }
      public string Name { get; set; }
   }
   class PermanentEmployee : Employee{
      public int AnnualSalary { get; set; }
   }
   class ContractEmployee : Employee{
      public int HourlySalary { get; set; }
   }
}

Atas ialah kandungan terperinci Apakah kegunaan kata kunci 'is' dalam C#?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:tutorialspoint.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam
Artikel sebelumnya:Apakah pengaturcaraan C#?Artikel seterusnya:Apakah pengaturcaraan C#?