using extension methods on int

后端 未结 7 1283
难免孤独
难免孤独 2021-02-19 04:10

I\'m reading about extension methods, and monkeying around with them to see how they work, and I tried this:

namespace clunk {
    public static class oog {
             


        
相关标签:
7条回答
  • 2021-02-19 04:32

    Unfortunately you cannot introduce new operators, or implement support for existing operators, on types, through extension methods.

    Basically, what you want to do cannot be done.

    The only way to introduce new operators is to put them in one of the involved types. For binary operators you can put them in your own type, provided your type is one of the two, for unary types you need to put the operator inside the type itself.

    Since you can't extend an int in any way, you can't do it.

    0 讨论(0)
  • 2021-02-19 04:35

    Unfortunately you cannot use extension methods to add operators, and implicit type conversion in C# is implemented as an operator.

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

    Extension methods are exactly that—methods.
    You cannot make extension operators or properties.

    Had that been possible, it would result in very hard-to-read code.
    If you aren't familiar with the code base, it's almost impossible to figure out what if (7) means.

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

    Console.WriteLine(5.doubleMe());

    is equivalent to

    Console.WriteLine(oog.doubleMe(5));

    Given that, you can see why if ( 7 ) doesn't work.

    Extension methods are nothing more than syntax.

    As a side note since it is syntax you can call extension methods on null variables because it translates to a normal method call, and methods can take null parameters.

    0 讨论(0)
  • 2021-02-19 04:42

    In the first instance you are writing an extension method - e.g. extending the functionality of the int data type. In the second code set, you are trying to override the bool operators. These are two totally different things.

    0 讨论(0)
  • 2021-02-19 04:54

    As others have said, there's no such thing as extension operators in C#.

    The closest you can get, running the risk of facilitating lots of nasty bugs down the line, would be with implicit conversion operators on a custom "bridge" type:

    // this works
    BoolLikeC evil = 7;
    if (evil) Console.WriteLine("7 is so true");
    
    // and this works too
    if ((BoolLikeC)7) Console.WriteLine("7 is so true");
    
    // but this still won't work, thankfully
    if (7) Console.WriteLine("7 is so true");
    
    // and neither will this
    if ((bool)7) Console.WriteLine("7 is so true");
    
    // ...
    
    public struct BoolLikeC
    {
        private readonly int _value;
        public int Value { get { return _value; } }
    
        public BoolLikeC(int value)
        {
            _value = value;
        }
    
        public static implicit operator bool(BoolLikeC x)
        {
            return (x.Value != 0);
        }
    
        public static implicit operator BoolLikeC(int x)
        {
            return new BoolLikeC(x);
        }
    }
    
    0 讨论(0)
提交回复
热议问题