C# 基础

一、多态(virtual,override,new,abstract 关键字)
  抽象类:abstract必须子类重写,并且不能申明主体
       virtual 可以不重写,编译器会警告,必须申明主体
   实现类:override 重写,对应父类的
abstract 和 virtual
       new 对应父类的
virtual
  override sealed 不允许子类 重写
 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Person person1 = new Chiness("吉米");
 6             Person person2 = new Japanese("山本");
 7             person1.SayHello();
 8             person2.SayHello();
 9         }
10     }
11     public class Person
12     {
13         public string Name { set; get; }
14         public Person(string name)
15         {
16             this.Name = name;
17         }
18         public virtual void SayHello()
19         {
20             Console.WriteLine("I'm Person,my name is " + this.Name);
21         }
22     }
23     public class Chiness : Person
24     {
25         public Chiness(string name)
26             : base(name)
27         {
28 
29         }
30         public override void SayHello()
31         {
32             Console.WriteLine("I'm Chiness,my name is " + this.Name);
33         }
34     }
35     public class Japanese : Person
36     {
37         public Japanese(string name)
38             : base(name)
39         {
40 
41         }
42         public new void SayHello()
43         {
44             Console.WriteLine("I'm Japanese,my name is " + this.Name);
45         }
46     }
View Code
   
  


二、装箱和拆箱
 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             ArrayList listArr = new ArrayList();
 6             List<object> list = new List<object>();
 7             Stopwatch sw = new Stopwatch();
 8             sw.Start();
 9             int k = 10;
10             for (int i = 0; i < 10000000; i++)
11             {
12                 object kk = k;
13                 list.Add(kk);
14             }
15             //装箱:00:00:00.9023259
16             //拆箱:00:00:00.0885700
17             sw.Stop();
18             Console.WriteLine(sw.Elapsed);
19             Console.ReadKey();
20         }
21     }
View Code

  同样的操作装箱需时间:00:00:00.9023259,拆箱只需00:00:00.0885700


三、委托
using System;

namespace ConsoleApplication1
{
    class Program
    {
        public delegate int dele(string x);
        static void Main(string[] args)
        {
            Action<string> action = str => { Console.WriteLine(str + "1"); Console.WriteLine(str + "2"); };
            action("123");//1231,1232
            Func<string, int> func1 = x => A(x);
            int i = func1("123456");
            Console.WriteLine(i);//16
            Func<string, string, string, string> func2 = (x, y, z) => x + y + z;
            Console.WriteLine(func2("1","2","3"));//123

        }
        public static int A(string a)
        {
            return a.Length + 10;
        }
    }
}
View Code

四、Linq 乱 ... 仔细看!!!
using System;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<A> list = new List<A>() 
            {
                new A(){Id=1,Name="AA"},
                new A(){Id=6,Name="WW"},
                new A(){Id=4,Name="TT"},
                new A(){Id=7,Name="SS"},
                new A(){Id=5,Name="RR"},
                new A(){Id=2,Name="QQ"},
                new A(){Id=3,Name="GG"},
            };
            //执行
            Action<List<A>> action = x =>
            {
                x.ForEach(item => Console.WriteLine("{0},{1},{2}", item.Id, item.Name, item.Age));
            };
            Action<A> actionSingle = x => Console.WriteLine("{0},{1},{2}", x.Id, x.Name, x.Age);
            var list1 = list;
            //筛选
            var l = list1.Where(n => n.Id > 3);
            //获取,排序 OrderByDescending 降序,OrderBy 升序
            list1 = l.Select(n => n).OrderByDescending(x => x.Id).ToList();
            //action(list1);

            var list2 = list;
            //所有元素都满足返回 true 否则返回 false
            bool b = list2.All(n => n.Age == 1);
            //Console.WriteLine(b);
            //任何一元素满足返回 true 否则返回 false
            b = list2.Any(n => n.Id > 1);
            //获取平均值
            double average = list1.Average(n => n.Id);
            //Console.WriteLine(average);
            list = list.Concat(list).ToList();
            //action(list);
            A a = new A() { Id = 1, Name = "AAA" };
            list.Add(a);
            b = list.Contains(a);
            //Console.WriteLine(b);//true
            int i = list.Count(x => x.Name.Length > 2);
            //Console.WriteLine(i);//1
            //返回 System.Collections.Generic.IEnumerable<T> 类型
            var listAble = list.DefaultIfEmpty();
            list.Add(a);
            //action(list);
            list = list.Distinct().ToList();
            //action(list);

            //First 获取第一个满足条件的数据,不满足条件的报异常
            var listOne = list.First<A>(n => n.Id > 1);
            //FirstOrDefault 不满足条件的报异常的情况 返回null 值
            listOne = list.FirstOrDefault<A>(n => n.Id > 12);
            //actionSingle(listOne);
            // Last 获取最一个满足条件的数据,不满足条件的报异常
            var listLast = list.First<A>(n => n.Id > 12);
            //FirstOrDefault 不满足条件的报异常的情况 返回null 值
            listOne = list.FirstOrDefault<A>(n => n.Id > 12);

            // Union 对两个集合采取“并”操作
            list = list.Union(list).ToList();
            list = list.Union(new List<A>() { new A() { Id = 12, Name = "123465", Age = 122 } }).ToList();
            //action(list);
            //Intersect  对两个集合采取“交”操作
            list = list.Intersect(new List<A>() { new A() { Id = 12, Name = "123465", Age = 122 } }).ToList();
            //action(list);
        }
    }
    public class A
    {
        public int Id { set; get; }
        public string Name { set; get; }
        public int Age { set; get; }
    }
}
View Code





原文地址:https://www.cnblogs.com/Jacob-Wu/p/6022073.html