none
IList IsFixedSize property RRS feed

  • Question

  • When looking at MSDN (http://msdn.microsoft.com/en-us/library/system.collections.ilist.isfixedsize.aspx), the definition for IsFixedSize is:

    "A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but it allows the modification of existing elements."

    However, the example given on the same page has:

    public bool IsFixedSize { get { return true; } }

    which appears contradictory as the methods Add/Remove/RemoveAt are functional and the property Count is variable, implying IsFixedSize should be false. Is this an error in the documentation, or where can I find a more precise definition of this property?

    Thanks,
    Jason.

    Sunday, September 26, 2010 3:58 PM

All replies

  • If you look closely at the example you'll notice that the list items are stored in this fixed-sized array:

    private object[] _contents = new object[8];
    
    

    The class in the example will not let you add/insert any more than 8 objects which is why it returns true from IsFixedSize.

    ShaneB

    Sunday, September 26, 2010 4:05 PM
  • Yes - I did see this.

    Let's take a black box view. "A collection with a fixed size does not allow the addition or removal of elements after the collection is created, but it allows the modification of existing elements."

    Clearly in this example, it does allow adding and removing elements, regardless that the internal implementation used a fixed size array. And for most components I won't see what the internal implementation is. I could equivalently define my fixed-size array as having the maximum possible elements an array can have, 8 is just an arbitrary number. Is it still Fixed Size?

    From reading MSDN, strictly the example should return "false" as the size of the array is variable from 0 to 8 elements if I'm to take the remarks as normative and the example as informative.

    If my interpretation is incorrect, what/where is a normative description of this element so i can be sure I implement this correctly?

    Jason.

    Sunday, September 26, 2010 4:35 PM
  • Excellent points.  I believe the intent of IsFixedSize is to tell you whether the IList's maximum capacity (Count) is less than 2^31 (int.MaxValue+1)...so in the example I think IsFixedSize should return true.  A better interface implementation may have been to just have an int property called MaxCapacity or MaxCount and get rid of IsFixedSize altogether.

    Anyway, I agree that the documentation wrong since most implementations of IList would allow you to add/remove items even if the size (capacity) of the list was restricted.  Of course, those methods would affect IList.Count but not necessarily the capacity of whatever array is holding the items.  Hopefully that makes sense.

    ShaneB

    • Marked as answer by SamAgain Wednesday, September 29, 2010 3:09 AM
    • Unmarked as answer by Jason Curl Wednesday, September 29, 2010 6:27 AM
    Sunday, September 26, 2010 5:43 PM
  • I agree, such a definition is effectively meaningless, but it still doesn't answer my original question completely. The remarks contradict the example and I've not seen or found an authorative non-contradictory statement explaining what the definition of IsFixedSize is. Searching through the web only quotes MSFT's remarks without reproducing the example. I looked in a ECMA standard, found the interface definition, but no explanation about the interface.

    Code examples from Mono initially had something like:

    void InsertRange(ICollection data) {
      if (IsFixedSize || IsReadOnly) {
        throw ArgumentException();
      }
      ...
    }

    That would imply that although you could insert a range (up to 8 elements), it would be artificially not allowed because of the IsFixedSize parameter, if I were to take the example.

    Anyone else found a concise definition of IsFixedSize?

    Wednesday, September 29, 2010 6:37 AM