/* * Section 7.14.4 Var */ void UseVar() { GenericZombie first = new GenericZombie("Stubbs"); GenericZombie second = new GenericZombie("Frankenstein"); GenericZombie third = new GenericZombie("Michael"); /* ┌─────────────────────────────────┐ */ /*┌─┤ Assumes the type after assigned │ */ /*↓ └─────────────────────────────────┘ */ var someThings = new ThreeThings<GenericZombie>(first, second, third); Debug.Log(someThings); // 1:A Zombie named Stubbs 2:A Zombie named Frankenstein 3:A Zombie named Michael var whatAmI = 1; Debug.Log(whatAmI.GetType()); // System.Int32 Debug.Log(someThings.GetType()); // Generics+ThreeThings`1[Generics+GenericZombie] }
void UseSwapGenerics() { GenericHumanoid[] humanoids = new GenericHumanoid[2]; humanoids[0] = new GenericZombie("Stubbs"); humanoids[1] = new GenericVampire("D"); Swap(ref humanoids[0], ref humanoids[1]); foreach (GenericHumanoid humanoid in humanoids) Debug.Log(humanoid); // A Vampire named D // A Zombie named Stubbs GenericZombie first = new GenericZombie("Rob"); string second = "Jackson"; Debug.Log(first + " " + second); //Swap(ref first, ref second); /* uncomment the line above to see the error */ // Error CS0411 // The type arguments for method 'Generics.Swap<T>(ref T, ref T)' // cannot be inferred from the usage. // Try specifying the type arguments explicitly. }