C#中is,as,using關(guān)鍵字的使用說明
一、問題描述
在C#中is,as,using關(guān)鍵字具有其特點及使用場景,其中is關(guān)鍵字用于檢查該對象是否與給定類型兼容,as關(guān)鍵字用于將對象轉(zhuǎn)換為指定類型,using關(guān)鍵字除了用于引入命名空間之外,還具有回收對象資源,如文件資源、網(wǎng)絡(luò)資源和數(shù)據(jù)庫資源等。
1、is:用于檢查對象是否與給定類型兼容,如果兼容,則返回true,否則返回false,不會拋出異常。在進(jìn)行類型轉(zhuǎn)換之前,可以先用is判斷對象是否與給定類型兼容,如果兼容再進(jìn)行轉(zhuǎn)換。
案例:
string str ="test";
object obj = str;
if(obj is string) {string str2 = (string)obj};
2、as:用于引用類型之間轉(zhuǎn)換,直接進(jìn)行轉(zhuǎn)換,若轉(zhuǎn)換成功,則返回轉(zhuǎn)換后的對象,若轉(zhuǎn)換失敗返回null,不拋出異常。
案例:
string str ="test";
object obj = str;
string str2 = obj as tring;
if(str2 !=null) {轉(zhuǎn)換成功}
3、using:引用命名空間,有效回收資源,using關(guān)鍵字可以回收多個對象的資源,關(guān)鍵字后面的小括號內(nèi)創(chuàng)建的對象必須實現(xiàn)IDisposable接口,或者該類的基類已經(jīng)實現(xiàn)了IDisposable接口?;厥召Y源的時機(jī)是在using關(guān)鍵字下面的代碼塊執(zhí)行完成之后自動調(diào)用接口方法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)
{
//轉(zhuǎn)為object
if (obj_rdb.Checked)
{
//使用using關(guān)鍵字,在代碼塊執(zhí)行完成之后自動回收資源
//由于FileStream已經(jīng)實現(xiàn)了IDisposable接口,可以直接使用
using (FileStream fileStream = new FileStream(@"d:\test.txt", System.IO.FileMode.Create))
{
object obj = fileStream as object; //直接使用as轉(zhuǎn)換
if (obj != null)
{
MessageBox.Show("FileStream轉(zhuǎn)換為object成功", "提示信息");
}
else
{
MessageBox.Show("FileStream轉(zhuǎn)換為object失敗", "錯誤信息");
}
}
}
else
{
using (FileStream fileStream = new FileStream(@"d:\test.txt", System.IO.FileMode.Create))
{
//直接強(qiáng)制轉(zhuǎn)換
try
{
Stream stream = (Stream)fileStream;
MessageBox.Show("FileStream轉(zhuǎn)換為Stream成功", "提示信息");
}catch(Exception ex)
{
MessageBox.Show(ex.Message, "錯誤信息");
}
}
}
}
}
}
三、顯示結(jié)果


補(bǔ)充知識:c#Constructor構(gòu)造函數(shù)注入
1、創(chuàng)建接口
public interface ITimeProvider
{
DateTime CurrentDate { get; }
string CurrentYear { get; }
}
2、繼承接口,實現(xiàn)類
public class TimeProvider : ITimeProvider
{
public DateTime CurrentDate { get { return DateTime.Now; } }
public string CurrentYear { get { return DateTime.Now.Year.ToString(); } }
}
3、創(chuàng)建注入機(jī)制
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、客戶端調(diào)用
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、使用實現(xiàn)
ITimeProvider timeProvider = (new Assembler()).Create<ITimeProvider>();
Client clinet = new Client(timeProvider);
Console.WriteLine(clinet.GetYear());
Console.WriteLine(clinet.GetDatetime());
以上這篇C#中is,as,using關(guān)鍵字的使用說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法
這篇文章主要介紹了C#從文件或標(biāo)準(zhǔn)輸入設(shè)備讀取指定行的方法,涉及C#文件及IO操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
C#使用時序數(shù)據(jù)庫InfluxDB的教程詳解
InfluxDB是一個開源的時序數(shù)據(jù)庫,可以自動處理時間序列數(shù)據(jù),這篇文章主要為大家詳細(xì)介紹了C#如何使用InfluxDB,感興趣的小伙伴可以跟隨小編一起了解下2023-11-11
Unity調(diào)用手機(jī)攝像機(jī)識別二維碼
這篇文章主要為大家詳細(xì)介紹了Unity調(diào)用手機(jī)攝像機(jī)識別二維碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07
解析C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù)
這篇文章主要介紹了C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù),是C#入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2016-01-01

