0

I have this class:

public class TextFiller
{
    public HtmlText Text { get; set; }
    public string Details { get; set; }
}

What I need is to create a parent class that will consist of many TextFillers. I am wondering the best way to do this. Would it be best to do it using what I have on line a, line b or is there some better solution?

public class TextFillerParent
{
  public TextFiller[] TextFiller { get; set; }    <<< a
  public IEnumerable<TextFiller> TextFiller { get; set; }  <<< b
}

What I will be doing is storing the data in Azure so I will want to be able to serialize TextFillerParent into a JSON string. Will there be any difference if the contents of TextFillerParent are an array of TextFiller or if I use the IEnumberable? Are there advantages in using IEnumerable?

I MUST be able to set the value of individual TextFiller. I know I can do this with an array but can I also do that if I use IEnumerable or is IEnumerable only good for sequential access?

Thank you.

Emily
  • 227
  • 1
  • 5
  • 9
  • 1
    An array must always be an arry, whereas an IEnumerable can be an array, a List, a Collection, or any other type that implements IEnumerable... Which is easier to use? –  Jun 13 '11 at 16:47
  • But if I wanted to use an array then don't I have to define it within the TextFillerParent like on line (a). You mention IEnumerable can be an array. How can I set the IEnumerable to be an array and does it really make a difference? Thanks – Emily Jun 13 '11 at 16:53
  • It makes a slight difference as to usability. Its a bit much to explain in comments. But think of it this way.. if I'm using your class, and I have a List and you require a TextFiller[] then I have to convert the list to an array. If you take any enumerable, I can pass in a List, an array, or any other type that implements IEnumerable. –  Jun 13 '11 at 18:46

4 Answers4

3

You may wish to use the generics version of IList if you need to access individual items. I would recommend it over the array.

Also, using LINQ, you can get much more than sequential access from IEnumerable objects.

David V
  • 11,531
  • 5
  • 42
  • 66
  • 1
    Why would a generic version of IList (not sure what that is), be better than an array? – Emily Jun 13 '11 at 16:52
  • Because an `IList` can be anything that implements IList, which allows indexing, element addition and removal, etc etc. An array is great for cases when you must constrain the length of the collection, but when you don't, it's a needless hindrance. – KeithS Jun 13 '11 at 16:55
  • 1
    @Emily - The IList is better than an array because it gives you similar functionality, but places fewer constraints on the actual type you use. Arrays are concrete implementations. It is preferable to use interfaces whenever possible, so that your code is not tied to any specific implementation. – David V Jun 13 '11 at 16:59
0

I'd use the IEnumerable version you say on b. It allows more control using generics and is also good for serialization.

Francis Gilbert
  • 3,382
  • 2
  • 22
  • 27
  • Yeah but another person here said not to use IEnumerable only if I don't need to index it and this is a requirement. – Emily Jun 13 '11 at 17:05
0

Generics: public IEnumerable<TextFiller> TextFiller { get; set; }

give you a friendlier / more type safe interface for adding, removing, searching, and so on. As long as your properties (Text, Details) remain public then they'll be serializable.

You can access an individual TextFiller in the list by doing either:

var thisItem = MyList[2];

or

var thisItem = MyList.Find(AddressOf anotherObject);

Here's another post which covers some benefits of Generics over Arrays.

Generics vs. Array Lists


    var Parent = new TextFillerParent();
    Parent.TextFillers.Add(new TextFiller { ..});
    Parent.TextFillers.Add(new TextFiller { ..});

    if(Parent.TextFillers[1] != null)
    {
        var child = Parent.TextFillers[1];
    }
Community
  • 1
  • 1
christofr
  • 2,680
  • 1
  • 18
  • 19
  • If you have a minute could you show me how I could create a TextFillerParent and then add two objects of TextFiller to it. Then how I could access the second of the TextFiller objects. – Emily Jun 13 '11 at 16:56
  • Sorry but I am getting confused here as another person said " would declare it as an IEnumerable, IF you don't need to be able to index it. If you do, try using IList " Are you saying not to use IList? – Emily Jun 13 '11 at 17:08
0

I would declare it as an IEnumerable, IF you don't need to be able to index it. If you do, try using IList (which inherits from the generic IEnumerable).

The reason why is exactly as Will said; an array must always be an array. You can index and enumerate through it, but you cannot add to, remove from, etc without rolling your own extendo-Array code.

By contrast, using IEnumerable allows you to use anything implementing that interface, which widens the candidate field considerably; all you care about is that whatever's referenced can be enumerated, on top of whatever else the actual enumerable type can do.

KeithS
  • 70,210
  • 21
  • 112
  • 164
  • What would be the syntax for me to use ILIST. It's VERY important that I can index it. I would also like to be able to add to the collection. – Emily Jun 13 '11 at 16:58
  • If it's an ILIST can I serialize it like with an array? – Emily Jun 13 '11 at 16:59