Program.cs 2.2 KB
Newer Older
F
feilong 已提交
1 2 3
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

F
feilong 已提交
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


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);

F
feilong 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
// // // 协变代码
// 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);
F
feilong 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
// 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());
//     }
// }