C#面向对象几组关键字的详解(this,base,dynamic,var,const,readonly,is,as)

  这几个关键字,在用法上有许多相似之处。这里主要看看细节之处的差异:

一.this和base的区别:

  • this(本类相关):只能在和对象相关的地方使用。this表示的是当前对象;new的是哪个对象,这个this就表示的是哪个对象
  • base(父类相关):表示调用父类的东西,或是父类构造方法
class Person
{
    static Person()
    {
        Console.WriteLine("Person静态构造方法");
    }
    public Person()
    {
        Console.WriteLine("Person构造方法");
        Console.WriteLine(this);
    }
    public virtual void Show()
    {
        Console.WriteLine("Person方法");
    }
}
class Chinese : Person
{
    static Chinese()
    {
        Console.WriteLine("Chinese静态构造方法");
    }
    public Chinese():base()   //调用父类构造方法
    {
        Console.WriteLine("Chinese构造方法");
        Console.WriteLine(this);
        base.Show();     //父类对象调用
        this.Show();       //本类对象调用
    }
    public override void Show()
    {
        Console.WriteLine("Chinese方法");
    }
}
例子分析

调用:

static void Main(string[] args)
{
    Person c = new Chinese();
    Console.ReadKey();
}

输出信息:

  Chinese静态构造方法
  Person静态构造方法
  Person构造方法
  命名空间.Chinese
  Chinese构造方法
  命名空间.Chinese
  Person方法
  Chinese方法

静态构造方法与构造方法:

    • 静态构造方法:先调用本类的静态构造方法,再调用父类
    • 构造方法:先调用父类构造方法,在调用本类的

二.var和dynamic的区别:

  • var(推断类型):变量的类型是在编译时决定的
  • dynamic:变量的类型是在运行时决定的
static void Main(string[] args)
{
    var name = 12;        //在编译时,根据右边的12就推测出左边的name为int
    name = "12";          //编译时,这句就会异常,string不能赋值int
dynamic age = 12; //编译的时候不会去管age是什么类型,程序运行过程中再解析 age = "12"; //这句会正常运行,程序运行解析为string类型 Console.ReadKey(); }

一般可能在下列情况使用dynamic:

    1. 动态语言对象
    2. 反射中动态创建对象

三.const和readonly的区别:

  • const:const的值是在编译期间确定的,因此只能在声明时通过常量表达式指定其值
  • readonly是在运行时计算出其值的,所以还可以通过静态构造函数来赋值
class Person
{
    public Person() { }
    public const string s1 = "const";   //所有的s1都替换成了“const”
    public readonly string str = "";   //这里的值可以运算中计算得出
    public Person(string s)
    {
        str = s;
        //s1 = "ww";  //这里s1就是一个常量了
    }
}
例子分析

调用:

static void Main(string[] args)
{
    System.Console.WriteLine(Person.s1);
    Console.WriteLine(new Person().str);
    Person p1=new Person("11111");
    //p1.str="";         //只读字段这里是不能赋值的
    Console.WriteLine();
    Console.ReadKey();
}

四.is和as的区别:

  • is(判断):is运算符用来检查对象(变量)是否属于某数据类型(如int、string、bool、double、class等),可在安全类型转换之前进行判断之用
  • as(转换):as运算符用于执行引用类型的显式类型转换。如果要转换的类型与指定的类型兼容,转换就会成功进行;如果类型不兼容,as运算符就会返回值null

is表示对象是该类型,或者派生该类型:

static void Main(string[] args)
{
    string str = "string";
    int i = 21;
    Console.WriteLine(i is object);   //true
    Console.WriteLine(i is int);      //true
    Console.WriteLine(i is string);   //false

    Console.WriteLine(str is object);  //true
    Console.WriteLine(str is int);     //false
    Console.WriteLine(str is string);  //true
    Console.ReadKey();
}

as只能转换引用类型,转换失败则返回null

static void Main(string[] args)
{
    object obj = "string";
    object o = 5;
    string str = obj as string;   //string
    string str2 = o as string;    //null
    Console.ReadKey();
}
原文地址:https://www.cnblogs.com/fengxuehuanlin/p/6583195.html