<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # 如何:使用 as 和 is 運算符安全地進行強制轉換(C# 編程指南) 由于對象是多態的,因此基類類型的變量可以保存派生類型。若要訪問派生類型的方法,需要將值強制轉換回該派生類型。不過,在這些情況下,如果只嘗試進行簡單的強制轉換,會導致引發 [InvalidCastException](https://msdn.microsoft.com/zh-cn/library/system.invalidcastexception.aspx) 的風險。這就是 C# 提供 [is](https://msdn.microsoft.com/zh-cn/library/scekt9xw.aspx) 和 [as](https://msdn.microsoft.com/zh-cn/library/cscsdfbt.aspx) 運算符的原因。您可以使用這兩個運算符來測試強制轉換是否會成功,而沒有引發異常的風險。通常,**as** 運算符更高效一些,因為如果可以成功進行強制轉換,它會實際返回強制轉換值。而 **is** 運算符只返回一個布爾值。因此,如果只想確定對象的類型,而無需對它進行實際強制轉換,則可以使用 is 運算符。 下面的示例演示如何使用 **is** 和 **as** 運算符從一個引用類型強制轉換為另一個引用類型,而沒有引發異常的風險。此示例還演示如何對 **as** 運算符使用可以為 null 值的類型。 ``` class SafeCasting { class Animal { public void Eat() { Console.WriteLine("Eating."); } public override string ToString() { return "I am an animal."; } } class Mammal : Animal { } class Giraffe : Mammal { } class SuperNova { } static void Main() { SafeCasting app = new SafeCasting(); // Use the is operator to verify the type. // before performing a cast. Giraffe g = new Giraffe(); app.UseIsOperator(g); // Use the as operator and test for null // before referencing the variable. app.UseAsOperator(g); // Use the as operator to test // an incompatible type. SuperNova sn = new SuperNova(); app.UseAsOperator(sn); // Use the as operator with a value type. // Note the implicit conversion to int? in // the method body. int i = 5; app.UseAsWithNullable(i); double d = 9.78654; app.UseAsWithNullable(d); // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } void UseIsOperator(Animal a) { if (a is Mammal) { Mammal m = (Mammal)a; m.Eat(); } } void UseAsOperator(object o) { Mammal m = o as Mammal; if (m != null) { Console.WriteLine(m.ToString()); } else { Console.WriteLine("{0} is not a Mammal", o.GetType().Name); } } void UseAsWithNullable(System.ValueType val) { int? j = val as int?; if (j != null) { Console.WriteLine(j); } else { Console.WriteLine("Could not convert " + val.ToString()); } } } ``` ## 請參閱 [類型(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/ms173104.aspx) [強制轉換和類型轉換(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/ms173105.aspx) [可以為 null 的類型(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/1t3y8s4s.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看