Advanced_C#_Skill_C# 中泛型在 Class 上的實踐

泛型   是捨麼意思呢???


就是指  我們在寫程式的時候需要一個
資料型態(數據類型)然後我們在剛開始的時候還不確定這個數據類型是怎麼樣的

或者是說我們對於不同的多個的數據類型做相同的功能、操作
此時我們要不想要多次的寫這個code

此時  泛型  就派上用場了

我們用泛型來表示一個操作針對不同的數據類型


我們就先來一個 C# Console Application  吧


這是一個泛型的 陣列裏頭用一個間括號加一個T

裏頭 參數是一個 T   可以是各式各樣的類型








我們已經新建好一個  Genere 的  class 也就是 泛型 的class


第一階段Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS泛型
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }

    //創建一個使用到 泛型 的  class
    //這是一個泛型的 陣列(數組)
    public class MyGenericArray<T>
    {
        private T[] array;//一個陣列型態的成員變數 以T資料型態的陣列

        //構造函數(建構子/式)一個屬於有參數的建構式傳進來一個整數型態參數
        public MyGenericArray(int size)
        {
            array = new T[size + 1];//使它裏頭局部變數的陣列做第一次初始化(實體化)
        }
        //創建兩個方法分別用來獲取陣列(數組)中的內容  及   設值
        public T GetItem(int index)//
        {
            return array[index];
        }
        //設置元素的值
        //兩個參數:一個鎮列整數索引、一個泛型陣列的值(目前都還不知道這個T是捨麼類型)變數名稱叫value
        public void SetItem(int index, T value)
        {
            array[index] = value;//直接傳遞給這個數組(陣列)
        }
    }
}


接下來讓我們來看一下   泛型的'強大之處

定義好了   C#泛型的Class之後  就要使用它



首先新建一個   規定  泛型裏頭要為   整數型態的   陣列

MyGenericArray<int> intArray = new MyGenericArray<int>(5);



我們先給每一位賦予值



再把它輸出出來






第二階段Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS泛型
{
    class Program
    {
        static void Main(string[] args)
        {
            MyGenericArray<int> intArray = new MyGenericArray<int>(5);

            for (int c = 0; c < 5; c++)
            {
                intArray.SetItem(c, c*5);//第幾位就乘5
            }
            for (int c = 0; c < 5; c++)
            {
                Console.Write(intArray.GetItem(c)+" ");
            }

            Console.ReadLine();
        }
    }

    //創建一個使用到 泛型 的  class
    //這是一個泛型的 陣列(數組)
    public class MyGenericArray<T>
    {
        private T[] array;//一個陣列型態的成員變數 以T資料型態的陣列

        //構造函數(建構子/式)一個屬於有參數的建構式傳進來一個整數型態參數
        public MyGenericArray(int size)
        {
            array = new T[size + 1];//使它裏頭局部變數的陣列做第一次初始化(實體化)
        }
        //創建兩個方法來獲取陣列(數組)中的內容
        public T GetItem(int index)//
        {
            return array[index];
        }
        //設置元素的值
        //兩個參數:一個鎮列整數索引、一個泛型陣列的值(目前都還不知道這個T是捨麼類型)變數名稱叫value
        public void SetItem(int index, T value)
        {
            array[index] = value;//直接傳遞給這個數組(陣列)
        }
    }
}





當我們需要用到  另一種  資料型態  做同樣的事情時候呢???

比方說     字元陣列




我們就複製上方程式   把型態改為   char


這裡小地方    注意一下   char   對應的 Ascii code   可以記常用的10進位那一直列即可



同一個  Class  用了一個  泛型效果  針對不同  資料型態  實踐同一個操作


可以 對  int   也可以對  char    做操作

提高資料重用性   提高C#效能


資料類型是   安全的!!!

可對不同資料型態做同樣的一塊 代碼操作


第三階段Code_擴充


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS泛型
{
    class Program
    {
        static void Main(string[] args)
        {
            MyGenericArray<int> intArray = new MyGenericArray<int>(5);

            for (int c = 0; c < 5; c++)
            {
                intArray.SetItem(c, c*5);//第幾位就乘5
            }
            for (int c = 0; c < 5; c++)
            {
                Console.Write(intArray.GetItem(c)+" ");
            }
            Console.WriteLine();
            Console.WriteLine("====================================");

            MyGenericArray<char> charArray = new MyGenericArray<char>(5);

            for (int c = 0; c < 5; c++)
            {
                charArray.SetItem(c, (char)(c + 97));//依序顯示小寫英文字母
            }
            for (int c = 0; c < 5; c++)
            {
                Console.Write(charArray.GetItem(c) + " ");
            }
            Console.ReadLine();
        }
    }

    //創建一個使用到 泛型 的  class
    //這是一個泛型的 陣列(數組)
    public class MyGenericArray<T>
    {
        private T[] array;//一個陣列型態的成員變數 以T資料型態的陣列

        //構造函數(建構子/式)一個屬於有參數的建構式傳進來一個整數型態參數
        public MyGenericArray(int size)
        {
            array = new T[size + 1];//使它裏頭局部變數的陣列做第一次初始化(實體化)
        }
        //創建兩個方法來獲取陣列(數組)中的內容
        public T GetItem(int index)//
        {
            return array[index];
        }
        //設置元素的值
        //兩個參數:一個鎮列整數索引、一個泛型陣列的值(目前都還不知道這個T是捨麼類型)變數名稱叫value
        public void SetItem(int index, T value)
        {
            array[index] = value;//直接傳遞給這個數組(陣列)
        }
    }
}



留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題