2013-02-07 2 views

답변

2

할 수 있습니다. 이것은 컴파일러에게 IC<IA> 변수에 IC<A>을 할당 할 수 있음을 알립니다.

자세한 내용은 this page을 참조하십시오.

1

당신은 당신이 캐스트가 작동하려면 interface IC<out T>IC을 선언 할 필요가

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    interface IPerson 
    { 
    } 

    //Have to declare T as out 
    interface ICrazy<out T> 
    { 
    } 

    class GTFan : IPerson 
    { 
    } 

    class CrazyOldDude : ICrazy<GTFan> 
    { 
    } 

    class Program 
    { 
     static void Main(string[] args) { 
      IPerson someone; 
      someone = (IPerson)new GTFan(); // <~~~ No exception here 

      ICrazy<GTFan> crazyGTFanatic; 
      ICrazy<IPerson> crazyPerson; 

      crazyGTFanatic = new CrazyOldDude() as ICrazy<GTFan>; 

      crazyGTFanatic = (ICrazy<GTFan>)(new CrazyOldDude()); 

      crazyPerson = (ICrazy<IPerson>)crazyGTFanatic; 
     } 
    } 
} 
+1

예, 그렇지만 'ica'를 null로 설정하면 도움이되지 않습니다. 많은. –

+0

@BrianRasmussen 오, 그렇다면 ica가 절대로 null이 아닐 것이라고 말하는 것입니까? 그럼에도 불구하고 내 제안을 사용하면 _is_ null로 정확합니다. – Rake36

+0

예, 캐스트가 유효하지 않으므로 'as'연산자가 null을 반환합니다. –