// See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); // // // 协变代码 // 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()); } }