Wozu dient das Schlüsselwort „is' in C#?

WBOY
Freigeben: 2023-09-04 19:09:03
nach vorne
723 Leute haben es durchsucht

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

Das Schlüsselwort „is“ wird verwendet, um zu prüfen, ob ein Objekt in einen bestimmten Typ konvertiert werden kann. Der Rückgabetyp der Operation ist Boolean.

Beispiel

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; }
   }
}
Nach dem Login kopieren

Das obige ist der detaillierte Inhalt vonWozu dient das Schlüsselwort „is' in C#?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!