Is it possible define an extension operator method?

前端 未结 10 731
迷失自我
迷失自我 2021-01-04 02:09

is it possible to define an extension method that at the same time is an operator? I want for a fixed class add the possibility to use a known operator that actually can\'t

相关标签:
10条回答
  • 2021-01-04 02:38

    I would argue that you should use a wrapper class, even if you could write an extension operator.

    //i really know that this string contains a numeric value
    

    is exactly the sort of situation that type-safety was invented for.

    Another way of looking at it is that by writing that operator, you have broken many other functions and operators that work with the string class, since they don't necessarily preserve the property of containing a numeric value. By using a wrapper class, not a derived class, you only re-implement those features of string that make sense for numeric strings.

    0 讨论(0)
  • 2021-01-04 02:38

    i was in a very similar situation as you described: i needed to increase the text (containing a numeric value for sure) in a Windows Forms textbox.

    I understand your need as you described

    somestring++; //i really know that this string contains a numeric value

    My soultion is something like that which i believe is close to your description

    somestring = (incrementable)somestring + 1

    All i needed to do was

    1. creating class called incrementable
    2. defining an explicit operator in it (to aid converting string to incrementable )
    3. defining an implicit operator in it (to aid converting incrementable back to string )
    4. operator for + (plus sign)

    Here's how my class looks in complete

    public class incrementable
    {
        public string s; // For storing string value that holds the number
    
        public incrementable(string _s)
        {
            s = _s;
        }
    
        public static explicit operator incrementable(string tmp)
        {
            return new incrementable(tmp);
        }
    
        public static implicit operator string(incrementable tmp)
        {
            return tmp.s;
        }
    
        public static incrementable operator +(incrementable str, int inc) // This will work flawlessly like `somestring = (incrementable)somestring + 1`
            => new incrementable((Convert.ToInt32(str.s) + inc).ToString());
    
        public static incrementable operator ++(incrementable str) // Unfortunately won't work, see below
            => new incrementable((Convert.ToInt32(str.s) + 1).ToString());
    }
    

    Unfortunately i just couldn't get managed to improve my class by the usage of unary ++ operator. The reason against of usage of implicit conversion like ((incrementable)somestring)++ is that it is going to result in error saying The operand of an increment or decrement operator must be a variable, property or indexer hence can not be result of that casting.

    Anyway, hope this helps!

    0 讨论(0)
  • 2021-01-04 02:41

    This is all true, but it would be nice for M$ to add this functionality in the future. Sometimes the framework is just missing things and an extension can help plug the gap (or fix the issue) this can sometimes be operators.

    An example. To compare IP Addresses, you must use the Equals method to directly compare (of course parts of the struct could also be compared as could the address bytes individually - but that's another story). However, using the == operator always returns false at the object level (i.e. without converting them to strings etc). How hard is it to put the Equals method call inside the == operator call (that's rhetorical), but we can't do it. This is inconsistant and a place for bugs to creep in (note it does not fail, just always equates to false - whereas Equals does not).

    0 讨论(0)
  • 2021-01-04 02:50

    No, it is not possible to do from outside of the class. ++ operator should be defined inside class which is being incremented. You can either create your own class which will be convertible from string and will have ++ overload or you can forget about this idea and use regular methods.

    0 讨论(0)
提交回复
热议问题