Define assigment operator that change the value of some “this” in C# [duplicate]

霸气de小男生 提交于 2019-12-08 15:22:19

问题


I want to define an operator= that try to change the inner value of the class

I'm implementing a variatic type in c# that may contain an int, bool or double and I want to overrload the = operator so I can do something like:

Variable bar = new Variable(1.2);
bar = 2.3;

I internaly keep the initialized type, so you can't change type over execution, so if I do:

Variable foo = new Variable(1.2);
foo = true;//ERROR, ilegal operation

an exception is thrown. I've tryed to do:

public static Variable operator=(Variable variable, int value)
{
    if (!variable.TrySetValue(value))
    {
        throw new Exception(string.Format("ERROR: you're trying to assing an 'int' value to an {0}",
            Enum.GetName(typeof(Variable.Type), variable.type)));
    }
    return variable;
}

The TrySetValue() method looks something like this:

public bool TrySetValue(int value)
{
    if (type == Type.INT)
    {
        this.value = value;
        return true;
    }
    return false;
}

and the same with bool and double

That gives me the error Overloadable binary operator expected.

I could define an simple public static implicit operator Variable(int i){...}, but I don't want to create a new Variable, I want to change the value of the current Variable if it's possible.
I know I could do this in C++, and I want to believe C# can do it as well.
I have not found anything like that on the internet so, please, help me.


回答1:


As @madreflection said, you can't overload =.

But if you want to create an embedded numeric value, you may implement these methods:

static public explicit operator T(Variable value)
static public explicit operator Variable(T value)

public override int GetHashCode()

public virtual object Clone()

public override bool Equals(object value)
public bool Equals(T value)
public bool Equals(Variable value)

public bool ToBoolean(IFormatProvider provider)
public byte ToByte(IFormatProvider provider)
public char ToChar(IFormatProvider provider)
public DateTime ToDateTime(IFormatProvider provider)
public decimal ToDecimal(IFormatProvider provider)
public double ToDouble(IFormatProvider provider)
public short ToInt16(IFormatProvider provider)
public int ToInt32(IFormatProvider provider)
public long ToInt64(IFormatProvider provider)
public sbyte ToSByte(IFormatProvider provider)
public float ToSingle(IFormatProvider provider)
public string ToString(IFormatProvider provider)
public object ToType(Type conversionType, IFormatProvider provider)
public ushort ToUInt16(IFormatProvider provider)
public uint ToUInt32(IFormatProvider provider)
public ulong ToUInt64(IFormatProvider provider)

public TypeCode GetTypeCode()

static public bool operator ==(Variable v1, Variable v2)
static public bool operator ==(Variable v1, object v2)
static public bool operator ==(object v1, Variable v2)
static public bool operator !=(Variable v1, Variable v2)
static public bool operator !=(Variable v1, object v2)
static public bool operator !=(object v1, Variable v2)

public virtual int CompareTo(object obj)
static public bool operator <(Variable v1, Variable v2)
static public bool operator <(Variable v1, T v2)
static public bool operator <(T v1, Variable v2)
static public bool operator >(Variable v1, Variable v2)
static public bool operator >(Variable v1, T v2)
static public bool operator >(T v1, Variable v2)
static public bool operator <=(Variable v1, Variable v2)
static public bool operator <=(Variable v1, T v2)
static public bool operator <=(T v1, Variable v2)
static public bool operator >=(Variable v1, Variable v2)
static public bool operator >=(Variable v1, T v2)
static public bool operator >=(T v1, Variable v2)

static public Variable operator +(Variable v1, Variable v2)
static public Variable operator +(Variable v1, T v2)
static public Variable operator +(T v1, Variable v2)
static public Variable operator -(Variable v1, Variable v2)
static public Variable operator -(Variable v1, T v2)
static public Variable operator -(T v1, Variable v2)
static public Variable operator *(Variable v1, Variable v2)
static public Variable operator *(Variable v1, T v2)
static public Variable operator *(T v1, Variable v2)
static public Variable operator /(Variable v1, Variable v2)
static public Variable operator /(Variable v1, T v2)
static public Variable operator /(T v1, Variable v2)

I hope I have not forgotten some...

You may want to create a generic for not having to create each for int, bool and double:

Variable<T> where T : IComparable


来源:https://stackoverflow.com/questions/58189427/define-assigment-operator-that-change-the-value-of-some-this-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!