コード例 #1
0
        //через as
        void UseOne2(One one)
        {
            Two two = (one as Two);

            if (two != null && two.SomeBool == false)
            {
                two.Display();
            }
            else
            {
                Console.WriteLine("Преобразование не удалось");
            }
        }
コード例 #2
0
 //через try catch
 void UseOne3(One one)
 {
     try
     {
         Two two = (Two)one;
         if (two.SomeBool == false)
         {
             two.Display();
         }
     }
     catch (InvalidCastException e)
     {
         Console.WriteLine(e.Message);
     }
 }
コード例 #3
0
 // через is
 void UseOne1(One one)
 {
     if (one is Two)
     {
         Two two = (Two)one;
         if (two.SomeBool == false)
         {
             two.Display();
         }
     }
     else
     {
         Console.WriteLine("Преобразование не удалось");
     }
 }