Net复习笔记:第六部分:using关键字

Using一般作用是

  1. 引入命名空间using system
  2. 创建别名 using alias=namespace | type(using MsWord=Microsoft.office.Interop.Word)
  3. 强制资源管理using(){}
  4. Using在编译时会被换成(try-finally)
  5. Using在做资源管理的时候适合做单一非托管资源管理,多个的话建议使用try
  6. Using只能适用于实现了IdisPosable接口的对象

类型转化

  1. explicit用于声明必须强制转化的自定义类型
  2. implicit用于声明隐私的自定义类型转化
  3. public partial class WebForm8 : System.Web.UI.Page
  4.     {
  5.         protected void Page_Load(object sender, EventArgs e)
  6.         {
  7.             MyAge age = new MyAge();
  8.             int bornAge = (int)age;
  9.             Response.Write(bornAge+"<br>");
  10.             age = DateTime.Now.Year;
  11.             Response.Write(age + "<br>");
  12.             Response.Write(age);
  13.         }
  14.     }
  15.     class MyAge
  16.     {
  17.         private int age = 0;
  18.         public int Age
  19.         {
  20.             get { return age; }
  21.             set { age = value; }
  22.         }
  23.  
  24.         public MyAge()
  25.         {
  26.         
  27.         }
  28.  
  29.         private MyAge(int age)
  30.         {
  31.             this.age = age;
  32.         }
  33.  
  34.         public static implicit operator MyAge(int year)
  35.         {
  36.             return new MyAge(year);
  37.         }
  38.  
  39.         public static explicit operator int(MyAge age)
  40.         {
  41.             return age.Age;
  42.         }
  43.  
  44.         public static implicit operator string(MyAge age)
  45.         {
  46.             return "你?的Ì?年¨º龄¢?:êo" + age.Age.ToString();
  47.         }
  48.     }
  49.  
posted @ 2014-01-23 16:44  瀚海行舟  阅读(103)  评论(0编辑  收藏  举报