It comes in handy of course that you can dynamically adjust the size of a List or Dictionary.
But this time I want to use a Dictionary with sizes from 1 to 8.
When I have e.g. size 3, it will stay that way.
On the net I found a solution to use a wrapper class around a Dictionary, which could do the job.
Should I resort to an array instead, or are there still other options?

Dani AI

Generated

Short summary tied to the thread: wants a collection fixed to 1–8 ports for gate simulation, and correctly notes that for a few objects memory rarely matters. For clarity, correctness and simplest code, an array-backed fixed-size wrapper is the best balance—minimal overhead, O(1) access, and explicit port-range checks. Composition (an internal array) is preferable to inheriting or forcing Dictionary behavior.

A minimal example that enforces size and port-range (1-based ports shown) and keeps the API small and testable:

public class FixedPortCollection<T>
{
    readonly T[] items;
    public int Size { get; }

    public FixedPortCollection(int size)
    {
        if (size < 1 || size > 8) throw new ArgumentOutOfRangeException(nameof(size));
        Size = size;
        items = new T[size];
    }

    public T this[int port]
    {
        get => items[Validate(port)];
        set => items[Validate(port)] = value;
    }

    int Validate(int port)
    {
        if (port < 1 || port > Size) throw new ArgumentOutOfRangeException(nameof(port));
        return port - 1;
    }
}

If the signal type is truly binary (High/Low) and there will be many gate instances, consider packing ports into a single byte per gate (bitmask) to save memory. That is more compact but less flexible and slightly harder to read and test.

Tradeoffs and practical notes: use the simple array wrapper unless profiling shows a problem. Add unit tests for port validation and default values. Make the class immutable in size (no Resize), expose IReadOnlyList or a small indexer API for clarity, and add locking or copy-on-write if gates are accessed concurrently. For simulation code clarity usually wins; optimize to bit-packing only when there is measured need.

Recommended Answers

All 4 Replies

Why exactly do you need this? Unless you're writing a library (in which case a wrapper is the appropriate solution), it's more a matter of careful coding and checking invariants where necessary.

I would like to simulate some digital gates like NAND-gate, OR-gate etc.
For the input ports I thought a Dictionary might be handy. With as key portnr and as value a signal enumeration, indicating the signal level. I know that for optimalisation reasons a collection is bigger than it needs to be, but for sizes of 1 to 8 input ports that is of no importance. Hence my question. Please correct me if you see other solutions.

Honestly, I wouldn't worry about it unless you're under some harsh memory constraints.

Thanks. I come from a past where every byte counted. I have to debrief on that I guess. :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.