<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # out(泛型修飾符)(C# 參考) 對于泛型類型參數,**out** 關鍵字指定該類型參數是協變的。可以在泛型接口和委托中使用 **out** 關鍵字。 通過協變,可以使用與泛型參數指定的派生類型相比,派生程度更大的類型。這樣可以對委托類型和實現變體接口的類進行隱式轉換。引用類型支持協變和逆變,但值類型不支持。 如果接口具有協變類型形參,則允許其方法返回與接口類型形參指定的派生類型相比,派生程度更大的類型的實參。例如,由于在 .NET Framework 4 的 [IEnumerable&lt;T&gt;](https://msdn.microsoft.com/zh-CN/library/9eekhta0.aspx) 接口中,類型 T 是協變的,因此無需使用任何特殊轉換方法就可以將 IEnumerabe(Of String) 類型的對象分配給 IEnumerable(Of Object) 類型的對象。 可以向協變委托分配同一類型的其他委托,但需使用派生程度較大的泛型類型參數。 有關更多信息,請參見[協變和逆變(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/ee207183.aspx)。 下例演示如何聲明、擴展和實現一個協變泛型接口。此外還演示了如何對實現協變接口的類使用隱式轉換。 ``` // Covariant interface. interface ICovariant<out R> { } // Extending covariant interface. interface IExtCovariant<out R> : ICovariant<R> { } // Implementing covariant interface. class Sample<R> : ICovariant<R> { } class Program { static void Test() { ICovariant<Object> iobj = new Sample<Object>(); ICovariant<String> istr = new Sample<String>(); // You can assign istr to iobj because // the ICovariant interface is covariant. iobj = istr; } } ``` 在泛型接口中,當符合下列條件時,可以將類型參數聲明為是協變的。 * 類型形參僅用作接口方法的返回類型,不用作方法實參的類型。 | ![](https://box.kancloud.cn/2016-01-31_56adb62c1380a.jpg) 注意 | | :-- | | 此規則有一個例外。如果在協變接口中,包含用作方法參數的逆變泛型委托,則可以將協變類型用作此委托的泛型類型參數。有關協變和逆變泛型委托的更多信息,請參見[委托中的變體(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd233060.aspx)和[對 Func 和 Action 泛型委托使用變體(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd465122.aspx)。 | * 類型參數不用作接口方法的泛型約束。 下例演示如何聲明、實例化和調用一個協變泛型委托。此外還演示了如何隱式轉換委托類型。 ``` // Covariant delegate. public delegate R DCovariant<out R>(); // Methods that match the delegate signature. public static Control SampleControl() { return new Control(); } public static Button SampleButton() { return new Button(); } public void Test() { // Instantiate the delegates with the methods. DCovariant<Control> dControl = SampleControl; DCovariant<Button> dButton = SampleButton; // You can assign dButton to dControl // because the DCovariant delegate is covariant. dControl = dButton; // Invoke the delegate. dControl(); } ``` 在泛型委托中,如果類型僅用作方法返回類型,且不用于方法參數,則可聲明為是協變的。 ## C# 語言規范 有關詳細信息,請參閱 [C# 語言規范](https://msdn.microsoft.com/zh-CN/library/ms228593.aspx)。該語言規范是 C# 語法和用法的權威資料。 ## 請參閱 [泛型接口中的變體(C# 和 Visual Basic)](https://msdn.microsoft.com/zh-CN/library/dd233059.aspx) [in(泛型修飾符)(C# 參考)](https://msdn.microsoft.com/zh-CN/library/dd469484.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>

                              哎呀哎呀视频在线观看