.net – Implementation of a fixed collection in C #

I want to define some constant sequences (especially bytes) in my C # library / API. Both length and content should not vary, preferably by no means (without reflection). For this purpose a byte[] Array would not be enough and a ReadOnlyCollection can still be mutated. I have not made a full comparison yet, but this effort could be reinvented ImmutableListIn that case, I would still like to receive feedback on the successes and weaknesses of this implementation. To be specific, this class should be inherited from several derived classes that actually define the constants.

Design & Style

  • I have chosen this class on the most primitive interface (IEnumerableI could do that partly for maximum compatibility, and partly to avoid hiding or throwing NotSupportedException most certainly ICollection Methods and the like. Does that have any disadvantages?

  • The actual collection that contains the items is a private list,

  • I have implemented a few comfort fields, AsString, number, and length (length feels more like the guy, but the existence of the Counting <> LINQ extension made me feel that both should be included for consistency. I considered receive-Only properties, however, this question and answer to SO points out read-only is a more explicit modifier. Are there other advantages / disadvantages that I miss in this particular case?

  • Since I only want to derive this class, I could mark it abstractbut I could build it as well interface? (This would affect the read-only/receive-nly decision.) Stylistically, however, it seems that derived classes should have an "actual relationship" bytes instead of a "can".

  • I have omitted all namespace qualifiers here, as seems to be the standard in C #. However, I mostly come from Python, and global imports and pollution of the global namespace are almost unbearable to me. I do not want to see either System.Collections.Generic.[...] all over; Is it an acceptable compromise, for example, to use using CollectionsGen = System.Collections.GenericOr would that be appalling if C # programmers read my code?

code

/// 
/// implementation of a fixed collection of bytes with some extras.
/// 
public class bytes: IEnumerable
{
private list bytes list;
public readonly string AsString;
public read only int Count;
public readonly int length; // Only a synonym for Count

// accepts values ​​from -Length to Length-1 (similar to Python indexing).
public byte this[int i] {get {return this.bytesList[ i>=0 ? i : i+this.Length ]; }}

public bytes (IEnumerable bytes)
{
this.bytesList = new list(Bytes);
this.AsString = string.Join ("", this.Select (b => $ "0x {b: X2}"));
this.Count = this.bytesList.Count;
this.Length = this.Count;
}

public IEnumerator GetEnumerator () {return this.bytesList.GetEnumerator (); }

IEnumerator IEnumerable.GetEnumerator () {return this.GetEnumerator (); }

I think I could generalize this by definition to each type public class MyImmutableCollection : IEnumerable with about the same body, but I do not need that now.

Usage

var byteArray = new byte[] {0x01, 0x02, 0x03};
var bytesInst = new bytes (byteArray);
byteArray[1] = 0xFF;
// Note that byteArray has changed, whereas bytesInst does not.
bytesInst[1] = 0xFF; // correctly generates a read-only compiler error