Using一般作用是
- 引入命名空间using system
- 创建别名 using alias=namespace | type(using MsWord=Microsoft.office.Interop.Word)
- 强制资源管理using(){}
- Using在编译时会被换成(try-finally)
- Using在做资源管理的时候适合做单一非托管资源管理,多个的话建议使用try
- Using只能适用于实现了IdisPosable接口的对象
类型转化
- explicit用于声明必须强制转化的自定义类型
- implicit用于声明隐私的自定义类型转化
- public partial class WebForm8 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- MyAge age = new MyAge();
- int bornAge = (int)age;
- Response.Write(bornAge+"<br>");
- age = DateTime.Now.Year;
- Response.Write(age + "<br>");
- Response.Write(age);
- }
- }
- class MyAge
- {
- private int age = 0;
- public int Age
- {
- get { return age; }
- set { age = value; }
- }
- public MyAge()
- {
- }
- private MyAge(int age)
- {
- this.age = age;
- }
- public static implicit operator MyAge(int year)
- {
- return new MyAge(year);
- }
- public static explicit operator int(MyAge age)
- {
- return age.Age;
- }
- public static implicit operator string(MyAge age)
- {
- return "你?的Ì?年¨º龄¢?:êo" + age.Age.ToString();
- }
- }