Global type aliases in C#

4k views Asked by At

let me start right away with the code:

class Item {
    public int highestBuyOffer;
    public int lowestSellOffer;
    [...]
}

I would like to prevent people using this class from accidently assigning a buy offer value to a sell offer value and the other way round (like someBuyOffer = someSellOffer). That's why I want to create my own types:

class Item {
    public BuyOffer highestBuyOffer;
    public SellOffer lowestSellOffer;
    [...]
}

Creating a struct for it seems overkill, as these both of values should behave exactly like an int.

The using directive is not what I want because:

  1. It is only valid for one file
  2. It does not count as a type, it's just a synonym
1

There are 1 answers

1
BernardV On

I made this class to cover identical needs:

public class NamedInt : IComparable<int>, IEquatable<int>
{
    internal int Value { get; }

    protected NamedInt() { }
    protected NamedInt(int val) { Value = val; }
    protected NamedInt(string val) { Value = Convert.ToInt32(val); }

    public static implicit operator int (NamedInt val) { return val.Value; }

    public static bool operator ==(NamedInt a, int b) { return a?.Value == b; }
    public static bool operator ==(NamedInt a, NamedInt b) { return a?.Value == b?.Value; }
    public static bool operator !=(NamedInt a, int b) { return !(a==b); }
    public static bool operator !=(NamedInt a, NamedInt b) { return !(a==b); }

    public bool Equals(int other) { return Equals(new NamedInt(other)); }
    public override bool Equals(object other) {
        if ((other.GetType() != GetType() && other.GetType() != typeof(string))) return false;
        return Equals(new NamedInt(other.ToString()));
    }
    private bool Equals(NamedInt other) {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Equals(Value, other.Value);
    }

    public int CompareTo(int other) { return Value - other; }
    public int CompareTo(NamedInt other) { return Value - other.Value; }

    public override int GetHashCode() { return Value.GetHashCode(); }

    public override string ToString() { return Value.ToString(); }
}

And to consume it in your case:

public class BuyOffer: NamedInt {
    public BuyOffer(int value) : base(value) { }
    public static implicit operator BuyOffer(int value) { return new BuyOffer(value); }
}

public class SellOffer: NamedInt {
    public SellOffer(int value) : base(value) { }
    public static implicit operator SellOffer(int value) { return new SellOffer(value); }
}

If you need to be able to serialize it (Newtonsoft.Json), let me know and I'll add the code.