mabue88
Goto Top

Eigener Datentyp in C-Sharp

Guten Morgen,

in C# benötige ich einen eigenen Datentyp.
Bislang habe ich hierfür einen Klasse mit entsprechende Methoden & Eigenschaften erstellt.

Hier ein kleines Beispiel von einem numerischen Datentyp, dessen Wertebereich von 0 bis 1000 reichen soll:
public class MyDatatype
{
    public const UInt16 MinValue = 0;
    public const UInt16 MaxValue = 1000;

    private UInt16 _value = 0;
    public UInt16 Value
    {
        get { return _value; }
        set
        {
            if (value > MaxValue)
            {
                _value = MaxValue;
            }
            else
            {
                _value = value;
            }
        }
    }

    public MyDatatype()
    {
    }
    public MyDatatype(UInt16 Value)
    {
        this.Value = Value;
    }
}

Verwendung:
MyDatatype my_value = new MyDatatype();
my_value.Value = 100;

Grundsätzlich funktioniert das natürlich.
Ich fände ich es allerdings schöner, wenn ich die Instanz "my_value" verwenden könnte wie z.B. eine Byte-Variable:
Byte my_byte_value = new Byte();
my_byte_value = 5;
nämlich ohne den "Umweg" über die Eigenschaft "Value" wie bei "MyDatatype".

Geht es überhaupt?
Falls ja, unter welchem Stichwort finde ich hierbei weitere Infos?

Vielen Dank
Gruß
mabue

Content-Key: 523701

Url: https://administrator.de/contentid/523701

Printed on: April 18, 2024 at 04:04 o'clock

Member: godlie
godlie Dec 10, 2019 at 07:23:56 (UTC)
Goto Top
Hallo,

hier ein kleines Beispiel, obwohl man es eig. schon so über eine eigene Klasse lösen sollte.

public class MyCustomInt64 : CustomValueType<MyCustomInt64, Int64>
{
    private MyCustomInt64(long value) : base(value) {}        
    public static implicit operator MyCustomInt64(long value) { return new MyCustomInt64(value); }
    public static implicit operator long(MyCustomInt64 custom) { return custom._value; }
}

public class CustomValueType<TCustom, TValue>
{        
    protected readonly TValue _value;

    public CustomValueType(TValue value)
    {
        _value = value;
    }

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

    public static bool operator <(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return Comparer<TValue>.Default.Compare(a._value, b._value) < 0;
    }

    public static bool operator >(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return !(a < b);
    }

    public static bool operator <=(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return (a < b) || (a == b);
    }

    public static bool operator >=(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return (a > b) || (a == b);
    }

    public static bool operator ==(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return a.Equals((object)b);
    }

    public static bool operator !=(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return !(a == b);
    }

    public static TCustom operator +(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return (dynamic) a._value + b._value;
    }

    public static TCustom operator -(CustomValueType<TCustom, TValue> a, CustomValueType<TCustom, TValue> b)
    {
        return ((dynamic) a._value - b._value);
    }

    protected bool Equals(CustomValueType<TCustom, TValue> other)
    {            
        return EqualityComparer<TValue>.Default.Equals(_value, other._value);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((CustomValueType<TCustom, TValue>)obj);
    }

    public override int GetHashCode()
    {
        return EqualityComparer<TValue>.Default.GetHashCode(_value);
    }
}

MyCustomInt64 myInt = 27; //use as like any other value type

im bereich des base(value) {} Constructors kannst du dann deine Eingrenzung bzgl. der Größe vornhemen
Member: mabue88
mabue88 Dec 10, 2019 at 18:10:11 (UTC)
Goto Top
Super, Danke!
Probiere ich morgen gleich mal aus!