<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # in(泛型修飾符)(C# 參考) 對于泛型類型參數,**in** 關鍵字指定該類型參數是逆變的。可以在泛型接口和委托中使用 **in** 關鍵字。 通過逆變,可以使用與泛型參數指定的派生類型相比,派生程度更小的類型。這樣可以對委托類型和實現變體接口的類進行隱式轉換。引用類型支持泛型類型參數中的協變和逆變,但值類型不支持。 在泛型接口或委托中,如果類型形參僅用作方法返回類型,而不用于方法實參,則可聲明為協變的。 **Ref** 和 **out** 參數不能為變體。 如果接口具有逆變類型形參,則允許其方法接受與接口類型形參指定的派生類型相比,派生程度更小的類型的實參。例如,由于在 .NET Framework 4 的 [IComparer&lt;T&gt;](https://msdn.microsoft.com/zh-CN/library/8ehhxeaf.aspx) 接口中,類型 T 是逆變的,因此如果 Employee 繼承 Person,則無需使用任何特殊轉換方法,就可以將 IComparer(Of Person) 類型的對象指派給 IComparer(Of Employee) 類型的對象。 可以向逆變委托分配同一類型的其他委托,但需使用派生程度較小的泛型類型參數。 有關更多信息,請參見[協變和逆變(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/ee207183.aspx)。 下例演示如何聲明、擴展和實現一個逆變泛型接口。此外還演示了如何對實現此接口的類使用隱式轉換。 ``` // Contravariant interface. interface IContravariant<in A> { } // Extending contravariant interface. interface IExtContravariant<in A> : IContravariant<A> { } // Implementing contravariant interface. class Sample<A> : IContravariant<A> { } class Program { static void Test() { IContravariant<Object> iobj = new Sample<Object>(); IContravariant<String> istr = new Sample<String>(); // You can assign iobj to istr because // the IContravariant interface is contravariant. istr = iobj; } } ``` 下例演示如何聲明、實例化和調用一個逆變泛型委托。此外還演示了如何隱式轉換委托類型。 ``` // Contravariant delegate. public delegate void DContravariant<in A>(A argument); // Methods that match the delegate signature. public static void SampleControl(Control control) { } public static void SampleButton(Button button) { } public void Test() { // Instantiating the delegates with the methods. DContravariant<Control> dControl = SampleControl; DContravariant<Button> dButton = SampleButton; // You can assign dControl to dButton // because the DContravariant delegate is contravariant. dButton = dControl; // Invoke the delegate. dButton(new Button()); } ``` ## C# 語言規范 有關詳細信息,請參閱 [C# 語言規范](https://msdn.microsoft.com/zh-CN/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 請參閱 [out(泛型修飾符)(C# 參考)](https://msdn.microsoft.com/zh-CN/library/dd469487.aspx) [協變和逆變(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/ee207183.aspx) [修飾符(C# 參考)](https://msdn.microsoft.com/zh-CN/library/6tcf2h8w.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>

                              哎呀哎呀视频在线观看