C#: implicit operator and extension methods

前端 未结 3 2070
暗喜
暗喜 2021-01-11 11:39

I am trying to create a PredicateBuilder class which wraps an Expression> and provides some methods to easily bu

3条回答
  •  广开言路
    2021-01-11 12:05

    This is not specific to extension methods. C# won't implicitly cast an object to another type unless there is a clue about the target type. Assume the following:

    class A {
        public static implicit operator B(A obj) { ... }
        public static implicit operator C(A obj) { ... }
    }
    
    class B {
        public void Foo() { ... }
    }
    
    class C {
        public void Foo() { ... }
    }
    

    Which method would you expect to be called in the following statement?

    new A().Foo(); // B.Foo? C.Foo? 
    

提交回复
热议问题