深入理解 c# 第五章 c#1到c#2 的改变

    class BreakingChange
    {
        delegate void SampleDelegate(string x);

        public void CandidateAction(string x)  //c#1 中执行,因为object类型和 string不兼容
        {
            Console.WriteLine("Snippet.CandidateAction");
        }

        public class Derived : BreakingChange
        {
            public void CandidateAction(object o)  //c#2执行,object和string兼容, Main里面声明的是 Derived类
            {
                Console.WriteLine("Derived.CandidateAction");
            }
        }

        static void Main()
        {
            Derived x = new Derived();  //声明一个 Derived类
            SampleDelegate factory = new SampleDelegate(x.CandidateAction);
            factory("test");  //调用 Derived类的 CandidateAction方法
        }
    }



输出 

Derived.CandidateAction

你可能感兴趣的:(c#)