Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array of Interfaces

Array of Interfaces

Scheduled Pinned Locked Moved C#
tutorialquestioncsharpcomdata-structures
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Meysam Mahfouzi
    wrote on last edited by
    #1

    Suppose that I have an interface named IMyInterface and two classes both of which implement this interface, say ClassA and ClassB. Now I need a collection of these classes. So I have to have an array of IMyInterface(I don't want to use any thing like ArrayList or something). Now my problem is how to initiate this kind of array so that it would be able to hold both ClassA and ClassB? For example think I need an array of size 10:

    private IMyInterface[] _interfaces;
    _interfaces = new (what?)[10]

    ps: What is the best way of having such a dynamic array? should I have a temp array and use .CopyTo method and re-new the old array?


    Don't forget, that's

    Persian Gulf

    not Arabian gulf!


    Murphy:Click Here![^] Events and Delegates simplified:Click Here![^]


    I'm thirsty like sun, more landless than wind...

    C 1 Reply Last reply
    0
    • M Meysam Mahfouzi

      Suppose that I have an interface named IMyInterface and two classes both of which implement this interface, say ClassA and ClassB. Now I need a collection of these classes. So I have to have an array of IMyInterface(I don't want to use any thing like ArrayList or something). Now my problem is how to initiate this kind of array so that it would be able to hold both ClassA and ClassB? For example think I need an array of size 10:

      private IMyInterface[] _interfaces;
      _interfaces = new (what?)[10]

      ps: What is the best way of having such a dynamic array? should I have a temp array and use .CopyTo method and re-new the old array?


      Don't forget, that's

      Persian Gulf

      not Arabian gulf!


      Murphy:Click Here![^] Events and Delegates simplified:Click Here![^]


      I'm thirsty like sun, more landless than wind...

      C Offline
      C Offline
      Corinna John
      wrote on last edited by
      #2

      //this initializes only the array, not the elements _interfaces = new IMyInterface[10]; //now you have an array filled with 10 "null" values

      U 1 Reply Last reply
      0
      • C Corinna John

        //this initializes only the array, not the elements _interfaces = new IMyInterface[10]; //now you have an array filled with 10 "null" values

        U Offline
        U Offline
        User 260964
        wrote on last edited by
        #3

        You are right about interfaces = new IMyInterface[10];, but if I recall it correctly, the array is set to a size of 10, but contains NO values, not even null. - Daniël Pelsmaeker

        An egoist is someone who doesn't think about me.

        H W 2 Replies Last reply
        0
        • U User 260964

          You are right about interfaces = new IMyInterface[10];, but if I recall it correctly, the array is set to a size of 10, but contains NO values, not even null. - Daniël Pelsmaeker

          An egoist is someone who doesn't think about me.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Interfaces are reference types, hence the array is filled with null. Try to call methods on any of those array elements (before initialization) and you'll get a NullReferenceException or ArgumentNullException depending on how you use the array elements. Step through in a debugger and you'll find that they're null as well. Structs and other value types - on the other hand, do not require instantiation in an array and their members will be the default values for whatever Types they are (reference types will still be null since you can't provide a default constructor with structs, but an Int32 will be 0, etc.).

          Microsoft MVP, Visual C# My Articles

          1 Reply Last reply
          0
          • U User 260964

            You are right about interfaces = new IMyInterface[10];, but if I recall it correctly, the array is set to a size of 10, but contains NO values, not even null. - Daniël Pelsmaeker

            An egoist is someone who doesn't think about me.

            W Offline
            W Offline
            Werdna
            wrote on last edited by
            #5

            Each element of your array will be set to null. There is no such thing as NO value. Every reference type is initialized to null. If you want dynamic array, you can use ArrayList() and just cast when you get elements: ArrayList list = new ArrayList(); list.add(elem); IMyInterface iface = (IMyInterface)list[i];

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups