示例#1
0
        public string ConcatFirstAndPrint <A>(A a, T dummy)
        {
            var b1 = new MyBox <A, int>(a);
            var b2 = new MyBox <int, MyBox <int, int> >(3);
            var b3 = new MyBox <int, MyBox <A, A> >(4);

            return("t=" + t + ", a=" + a.ToString() + ", Second=" + Second);
        }
示例#2
0
        static void Main()
        {
            {
                TestLogger.Log("Testing first- vs higher-kinded and mono- vs polymorphic-method combinations...");
                var l = new List <string>();
                l.Add("abc");
                TestLogger.Log(new FirstOrderClassPolymorphicMethod().Test(l));
                TestLogger.Log(new HigherKindedClassMonomorphicMethod <string>().Test(l));
                var dict = new Dictionary <string, int>();
                dict.Add("def", 3);
                TestLogger.Log(new HigherKindedClassPolymorphicMethod <string>().Test(dict));
            }

            {
                TestLogger.Log("Testing implicit implementations of interface methods on different instantiations of the same interface ...");
                TestSameInterfaceTwice <string>("test");
                TestSameInterfaceTwice <int>(1);
            }

            {
                TestLogger.Log("Testing implicit and explicit implementations of interface methods on the same interface...");
                var x = new ImplicitAndExplicit <string>();
                x.Test1(new Wrapper <string>("hij"));
                ITwoTests <Wrapper <string> > y = x;
                y.Test2(new Wrapper <string>("klm"));
            }

            {
                TestLogger.Log("Testing construction and field mutation...");
                var p = new Person("pqr");
                TestLogger.Log(p.ToString());

                var b1 = new MyBox <Person, string>(new Person("opq"));
                b1.u = "b1";
                var b2 = new MyBox <int, string>(1);
                b2.u = "b2";

                TestLogger.Log(b1.ToString());
                TestLogger.Log(b2.ToString());

                b1.t = new Person("rst");
                b1.u = "uvw";
                b2.t = 3;

                TestLogger.Log(b1.ToString());
                TestLogger.Log(b2.ToString());
            }

            {
                TestLogger.Log("Testing mini generic lists of object...");
                var list = new MyList <object>();
                list.Add(1);
                list.Add("2");
                list.Add(new Person("pqr"));
                foreach (var obj in list)
                {
                    TestLogger.Log(obj.ToString());
                }
            }

            {
                TestLogger.Log("Testing mini generic dictionary of string to int...");
                var dict = new MyDictionary <string, int>();
                dict.Add("one", 1);
                dict.Add("two", 2);
                dict.Add("three", 3);
                foreach (var kvPair in dict)
                {
                    TestLogger.Log(kvPair.Key + " -> " + kvPair.Value);
                }
            }

            {
                TestLogger.Log("Testing static fields on distinct type instances...");
                MyList <int> .testVal    = 1;
                MyList <string> .testVal = 2;

                TestLogger.Log(MyList <int> .testVal.ToString());
                TestLogger.Log(MyList <string> .testVal.ToString());
            }

            {
                TestLogger.Log("Testing generic methods with interesting permutation of type arguments..");;
                var b = new MyBox <Person, string>(new Person("opq"));
                b.u = "second";
                TestGenericMethod <Person, string, int>(b, 5, null);
                TestGenericMethod <Person, string, string>(b, "xyz", null);
                TestGenericMethod <Person, string, Person>(b, new Person("efg"), null);
            }

            {
                TestLogger.Log("Testing recursive types...");
                var a = new A <B>();
                var b = new B();
                TestLogger.Log(a.M(b));
                TestLogger.Log(b.M(a));
            }
        }
示例#3
0
 static void TestGenericMethod <B, C, A>(MyBox <B, C> b, A val, B dummyVal)
 {
     TestLogger.Log(b.ConcatFirstAndPrint <A>(val, dummyVal));
 }