Home
Browsez les 647 exemples
Téléchargez les 647 exemples
Téléchargez des chapitres
Achetez sur amazon.fr

Niveau: Débutant/Intermédiaire
ISBN-2-84177-339-6
50 Euros

Exemple 13-31 extrait du chapitre La généricité


Exemple 13-30<     > Exemple 13-32


Cet exemple peut être compilé avec la ligne de commande:
csc.exe /target:exe Exemple_13_31.cs
Erreurs: 6 Avertissements: 0
Remarque:


Exemple_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() {
      // Erreur de compilation: The type arguments for method 
      // 'C.Fct1<U>()' cannot be inferred from the usage.
      string = C.Fct1();

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

      C.Fct2("hello"); // Infère: le type paramètre U est string.

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

      int = C.Fct3(6); // Infère: le type paramètre U est int.

      double = C.Fct3(6); // ATTENTION: Infère: le type paramètre 
                            // U est int et non pas double.

      // Erreur de compilation: Cannot implicitly convert 'int' 
      // to 'System.IDisposable'.
      System.IDisposable dispose = C.Fct3(6);

      // Infère le type paramètre U est string.
      C.Fct4("hello""bonjour"); 

      // Erreur de compilation: 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]); // Infère: le type paramètre U est int.
   }
}	
Copyright Patrick Smacchia 2006 2007