Program.cs 1.5 KB
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

// // // 协变代码
// ISearch<Article> search = new CodeArticleSearch<CodeArticle>();
// Article article = search.Search();
// Console.WriteLine(article);

// // 逆变代码
// ISend<CodeArticle> sender = new ArticleSender<Article>();
// 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<string> Find(string key, int number, bool sort){
        Console.WriteLine("key:{0}, number:{1}, sort:{2}", key, number, sort);
        return new List<string>(){};
    }
}


// 父类和子类
class Article{
    public string ToString(){
        return "article";
    }
}

class CodeArticle: Article{
    public string ToString(){
        return "article with code";
    }
}


// 协变
public interface ISearch<out T> {    
    T Search();
}

public class CodeArticleSearch<T>: ISearch<T> where T:new(){
    public T Search(){
        return new T();
    }
}

// 逆变
public interface ISend<in T> {    
    void Send(T t);
}

public class ArticleSender<T> : ISend<T>{
    public void Send(T t){
        Console.WriteLine("Send:{0}", t.ToString());
    }
}