c# - Design a class, what's wrong if using IList as type of property? -


i have simple class below, has property pages of type ilist.

there options implement property, can array or collection / list / readonlycollection

public class book     {         private string[] _pages;          public book(string[] pages)         {             _pages = pages;         }          public ilist<string> pages         {                         {                 return _pages;                 //return new collection<string>(_pages);                 //return new list<string>(_pages);                 //return new readonlycollection<string>(_pages);             }         }     } 

at design time, not know actions clients use property choosing option above affect clients.

if client uses book class below

var book = new book(new[] {"a", "b"}); var pages = book.pages; pages[0] = "a2"; 

not implementation options of property pages work client.

option 1: returning array pages // ok, works

public ilist<string> pages     {                 {             return _pages;         }     } 

option 2: returning collection pages // ko, throws exception notsupportedexception collection read-only

public ilist<string> pages     {                 {             return new collection<string>(_pages);         }     } 

option 3: returning list pages // ok, works

public ilist<string> pages     {                 {             return new list<string>(_pages);         }     } 

option 4: returning readonlycollection pages // ko, throws exception systemexception collection read-only

public ilist<string> pages     {                 {             return new readonlycollection<string>(_pages);         }     } 

i don't think wrong on client side. please give explanation , suggest type page?

another option define own collection type pagecollection example , use in book class. advantage of is, hide detail of how collection implemented. advantage of approach is, can provide "special" methods collection.

for example extend existing collection (it may advise create custom class page, instead of using plain strings:

public class pagecollection : list<page> {     // additional methods } 

you can wrap existing collection. has advantage have full controll on methods want provide "user".

public class pagecollection : ienumerable<page> {     private list<page> _innercollection = new list<page>();      public void ripout(ienumerable<page> pages)     {         foreach (var page in pages)         {             _innercollection.remove(page);         }     }      // other methods      public ienumerator<page> getenumerator()     {         return _innercollection.getenumerator();     }      ienumerator ienumerable.getenumerator()     {         return this.getenumerator();     } } 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -