欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#中is,as,using關(guān)鍵字的使用說明

 更新時(shí)間:2020年12月07日 14:45:42   作者:tongyuehong  
這篇文章主要介紹了C#中is,as,using關(guān)鍵字的使用說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

一、問題描述

在C#中is,as,using關(guān)鍵字具有其特點(diǎn)及使用場(chǎng)景,其中is關(guān)鍵字用于檢查該對(duì)象是否與給定類型兼容,as關(guān)鍵字用于將對(duì)象轉(zhuǎn)換為指定類型,using關(guān)鍵字除了用于引入命名空間之外,還具有回收對(duì)象資源,如文件資源、網(wǎng)絡(luò)資源和數(shù)據(jù)庫資源等。

1、is:用于檢查對(duì)象是否與給定類型兼容,如果兼容,則返回true,否則返回false,不會(huì)拋出異常。在進(jìn)行類型轉(zhuǎn)換之前,可以先用is判斷對(duì)象是否與給定類型兼容,如果兼容再進(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)換后的對(duì)象,若轉(zhuǎn)換失敗返回null,不拋出異常。

案例:

string str ="test"; 
object obj = str;
string str2 = obj as tring;
if(str2 !=null) {轉(zhuǎn)換成功}

3、using:引用命名空間,有效回收資源,using關(guān)鍵字可以回收多個(gè)對(duì)象的資源,關(guān)鍵字后面的小括號(hào)內(nèi)創(chuàng)建的對(duì)象必須實(shí)現(xiàn)IDisposable接口,或者該類的基類已經(jīng)實(shí)現(xiàn)了IDisposable接口。回收資源的時(shí)機(jī)是在using關(guān)鍵字下面的代碼塊執(zhí)行完成之后自動(dòng)調(diào)用接口方法Dispose()銷毀對(duì)象。

案例:

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í)行完成之后自動(dòng)回收資源
    //由于FileStream已經(jīng)實(shí)現(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失敗", "錯(cuò)誤信息");
     }
    }
   }
   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, "錯(cuò)誤信息");
     }
     
    }
   }
   
  }
 }
}

三、顯示結(jié)果

補(bǔ)充知識(shí):c#Constructor構(gòu)造函數(shù)注入

1、創(chuàng)建接口

 public interface ITimeProvider
  {
    DateTime CurrentDate { get; }
    string CurrentYear { get; }
  }

2、繼承接口,實(shí)現(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、使用實(shí)現(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)鍵字的使用說明就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • C#中的多播委托和泛型委托

    C#中的多播委托和泛型委托

    這篇文章介紹了C#中的多播委托和泛型委托,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#使用時(shí)序數(shù)據(jù)庫InfluxDB的教程詳解

    C#使用時(shí)序數(shù)據(jù)庫InfluxDB的教程詳解

    InfluxDB是一個(gè)開源的時(shí)序數(shù)據(jù)庫,可以自動(dòng)處理時(shí)間序列數(shù)據(jù),這篇文章主要為大家詳細(xì)介紹了C#如何使用InfluxDB,感興趣的小伙伴可以跟隨小編一起了解下
    2023-11-11
  • C#中關(guān)于可空類型的小知識(shí)

    C#中關(guān)于可空類型的小知識(shí)

    這篇文章主要介紹了C#中關(guān)于可空類型的小知識(shí),本文講解可空類型運(yùn)算的小技巧,需要的朋友可以參考下
    2015-04-04
  • Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼

    Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼

    這篇文章主要為大家詳細(xì)介紹了Unity調(diào)用手機(jī)攝像機(jī)識(shí)別二維碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • 解析C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù)

    解析C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù)

    這篇文章主要介紹了C#中的私有構(gòu)造函數(shù)和靜態(tài)構(gòu)造函數(shù),是C#入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01
  • 最新評(píng)論