FP Homework 3

From Marek Běhálek Wiki
Jump to navigation Jump to search

Immutable data types

Immutable Array

private abstract class Triplet<A> : ITriplet
        {
            private A[] data = new A[3];
            public Triplet(A left, A middle, A right)
            {
                data[0] = left;
                data[1] = middle;
                data[2] = right;
            }
            public A this[int index]
            {
                get => index switch
                {
                    >= 0 and <= 2 => this.data[index],
                    _ => throw new IndexOutOfRangeException($"Index {index} out of range for a triplet.")
                };
            }
        }