Array of Interfaces
-
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...
-
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...
//this initializes only the array, not the elements _interfaces = new IMyInterface[10]; //now you have an array filled with 10 "null" values
-
//this initializes only the array, not the elements _interfaces = new IMyInterface[10]; //now you have an array filled with 10 "null" values
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 evennull
. - Daniël PelsmaekerAn egoist is someone who doesn't think about me.
-
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 evennull
. - Daniël PelsmaekerAn egoist is someone who doesn't think about me.
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 aNullReferenceException
orArgumentNullException
depending on how you use the array elements. Step through in a debugger and you'll find that they'renull
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 anInt32
will be 0, etc.).Microsoft MVP, Visual C# My Articles
-
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 evennull
. - Daniël PelsmaekerAn egoist is someone who doesn't think about me.
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];