Cannot assign a delegate of one type to another even though signature matches

前端 未结 2 741
鱼传尺愫
鱼传尺愫 2020-12-29 12:23

My morbid curiosity has me wondering why the following fails:

// declared somewhere
public delegate int BinaryOperation(int a, int b);

// ... in a method bo         


        
2条回答
  •  离开以前
    2020-12-29 12:50

    Here's a similar question: why doesn't this compile?

    // declared somewhere
    struct Foo {
        public int x;
        public int y;
    }
    
    struct Bar {
        public int x;
        public int y;
    }
    
    // ... in a method body
    Foo item = new Foo { x = 1, y = 2 };
    
    Bar b1 = item; // doesn't compile, and casting doesn't compile
    Bar b2 = new Bar { x = 1, y = 2 }; // compiles!
    

    In this case it seems a little more natural for the cast to not work, but it's really the same reason.

提交回复
热议问题