locked
How to extend List of interfaces to class? RRS feed

  • Question

  • User-1078128378 posted

    Hi All,

    How can i extend list of interface to a class

    For example i have below interfaces:

    interface I1 {
    int ID { get; set; } 
    } 
    interface I2 {
    int AMOUNT { get; set; }
    }
    
    

    and i have below class i want to be implement like this

    class Test : List<I1>, List<I2> {
     }

    could anyone please tell me how can i perform above task?

    Sunday, October 7, 2018 12:50 PM

Answers

  • User-369506445 posted

    hi

    when you drive List<I1> and List<I2>, in <g class="gr_ gr_8 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="8" data-gr-id="8">fact</g> you are <g class="gr_ gr_15 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="15" data-gr-id="15">inheritance</g> from 2 List classes that c# doesn't support Multiple inheritances, namely each class inherits from a single parent class(<g class="gr_ gr_114 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del" id="114" data-gr-id="114">here</g> are 2 classes called List )

    you need to use IList instead of List

    class Test : IList<I1>, List<I2>
    {
    }

    in this <g class="gr_ gr_9 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="9" data-gr-id="9">case</g> you have to implement the IList interface

    but you can define a List of your Interface in your class

    interface I1
        {
            int ID { get; set; }
        }
        interface I2
        {
            int AMOUNT { get; set; }
        }
        class Test
        {
            public List<I1> listL1;
            public List<I2> listL2;
    
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, October 7, 2018 2:06 PM