Tech Off Post

Single Post Permalink

View Thread: How about some syntax sugar for IEnumerable<>?
  • User profile image
    evildictait​or

    stevo_ said:
    Tell me about it, I've wanted this for ages - but I'd also like some immutable structures as well, perhaps the f# ones will be available in .net 4, even if I have to reference an f# assembly thats gac'd.
    IEnumerable<T> has no direct methods for mutating a collection, so unless you use reflection or do an upwards cast then an IEnumerable<T> is immutable.

    If you want to be really sure, then do this:

    class {
      List<foo> myMutableCollection = new List<foo>();

      public IEnumerable<foo> EnumerableFoo {
        get {
           return new ReadOnlyCollection(myMutableCollection);
        }
      } 
    }

    and now the mutablecollection can only be altered by the class or by reflection (and there's not much you can do to prevent reflection doing bad things)