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

C#中is,as,using關鍵字的使用說明

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

一、問題描述

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

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關鍵字可以回收多個對象的資源,關鍵字后面的小括號內創(chuàng)建的對象必須實現(xiàn)IDisposable接口,或者該類的基類已經(jīng)實現(xiàn)了IDisposable接口?;厥召Y源的時機是在using關鍵字下面的代碼塊執(zhí)行完成之后自動調用接口方法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關鍵字,在代碼塊執(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轉換
     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構造函數(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)建注入機制

 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、使用實現(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關鍵字的使用說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • C#反射(Reflection)詳解

    C#反射(Reflection)詳解

    本文詳細講解了C#中的反射(Reflection),文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#從文件或標準輸入設備讀取指定行的方法

    C#從文件或標準輸入設備讀取指定行的方法

    這篇文章主要介紹了C#從文件或標準輸入設備讀取指定行的方法,涉及C#文件及IO操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • 淺談C#多線程下的調優(yōu)

    淺談C#多線程下的調優(yōu)

    本文主要介紹了C#多線程下的調優(yōu),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-04-04
  • UGUI實現(xiàn)4位驗證碼輸入

    UGUI實現(xiàn)4位驗證碼輸入

    這篇文章主要為大家詳細介紹了UGUI實現(xiàn)4位驗證碼輸入,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • unity里獲取text中文字寬度并截斷省略的操作

    unity里獲取text中文字寬度并截斷省略的操作

    這篇文章主要介紹了unity里獲取text中文字寬度并截斷省略的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#中的多播委托和泛型委托

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

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

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

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

    C#中關于可空類型的小知識

    這篇文章主要介紹了C#中關于可空類型的小知識,本文講解可空類型運算的小技巧,需要的朋友可以參考下
    2015-04-04
  • Unity調用手機攝像機識別二維碼

    Unity調用手機攝像機識別二維碼

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

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

    這篇文章主要介紹了C#中的私有構造函數(shù)和靜態(tài)構造函數(shù),是C#入門學習中的基礎知識,需要的朋友可以參考下
    2016-01-01

最新評論