CLR C#学习笔记

 

 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApp22
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             ///   ///   ///   ///   ///   ///
13             dynamic myvar = 100;        //"dynamic"用来弱化 强类型语言 一开始“myvar”是int类型
14             Console.WriteLine(myvar);
15             myvar = "myvar OK! ";           //但是跑到这来“myvar”变成 string类型  模仿了弱类型语言编程
16             Console.WriteLine(myvar);
17 
18             ///   ///   ///   ///   ///   ///
19             try
20             {
21                 Int32 x = 5;
22                 object o0 = x;
23                 Int16 y = (Int16)o0;     //这句代码会报错  因为o是int32类型装箱进去的,所以当 拆箱强转是用的int16与装箱时不匹配,需要先取出int32之后在进行int16强转。
24 
25                 Int16 y1 = (Int16)(Int32)o0;//先取出int32之后在进行int16强转。,上述在启动调试时会报错.
26             }
27             catch (Exception e)
28             {
29 
30                 Console.WriteLine(e);
31             }
32 
33             ///   ///   ///   ///   ///   ///
34 Int32 v = 5;        //CLR Via C# P114页 装彩箱例子 35 object O = v; //生成的IL代码 中显示 这步会 “V”装箱 将引用指针存储到“O”中---装箱装箱装箱 36 v = 123; //将"123"加载到V里面,,这之后 会对“V”装箱 且将指针保留在栈上以进行Concat(连接)操作---装箱装箱装箱 37 //接着将字符串加载到栈上 以进行Concat(连接)操作 38 //对“O”拆箱 获取指针 指向栈上的 Int32 字段 39 //对Int32装箱,将指针保留在栈上 以进行Concat(连接)操作 ---装箱装箱装箱 40 Console.WriteLine(v + "," + (Int32)O); //这个经历了 3次的装箱 41 42 /// /// /// /// /// /// 43 44 Console.WriteLine();//这属于静态方法 无需实例化 直接用 45 object o1 = new object(); 46 o1.GetHashCode();//"public virtual int GetHashCode();这当中的"virtual"虚方法标志 ===抽象类中的 虚方法 我实现它 但是不调用它 要之后的来继承他的来重写覆盖它 47 o1.GetType(); //调用的是非虚实例方法 48 49 Console.ReadKey(); 50 } 51 52 int a = 100; 53 object x; 54 55 public void fun1() 56 { 57 x = a; //装箱 58 } 59 60 public void fun2() 61 { 62 x = a; 63 int b = (int)x; //拆箱(强转就需要拆箱) 64 Console.WriteLine(b); 65 } 66 67 68 //System.ValueType; 69 } 70 71 //public static class AStaticClass 72 //{ 73 // public static void AStaticMethd() 74 // { 75 76 // } 77 78 // public static string AStaticproper 79 80 81 //} 82 83 }

 

posted @ 2020-09-23 17:03  庚子年小菜鸟  阅读(67)  评论(0)    收藏  举报