C#中is,as,using關鍵字的使用說明
阿新 • • 發佈:2020-12-08
一、問題描述
在C#中is,as,using關鍵字具有其特點及使用場景,其中is關鍵字用於檢查該物件是否與給定型別相容,as關鍵字用於將物件轉換為指定型別,using關鍵字除了用於引入名稱空間之外,還具有回收物件資源,如檔案資源、網路資源和資料庫資源等。
1、is:用於檢查物件是否與給定型別相容,如果相容,則返回true,否則返回false,不會丟擲異常。在進行型別轉換之前,可以先用is判斷物件是否與給定型別相容,如果相容再進行轉換。
案例:
string str ="test"; object obj = str; if(obj is string) {string str2 = (string)obj};
2、as:用於引用型別之間轉換,直接進行轉換,若轉換成功,則返回轉換後的物件,若轉換失敗返回null,不丟擲異常。
案例:
string str ="test"; object obj = str; string str2 = obj as tring; if(str2 !=null) {轉換成功}
3、using:引用名稱空間,有效回收資源,using關鍵字可以回收多個物件的資源,關鍵字後面的小括號內建立的物件必須實現IDisposable介面,或者該類的基類已經實現了IDisposable介面。回收資源的時機是在using關鍵字下面的程式碼塊執行完成之後自動呼叫介面方法Dispose()銷燬物件。
案例:
using (Test test =new Test()) { 各種操作;} calss Test :IDisposable { public void Dispose() {回收操作;} }
二、程式碼案例
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace test1 { public partial class Form9 : Form { public Form9() { InitializeComponent(); } private void button1_Click(object sender,EventArgs e) { //轉為object if (obj_rdb.Checked) { //使用using關鍵字,在程式碼塊執行完成之後自動回收資源 //由於FileStream已經實現了IDisposable介面,可以直接使用 using (FileStream fileStream = new FileStream(@"d:\test.txt",System.IO.FileMode.Create)) { object obj = fileStream as object; //直接使用as轉換 if (obj != null) { MessageBox.Show("FileStream轉換為object成功","提示資訊"); } else { MessageBox.Show("FileStream轉換為object失敗","錯誤資訊"); } } } else { using (FileStream fileStream = new FileStream(@"d:\test.txt",System.IO.FileMode.Create)) { //直接強制轉換 try { Stream stream = (Stream)fileStream; MessageBox.Show("FileStream轉換為Stream成功","提示資訊"); }catch(Exception ex) { MessageBox.Show(ex.Message,"錯誤資訊"); } } } } } }
三、顯示結果
補充知識:c#Constructor建構函式注入
1、建立介面
public interface ITimeProvider { DateTime CurrentDate { get; } string CurrentYear { get; } }
2、繼承介面,實現類
public class TimeProvider : ITimeProvider { public DateTime CurrentDate { get { return DateTime.Now; } } public string CurrentYear { get { return DateTime.Now.Year.ToString(); } } }
3、建立注入機制
public class Assembler { private static Dictionary<Type,Type> dictionary = new Dictionary<Type,Type>(); public Assembler() { dictionary.Add(typeof(ITimeProvider),typeof(TimeProvider)); } public object Create(Type type) { if (type == null || !dictionary.ContainsKey(type)) throw new NullReferenceException(); Type targetType = dictionary[type]; return Activator.CreateInstance(targetType); } public T Create<T>() { return (T)Create(typeof(T)); } }
4、客戶端呼叫
public class Client { private ITimeProvider timeProvider; public Client(ITimeProvider timeProvider) { this.timeProvider = timeProvider; } public string GetYear() { return timeProvider.CurrentYear .ToString(); } public string GetDatetime() { return timeProvider.CurrentDate.ToString(); } }
5、使用實現
ITimeProvider timeProvider = (new Assembler()).Create<ITimeProvider>(); Client clinet = new Client(timeProvider); Console.WriteLine(clinet.GetYear()); Console.WriteLine(clinet.GetDatetime());
以上這篇C#中is,using關鍵字的使用說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。