static void Main() { // for constructors, type parameter needs to be explicitly specified var instanceWithInt = new GenericClass <int>(7); instanceWithInt.SomeMethod(); var instanceWithString = new GenericClass <string>("doișpe"); instanceWithString.SomeMethod(); // Factory method uses generic type inference: // no need to specify type parameters var instanceWithFloat = GenericClass.Create(1.23); instanceWithFloat.SomeMethod(); // call MoreGenericMethod with various other types instanceWithInt.MoreGenericMethod(333, 1); instanceWithInt.MoreGenericMethod(333, "hopa"); instanceWithInt.MoreGenericMethod(333, 0.001); // non-generic classes can have generic methods var nonGenericInstance = new NonGenericClass(); nonGenericInstance.GenericMethod(3); nonGenericInstance.GenericMethod(Math.PI); nonGenericInstance.GenericMethod("π"); ConstraintExamples.InterfaceConstraint(new MemoryStream()); }
static void Main(string[] args) { Check <int> c = new Check <int>(); var chk = c.Compare(2, 4); Console.WriteLine($"Comparison between 2 & 4 is: {chk}"); Check <string> c2 = new Check <string>(); var chk2 = c2.Compare("Ron", "Ron"); Console.WriteLine($"Comparison between Ron & Ron is: {chk2}"); NonGenericClass ngc = new NonGenericClass(); var max = ngc.Max(3, 10); Console.WriteLine($"Maximum between 3 & 10 is: {max}"); Console.ReadLine(); }