Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press

Buy directly from Paradoxal Press at $33.99 (Save 43%)



Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA


Listing 13-31 extracted from chapter Generics


Listing 13-30<     > Listing 13-32


This listing can be compiled with the command line:
csc.exe /target:exe Example_13_31.cs
Errors: 6 Warnings: 0


Example_13_31.cs
class C {
    public static U Fct1<U>() { return default(U); }
    public static void Fct2<U>( U u ) { return; }
    public static U Fct3<U>( U u ) { return default(U); }
    public static void Fct4<U>( U u1, U u2 ) { return; }
    public static void Fct5<U>( U[] arrayOfU ) { return; }
}
class Program {
   static void Main() {
      // Compilation error: The type arguments for method 
      // 'C.Fct1<U>()' cannot be inferred from the usage.
      string = C.Fct1();

      // Compilation error: Cannot implicitly convert type 
      // 'System.IDisposable' to 'string'.
      string = C.Fct1<System.IDisposable>();
      = C.Fct1<string>(); // OK

      C.Fct2( "hello" ); // Infer 'U' as 'string'.

      // Compilation error: The type arguments for 
      // method 'C.Fct2<U>(U)' cannot be inferred from the usage.
      C.Fct2( null );

      int = C.Fct3( ); // Infer 'U' as 'int.

      double = C.Fct3( ); // Awkward: Infer 'U' as 'int' ...
                              // ... and not as 'double'.

      // Compilation error: Cannot implicitly convert 'int' 
      // to 'System.IDisposable'.
      System.IDisposable dispose = C.Fct3( );

      // Infer 'U' as 'string'.
      C.Fct4( "hello""bonjour" ); 

      // Compilation error: The type arguments for method 
      // 'C.Fct4<U>(U,U)' cannot be inferred from the usage.
      C.Fct4( 5"bonjour" );

      C.Fct5( new int[6] ); // Infer 'U' as 'int.
   }
}	
Copyright Patrick Smacchia 2006 2007