示例#1
0
        static void Main(string[] args)
        {
            Console.WriteLine($"Main Thread starting with Id {Thread.CurrentThread.ManagedThreadId}");
            // ArrayTest arrayTest = new ArrayTest();
            // arrayTest.printArray();

            // TypesDemo typesDemo = new TypesDemo();

            // typesDemo.Typetest();

            // OldProps old = new OldProps();
            // old.Name = "Anand";
            // Console.WriteLine(old.Name);

            // NewProps newProps = new NewProps();
            // newProps.Name = "Sardar";
            // Console.WriteLine(newProps.Name);

            // ArrayList names = new ArrayList();
            // names.Add("manish");
            // ExpBodiedMember expBodied = new ExpBodiedMember();
            // expBodied.Names = names;

            // Console.WriteLine(expBodied.Count);
            //  var name  = "Anand";
            // var desg = "microsoft";

            // Console.WriteLine("My name is " + name + " and i work for " + desg);

            // Console.WriteLine($"my name is {name}");

            // AutoImplementedProps.ListProducts();
            // Product product = new Product {Name = "comp", Price = 100}; // This call calls the default constructor for Product class and then calls setters for props
            // Product product = new Product(name: "comp", Price: 100);
            // Console.Write(product.Name);

            // ProductSorting productSorting = new ProductSorting();
            // productSorting.sort();
            // productSorting.sortWithDelegate();
            // productSorting.SortByLambda();
            // productSorting.PrintWithoutSort();
            // productSorting.filter();
            // LinqStart linqStart = new LinqStart();
            // linqStart.filter();
            // linqStart.filterWithJoin();

            // Delegate

            DelDemo delDemo = new DelDemo();
            // Printer pr = new Printer(delDemo.ConsolePrinter);
            // Printer pr1 = delegate(string x) {
            //     Console.WriteLine(x);
            // };

            //  delDemo.print(pr);
            // Implicit conversion from method group to delegate
            // delDemo.print(delDemo.ConsolePrinter);
            // passig delegate as lambda
            // delDemo.print((s) => Console.WriteLine("Delegate through lambda"));
            // passing Delegate as a anonymous method
            // delDemo.print(delegate(string x) {
            //     Console.WriteLine(x);
            // });

            // Program.DemostrateType<int>();

            // int ret;
            // int.TryParse("1", out ret);
            // Console.WriteLine(ret);

            List <int> List = new List <int>();

            // ThreadDemo threadDemo = new ThreadDemo();
            // threadDemo.LegacyWay();
            // threadDemo.AsyncWay();

            // BeginEnd beginEnd = new BeginEnd();
            // beginEnd.doWorkDelegateAsyncTest();

            // AsyncIO asyncIO = new AsyncIO("http://google.com");
            // asyncIO.AsyncDownload();

            // ThreadPoolDemo threadPoolDemo = new ThreadPoolDemo();
            // ManualResetEvent manualSignal = new ManualResetEvent(false);
            // threadPoolDemo.ManualSignal = manualSignal;
            // threadPoolDemo.DoWorkInPool();
            // manualSignal.WaitOne();

            // TaskDemo taskDemo = new TaskDemo();
            // taskDemo.DoWorkInSimpleTask();

            // AsyncIOWithTask asyncIOWithTask = new AsyncIOWithTask {Url = "http://google.com"};
            // asyncIOWithTask.Download();
            // asyncIOWithTask.DoInParallel();

            // AsyncAwait asyncAwaitLongOne = new AsyncAwait {Url = "https://go.microsoft.com/fwlink/p/?linkid=845299"};
            AsyncAwait asyncAwaitShortOne = new AsyncAwait {
                Url = "https://google.com"
            };
            Task <int> asyncResult = asyncAwaitShortOne.download();

            asyncResult.Wait();

            Console.WriteLine($"Main Thread End with Id {Thread.CurrentThread.ManagedThreadId}");
        }
示例#2
0
        static void Main(string[] args)
        {
            //匿名类  C#3.0的特性
            //var annoyCla1 = new
            //{
            //    ID = 10010,
            //    Name = "EdisonChou",
            //    Age = 25
            //};

            //Console.WriteLine("ID:{0}-Name:{1}-Age:{2}", annoyCla1.ID, annoyCla1.Name, annoyCla1.Age);
            //Console.WriteLine(annoyCla1.ToString());
            //Console.ReadKey();


            //普通委托未简写的使用方法 : 定义一个testFunc方法并作为参数传入委托中
            void TestFunc(string name)
            {
                Console.WriteLine("Hello: " + name);
            }

            DelegateTest dgTest1 = new DelegateTest(TestFunc);

            dgTest1("hejie");
            //由上面的步凑可以看出,我们要声明一个委托实例要为其编写一个符合规定的命名方法。
            //但是,如果程序中这个方法只被这个委托使用的话,总会感觉代码结构有点浪费

            //匿名方法 C#2.0的特性  使用匿名方法声明委托,就会使代码结构变得简洁,也会省去实例化的一些开销。
            //DelegateTest delegateTest2 = new DelegateTest(delegate (string name)
            //{
            //    Console.WriteLine("God," + name);
            //});
            //简写
            //DelegateTest delegateTest2 = delegate (string name)
            //{
            //    Console.WriteLine("God," + name);
            //};
            //最终可简写为Lambda表达式
            DelegateTest delegateTest2 = (string name) => Console.WriteLine("God," + name);

            delegateTest2("hejie");

            //另一个例子:
            DelDemo delDemo = new DelDemo(delegate(int a, int b)
            {
                return(a > b);
            });

            Console.WriteLine(delDemo(3, 5));  //输出结果为false
            //简写为Lambda表达式
            DelDemo delDemo1 = (a, b) => a > b;

            Console.WriteLine(delDemo(6, 5));

            //泛型委托

            DelegateT <int> delegateT = (x, y) => x + y;
            int             t         = delegateT(3, 5);

            Console.WriteLine(t);

            //内置委托
            Action action = () => Console.WriteLine("无参无返回值的委托");

            action();
            //通过action的泛型
            Action <string, string> action2 = (a, b) => Console.WriteLine(a + b);

            action2("有参数", "无返回值的委托");
            //有返回值的内置委托
            Func <int, int, string> func = (x1, x2) => (x1 + x2).ToString();
            string ss = func(6, 7);

            Console.WriteLine(ss);
            Console.ReadKey();
        }