那些年,我还在学习C#

那些年学了ASP.NET后,才开始学习C#,说来也怪,怎么学了ASP.NET才来学习C#,其实没有什么的。C#是一门面向对象的语言,具有面向对象的基本特征,抽象、封装、继承、多态等性质。学习C#除了一些基本的语法,还得学习一些新的特性,比如说:泛型、多线程、集合、反射等,下面就选其中一些来学习吧!

一、C#中的各种器

       A、 C#构造器-构造函数

       如下:

    

  //构造器1

        public Products(int id)

        {

            _Id = id;

        }

        //构造器2,使用this来调用构造器1

        public Products(int id, string Name, string Band)

            : this(id)

        {

            _ProductName = Name;

            _ProductBand = Band;

        }

静态构造器-用来对类进行初始化信息,它不是显示调用的,在首次访问类时将自动调用,用来初始化类的一些基本信息而不是对象,但最好不要使用静态的构造器,代码如下:

   

 static Products() { }//静态构造器

    public Products() { }

      B、初化器-在没有带参数的构造器时,我们可以用初始化器对对象的共公属性进行初始化

  如下:

     

 //产品的集合List

            List<Products> ListProduct = new List<Products>()

            {

                new Products() { _Id = 1, _ProductName = "whc" },//使用{}是调有用初始化器,对属性进行初始化

                new Products() { _Id = 1, _ProductName = "whc1", _ProductBand = "ctbu" },

                new Products() { _Id = 1, _ProductName = "whc2", _ProductBand = "ctbu" }

            };

    C、终结器

   终结器是在一次对象最后一次活动之后,并在程序终止之前执行。拉圾回收器会在回收过程中,找到带有终结器的对象,然后加入到终结队列,线程遍历完了,就调用终结队列上对象的终结器来回收资源

二、C#中那些重要的知识

  A、委托与事件

    委托

   C#中将一个方法作为一个参数传递给其它方法使用,实现这样功能的模式叫做委托

   1、委托的类型:是强类型,因为在声明委托方法时,指定的参数,在调用这个委托时必须传递相同类型的参数与参数个数

   2、委托的内部机制:C#中所有的委托都继承自System.Delegate,但是我们不能继承它来实现自定义的委托,可以使用delegate关键字来定义

   3、委托的定义:使用delegate关键字来使用

   4、委托的实例化:定义一个与委托相同类型的函数,作为委托的参数传递,不需要用new关键字进行实例化,它可以通过委托推断,在C#1.0中,在传递方法时,需要用new delegate(Method)

   5、委托的使用:

   

 class DelegateClass

    {

        //一个泛型的委托,可以不同类型的参数进行处理

        public delegate void AlculateMethod<T>(T first, T second);

    }

     class MehtodConllection

    {

        public void AlculateAdd<T>(T first, T second)

        {

            string third = first.ToString() + second.ToString();

            System.Console.WriteLine(third);

        }

        public void AlculateDelete(int first, int second)

        {

            System.Console.WriteLine(first - second);

        }

        public void AlculateAddOther<T>(T first, T second)

        {

            string third = first.ToString() + "Hello Word" + second.ToString();

            System.Console.WriteLine(third);

        }

    }

      private static void _Demo4()

        {

            //方法的集合

            MehtodConllection mc = new MehtodConllection();

            //泛型的委托声明

            DelegateClass.AlculateMethod<string> Demo = null;

            //添加委托方法

            Demo += mc.AlculateAdd<string>;

            //

            Demo += mc.AlculateAddOther<string>;

            //调用方法,所有在委托中的方法都能被执行

            Demo("str", "sterte");

        }

事件

    事件是一种特殊的委托,在声明委托时,添加一个event关键字

    步骤:

     1、定义委托的类型,相当于一个类,如:  public delegate void ActionMethod();

     2、定义事件委托变量,用1、中的委托类型定义,如: public event ActionMethod amd;

     3、调用定义的事件,触发器,如:

     

  class Cat

    {

        //定义委托方法

        public delegate void ActionMethod();

        //声明事件委托

        public event ActionMethod amd;

        //触发事件

        public void CatShout()

        {

            System.Console.WriteLine("猫叫了,事件触发了!!!");

            amd();

        }

    }

     4、向事件中添加方法,将方法与事件绑定在一起,以便在触发时一起执行,如:

      

  private static void _Demo15() {

            Cat cat = new Cat();

            HostPerson hp = new HostPerson();

            Mouse mouse = new Mouse();

           

            cat.amd += mouse.runing;

            cat.amd += hp.WeekUp;

            cat.CatShout();       

        }

      5、最后触发事件

B、反射与特性

  反射

   1、反射的作用:

     (1)、访问程序集中的元数据,比如说,方法属性修鉓符

     (2)、使用元数据,在运行时动态的调用元数据的成员与属性等,而不是在编译时进行绑定

   2、反射是择指对一个程序集中的元数据进行检查的过程,并且可以列举程序集的类型与属性,以及使用一些特定的对象调用上面的成员

   3、使用System.Type访问元数据

     类型的元数据System.Type是一个实例,这个实例提供了一些方法,这些方法可以列举元数据的成员,主要方法有以下几种:

     Type.Name、Type.IsPublic、Type.BaseType、Type.GetInterface()、Type.Assemble、Type.GetProperties()、Type.GetMethod()、Type.GetField()、Type.GetCustomAttributes()等属性

     (1)、使用GetType()得到元数据的类型对象(System.Type)

       例:

类一:     

 

class CustomClass

    {

        private string Name = "Test";

        public string _Name = "Demo";

        private int index { get; set; }

        public int _index { get; set; }

        private void GetName()

        {

        }

        public void Get_Name()

        {

        }

    }

类二:同时使用了typeof与GetType()来得到类型对象

     

 public void Exec()

        {

            CustomClass cc = new CustomClass();

            //得到当前类型的实例对象

            Type type = cc.GetType();           

              //得到当前类型的实例对象使用typeof

            //Type type = typeof(CustomClass);           

            //遍历public的属性,而不是字段,使用GetProperties()

            foreach (PropertyInfo property in type.GetProperties())

            {

                //得到属性名

                System.Console.WriteLine(property.Name);

                //得到属性的类型

                System.Console.WriteLine(property.PropertyType);

                //得到反射的类型,就是反射对象的类名

                System.Console.WriteLine(property.ReflectedType);

                //得到成员类型,是属性还是方法

                System.Console.WriteLine(property.MemberType);

            }

            System.Console.WriteLine("------------------------------------------");

            //得到当前对象的公共方法,包含公共属性的方法get,set

            foreach (System.Reflection.MethodInfo method in type.GetMethods())

            {

                //方法名

                System.Console.WriteLine(method.Name);

                //成员的类型

                System.Console.WriteLine(method.MemberType);

            }

        }

结果:

  (2)、得到与设置属性的值

                

   //设置属性的值

                property.SetValue(cc, 45, null);

                //得到属性的值

                System.Console.WriteLine(property.GetValue(cc, null).ToString());

  (3)、调用方法Invoke()函数

        

 MethodInfo demo = type.GetMethod("Get_Name");

            demo.Invoke(cc, null);

       得到一个无参的方法Get_Name,若有参null应为参数的数组

        如:    //调用有参的

         

   MethodInfo test = type.GetMethod("GetName");

            string[] param = { "12" };

            test.Invoke(cc, param);

特性 (attribute)

   1、特性是用来描述或修饰元数据的额外的信息,比如说:类、属性、程序集等

   2、自定义特性,继承自Attribute类

      如下:

       

class CustomAttribute : Attribute

    {

        public CustomAttribute();

        public CustomAttribute(AttributeTargets validOn);

        public bool AllowMultiple { get; set; }

        public bool Inherited { get; set; }

        public AttributeTargets ValidOn { get; }

    }

    使用:

    

  [CustomAttribute(AttributeTargets.All)]

    class CustomClass

    {

        [CustomAttribute(AllowMultiple = true)]

        [Custom(Inherited = true)]

        private string Name = "Test";

        public string _Name = "Demo";

        private int index { get; set; }

        public int _index { get; set; }

     }

C、扩展方法的使用与Lambda表达式

  扩展方法

    当你不能修改一个类的时候,扩展方法是一个方便给这个类添加其它方法的方式

    1、扩展方法的定义:扩展方法使用this这个关键字,将一个方法绑定到this所指向的类型(如:类)的成员中对,从而就可以通过这个类的对象来调用这个方法,在MVC中,扩展,HtmlHelper类是很有用的,如下代码:

     

 public static class PersonExtension

    {

        public static void Extension(this PersonSingle ps, string name)

        {

            System.Console.WriteLine("Name is " + name);

        }

    }

     将Extension(string name)这方法添加到PersonSingle中去,然后就可以通过对象调用这个方法

PersonSingle类:

    

 public class PersonSingle

    {

        public void Show()

        {

            System.Console.WriteLine("PersonSingle Method!!!");

        }

    }

测试:

     

private static void _Demo16()

        {

            PersonSingle ps = new PersonSingle();

            ps.Show();

            ps.Extension("whc");

        }

   2、扩展方法的访问权限要与所扩展的类的方法一致,这里都是public

    3、扩展方法是写在一个静态类中的静态方法

Lambda表达式

   Lambda表达式是一种比匿名方法更加简洁的一种匿名函数语法,其主要分为二类:一是语句lambda,二是表达式lambda

  1、语句Lambda:是一种匿名方法的简化语法,其中不包含delegate关键字,只需要使用lambda运算符=>,是一个语句块

    例:

       

Demo += (string first, string second) =>

            {

                System.Console.WriteLine();

            };

  2、表达式Lambda:是一个表达式,而不是一个语句块

    例:

     

  Demo = (first, second) => first.ToString();

3、Lambda表达式中能使用外部的变量

总结

 那些年学习C#,作了一些笔记,此文大都直接从笔记中拷贝,当然还有很多没有提到,将在下次追加一些;此文以回忆那些年学习的日子。

原文地址:https://www.cnblogs.com/xin_ny/p/2377193.html