// See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); Console.WriteLine(Test.Double(10/2)); // Console.WriteLine(Test.Double("10")); // Console.WriteLine(Test.Double(new object())); class Test{ public static dynamic Double(dynamic d){ if (d is int){ return d*2; }else if(d is string){ return d+d; }else{ throw new Exception("Not support"); } } } // 编译错误 // object n = 10; // int result = n + 5; // Console.WriteLine("result:{0}", result); // 编译时不检查,运行时才检查 // dynamic n = "10"; // int result = n + 5; // Console.WriteLine("result:{0}", result); // // // 协变代码 // ISearch
search = new CodeArticleSearch(); // Article article = search.Search(); // Console.WriteLine(article); // // 逆变代码 // ISend sender = new ArticleSender
(); // sender.Send(new CodeArticle()); // Test.Find("hello"); // Test.Find("hello", 10); // Test.Find("hello", 10, true); // Test.Find("hello", true, 10); // Test.Find(key:"hello", number:5); // Test.Find(key:"hello", sort:false, number:5); // Test.Find(sort:false, number:5, key:"hello"); // static class Test{ // public static List Find(string key, int number, bool sort){ // Console.WriteLine("key:{0}, number:{1}, sort:{2}", key, number, sort); // return new List(){}; // } // } // // 父类和子类 // class Article{ // public string ToString(){ // return "article"; // } // } // class CodeArticle: Article{ // public string ToString(){ // return "article with code"; // } // } // // 协变 // public interface ISearch { // T Search(); // } // public class CodeArticleSearch: ISearch where T:new(){ // public T Search(){ // return new T(); // } // } // // 逆变 // public interface ISend { // void Send(T t); // } // public class ArticleSender : ISend{ // public void Send(T t){ // Console.WriteLine("Send:{0}", t.ToString()); // } // }