none
Generic Interfaces (C# Programming Guide) RRS feed

  • Question

  • Hello,

    I was going through the topic on Generic Interfaces (C# Programming Guide) whose link is

    http://msdn.microsoft.com/en-us/library/kwtft8ak.aspx  I came across the statement "When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used" . I tried to understand but could not. Please help. The code as below.

    regards

    Manoj Gokhale

    // Statement => When an interface is specified as a constraint 
    // on a type parameter, only types that implement 
    // the interface can be used
    namespace ConsoleApplication1
    {
        public interface I1<T>
        {
             void m1(int aa);
        }
    
        // interface is specified as a constraint 
        // on a type parameter
        public class genclass<T> where T : I1<T>
        {
            public void m1(int x){}
            public void m2(){}
        }
        // genclass1<T> does implement the interface I1<T>
        public class genclass1<T> : I1<T>
        {
            public void m1(int yy){}
            public void m3(){}
        }
        // class2<T> does not implement the interface I1<T>
        public class class2<T>
        {
            public void m4(){}
        }
    
        class TestGenericList
        {
            static void Main()
            {
                genclass1<int> xx = new genclass1<int>();
                // No compile error
                xx.m3();
                class2<int> yy = new class2<int>();
                yy.m4();
            }
            
        }
    }

    Saturday, December 29, 2012 10:57 AM

All replies

  • I think the comment is refereing to the following class

         public class Person : System.IComparable<Person>

    The above class your can only sort on 'Person'

    The general class uses this class

           public class SortedList<T> : GenericList<T> where T : System.IComparable<T>

    The above class will sort on any type of System.IComparable<T>


    jdweng

    Saturday, December 29, 2012 12:17 PM
  • Hello,

    " When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used"

    What I did not understand was "only types that implement the interface can be used" what does it mean?

    Thanks and regards

    Manoj Gokhale

    Saturday, December 29, 2012 12:37 PM
  • I actually didn't know about it, but now I see it, I think I understand. Example (based on your code):

    using System;
    
    namespace MSDN_Ajuda2
    {
        public interface I1
        {
            void m1(int aa);
        }
        public interface I1<T>
        {
            void m1(int aa);
        }
        public interface I2
        : I1<I2> // added this
        {
            void m1();
        }
    
        public class genclass<T> where T : I1<T>
        {
            public void m1(int x) { }
            public void m2() { }
        }
        public class genclass1<T> : I1<T>
        {
            public void m1(int yy) { }
            public void m3() { }
        }
        public class genclass2<T> where T : I1 // new class here
        {
            public void m1(int x) { }
            public void m2() { }
        }
        // class2<T> does not implement the interface I1<T>
        public class class2<T>
        {
            public void m4() { }
        }
        public class class3 : I1<class3>
        {
            public void m1(int x) { }
            public void m4() { }
        }
        public class class4 : I1<I2>, I2
        {
            public void m1() { }
            public void m1(int x) { }
            public void m4() { }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                genclass<int> a = new genclass<int>(); // compiler error here
                genclass<I1<int>> a2 = new genclass<I1<int>>(); // compiler error here
                genclass<I1<I1<int>>> a2 = new genclass<I1<I1<int>>>(); // compiler error here, it doesn't matter how many times you use nested generic I1
                genclass<I2> a4 = new genclass<I2>(); // OK
                genclass<class3> a5 = new genclass<class3>(); // OK
                genclass<class4> a6 = new genclass<class4>(); // compiler error here
                genclass1<int> xx = new genclass1<int>(); // logically, this is OK, replace 'T' with 'int' in your mind and you'll understand
                genclass2<int> b = new genclass2<int>(); // compiler error here
    
                xx.m3();
                class2<int> yy = new class2<int>();
                yy.m4();
                Console.ReadKey();
            }
        }
    }

    12 clarifying errors appear, and can be justified by that statement: "When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used.". Also, sorry for the messy code, I hope it helps you anyway.


    "Penso, logo existo" - René Descartes
    "A produção de muitas coisas úteis resulta em muitas pessoas inúteis" - Karl Marx
    "Vive como se fosses morrer amanhã, aprende como se fosses viver para sempre" - Mahatma Gandhi

    João Miguel



    • Edited by JMCF125 Saturday, December 29, 2012 1:38 PM correcting another typo
    Saturday, December 29, 2012 1:29 PM
  • Your example is way too complicated, the following example produces a compiler error because class2 doesn't implement I1 and as a result it cannot be used as argument for genclass:

    interface I1 {
    }
    class genclass<T> where T : I1 {
    }
    class class2 {
    }
    class TestGenericList {
        static void Main() {
            genclass<class2> foo;
        }
    }
    To use class2 as argument for genclass class2 must implement I1.
    Saturday, December 29, 2012 1:35 PM
    Moderator
  • Hello

    Thanks all of you for helping me  by examples and completing the suffix of the problem statement

    Problem statement => When an interface is specified as a constraint on a type parameter, only types that implement the interface can be used

    "as argument for generic class"  was the implied suffix of the above statement

    Thanks again for your quick response

    regards

    Manoj Gokhale

    Saturday, December 29, 2012 6:24 PM