-9

What is the syntax for implementing an array of an interface?
iWord is an interface.

This is the syntax for List

public class Words : List<iWord>
{

}

But this fails
With an error message invalid base type
Am I getting an error because I am asking for something stupid?

public class Words : iWord[]
{

}
paparazzo
  • 44,497
  • 23
  • 105
  • 176
  • You cannot do that, maybe `IEnumerable` will do? What do you want to achive? – Zbigniew Apr 20 '13 at 14:26
  • @walkhard Basically IEnumerable and I will just add an indexer(this). I will give it a try. If you want to mark that as an answer I will accept. – paparazzo Apr 20 '13 at 14:28
  • Why not work with List<> and then turn it into array if needed? I find lists much easier to manipulate than arrays. – bobek Apr 20 '13 at 14:34
  • @Blam to put it into better words. Array's are part of the language itself and not written as part of the framework like a List or Stack or some other data structure. Any type that exists you could put a `[]` against it and now you've got an array of that type. So as a result you don't need to implement an array. Here is a question : **What would you put into your words class?** – gideon Apr 20 '13 at 14:35
  • @bobek Because under the covers is it an array as that is better suited for what I need than List – paparazzo Apr 20 '13 at 14:40
  • @gideon I just need to expose an enumerator and a this. – paparazzo Apr 20 '13 at 14:47
  • @Blam you'll get all of that when you inherit from `List` if you want it readonly then there are readonly lists too. – gideon Apr 20 '13 at 15:02
  • @gideon Under the covers is it an Array and I use it as an Array. Yes I could expose an enumerator and indexer with about any collection. – paparazzo Apr 25 '13 at 12:57

1 Answers1

1

You can't do that, but since arrays implements IEnumerable and IEnumerable<T> maybe it'd be enough to

class Words : IEnumerable<iWord> 

or you can just stick with your initial idea, after all List has an indexer:

class Words : List<iWord>
// use indexer to get first element
var myWords = new Words();
var first = myWords[0];
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
  • Because under the covers I need Array.Copy Method (Array, Int32, Array, Int32, Int32). To the outside world only need to expose IEnumerable and indexer. – paparazzo Apr 20 '13 at 14:44
  • If you really need to expose array to the outside word you can use `ToArray()` method. You can also take a look at [this](http://stackoverflow.com/questions/1921701/how-to-copy-list-to-array). – Zbigniew Apr 20 '13 at 14:47
  • See my comment "To the outside world only need (want) to expose IEnumerable and a (read only) indexer". Under the covers I do not want to use a List. Under the covers I need an array and that is what I have. I used List as a syntax example that did not throw an error - not because I wanted to expose a list. IEnumerable with with a read only indexer is exactly what I need. – paparazzo Apr 20 '13 at 15:14