C#開發 —— 泛型,檔案

來源:互聯網
上載者:User

標籤:style   blog   color   使用   檔案   資料   

泛型的目標是採用廣泛適用和可互動性的形式來表示演算法和資料結構 —— 參數化

泛型能子啊編譯時間提供強大的類型檢查,減少資料類型之間的顯式轉換,裝箱操作和運行時的類型檢查

泛型的型別參數T可以被看作是一個預留位置,代表了某種可能的類型

namespace Test01{    //建立一個泛型介面    public interface IGenericInterface<T>    {        T CreateInstance();                                //介面中調用CreateInstance方法    }    //實現上面泛型介面的泛型類    //派生約束where T : TI(T要繼承自TI)    //建構函式約束where T : new()(T可以執行個體化)    public class Factory<T, TI> : IGenericInterface<TI> where T : TI, new()    {        public TI CreateInstance()                            //建立一個公用方法CreateInstance        {            return new T();        }    }    class Program    {        static void Main(string[] args)        {            //執行個體化介面            IGenericInterface<System.ComponentModel.IListSource> factory = new Factory<System.Data.DataTable, System.ComponentModel.IListSource>();            //輸出指定泛型的類型            Console.WriteLine(factory.CreateInstance().GetType().ToString());            Console.ReadLine();        }    }}

在執行個體化泛型時可以使用約束對型別參數的類型種類施加限制,約束是使用where內容關鍵字指定的

 

泛型方法,在聲明中包括了型別參數T的方法

泛型方法可以使用多型別參數進行重載

namespace Test02{    public class Finder                                        //建立一個公用類Finder    {        public static int Find<T>(T[] items, T item)                    //建立泛型方法        {            for (int i = 0; i < items.Length; i++)                        //調用for迴圈            {                if (items[i].Equals(item))                            //調用Equals方法比較兩個數                {                    return i;                                    //返回相等數在數組中的位置                }            }            return -1;                                        //如果不存在指定的數,則返回-1        }    }    class Program    {        static void Main(string[] args)        {            int i = Finder.Find<int>(new int[] { 1, 2, 3, 4, 5, 6, 8, 9 }, 6);    //調用泛型方法,並定義數組指定數字            Console.WriteLine("6在數組中的位置:" + i.ToString());        //輸出中數字在數組中的位置            Console.ReadLine();        }    }}
namespace Test03{    class Program    {        static void Main(string[] args)        {            List<int> myList = new List<int>();            for (int i = 0; i < 10; i++)            {                myList.Add(i);            }            foreach (int i in myList)            {                Console.WriteLine(i);            }            Console.ReadLine();        }    }}
namespace Test04{    class Program    {        static void Main(string[] args)        {            myclass1<int> mclass1 = new myclass1<int>();            myclass2<int> mclass2 = new myclass2<int>();            Console.ReadLine();        }        class myclass1<T>        {            public myclass1()            {                Console.WriteLine("這是第一個泛型類");            }        }        class myclass2<T> : myclass1<T>        {            public myclass2()            {                Console.WriteLine("這是第二個泛型類");            }        }    }}

System.IO 命名空間

File 類和Directory 類分別用來對檔案和各種目錄進行操作,可以被執行個體化,但不能被其他類繼承

File類中的所有方法都是靜態,所以如果只想執行一個操作,那麼使用File類中方法的效率比使用相應的FileInfo類中的方法更高

File類的靜態方法對所有方法都執行安全檢查,如果打算多次重用某個對象,可考慮FileInfo方法 —— 不用總是檢查

if (File.Exists(textBox1.Text))       //使用File類的Exists方法判斷要建立的檔案是否存在                {                    MessageBox.Show("該檔案已經存在");                }                else                {                    File.Create(textBox1.Text);       //使用File類的Create方法建立檔案                }

Directory 類,操作目錄

if (Directory.Exists(textBox1.Text))          //使用Directory類的Exists方法判斷要建立的檔案夾是否存在                {                    MessageBox.Show("該檔案夾已經存在");                }                else                {                    Directory.CreateDirectory(textBox1.Text);  //使用Directory類的CreateDirectory方法建立檔案夾                }

FileInfo類沒有靜態方法,該類中的方法可以用於執行個體化的對象

如果要在檔案上執行幾種操作(或重複操作),則執行個體化FileInfo對象效率更高

FileInfo finfo = new FileInfo(textBox1.Text);                if (finfo.Exists)                        //使用FileInfo對象的Exists屬性判斷要建立的檔案是否存在                {                    MessageBox.Show("該檔案已經存在");                }                else                {                    finfo.Create();                       //使用FileInfo對象的Create方法建立檔案                }
DirectoryInfo dinfo = new DirectoryInfo(textBox1.Text);  //執行個體化DirectoryInfo類對象                if (dinfo.Exists)                        //使用DirectoryInfo對象的Exists屬性判斷要建立的檔案夾是否存在                {                    MessageBox.Show("該檔案夾已經存在");                }                else                {                    dinfo.Create();                      //使用DirectoryInfo對象的Create方法建立檔案夾                }

擷取檔案資訊

if (openFileDialog1.ShowDialog() == DialogResult.OK)            {                textBox1.Text = openFileDialog1.FileName;                FileInfo finfo = new FileInfo(textBox1.Text);         //執行個體化FileInfo對象                string strCTime, strLATime, strLWTime, strName, strFName, strDName, strISRead;                long lgLength;                strCTime = finfo.CreationTime.ToShortDateString();    //擷取檔案建立時間                strLATime = finfo.LastAccessTime.ToShortDateString(); //擷取上次訪問該檔案的時間                strLWTime = finfo.LastWriteTime.ToShortDateString();  //擷取上次寫入檔案的時間                strName = finfo.Name;                                 //擷取檔案名稱                strFName = finfo.FullName;                            //擷取檔案的完整目錄                strDName = finfo.DirectoryName;                       //擷取檔案的完整路徑                strISRead = finfo.IsReadOnly.ToString();              //擷取檔案是否唯讀                lgLength = finfo.Length;                              //擷取檔案長度                MessageBox.Show("檔案資訊:\n建立時間:" + strCTime + " 上次訪問時間:" + strLATime + "\n上次寫入時間:" + strLWTime + " 檔案名稱:" + strName + "\n完整目錄:" + strFName + "\n完整路徑:" + strDName + "\n是否唯讀:" + strISRead + " 檔案長度:" + lgLength);            }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.