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

C#使用泛型實(shí)現(xiàn)獲取并顯示員工信息

 更新時(shí)間:2024年02月21日 08:50:43   作者:wenchm  
泛型編程是一種編程方式,它利用“參數(shù)化類型”將類型抽象化,從而實(shí)現(xiàn)更為靈活的復(fù)用,泛型是用于處理算法、數(shù)據(jù)結(jié)構(gòu)的一種編程方法,本文給大家介紹了C#使用泛型實(shí)現(xiàn)獲取并顯示員工信息實(shí)例,需要的朋友可以參考下

一、關(guān)于泛型

1.泛型定義

泛型編程是一種編程方式,它利用“參數(shù)化類型”將類型抽象化,從而實(shí)現(xiàn)更為靈活的復(fù)用。泛型是用于處理算法、數(shù)據(jù)結(jié)構(gòu)的一種編程方法。泛型的目標(biāo)是采用廣泛適用和可交互性的形式來(lái)表示算法和數(shù)據(jù)結(jié)構(gòu),以使它們能夠直接用于軟件構(gòu)造。泛型類、結(jié)構(gòu)、接口、委托和方法可以根據(jù)它們存儲(chǔ)和操作的數(shù)據(jù)類型來(lái)進(jìn)行參數(shù)化。

2.泛型與非泛型的區(qū)別

泛型能在編譯時(shí)提供強(qiáng)大的類型檢查,減少數(shù)據(jù)類型之間的顯示轉(zhuǎn)換、裝箱操作和運(yùn)行時(shí)的類型檢查等。泛型類和泛型方法同時(shí)具備可重用性、類型安全和效率高等特性,這是非泛型類和非泛型方法無(wú)法具備的。

3.泛型的應(yīng)用

泛型類、結(jié)構(gòu)、接口、委托和方法可以根據(jù)它們存儲(chǔ)和操作的數(shù)據(jù)類型來(lái)進(jìn)行參數(shù)化。泛型通常用在集合和在集合上運(yùn)行的方法中。

泛型是C#編程中一種強(qiáng)大的功能,它允許創(chuàng)建可以處理多種類型的數(shù)據(jù)的通用類、方法和函數(shù)。泛型是在C# 2.0中引入的,它提供了一種類型安全的方式,可以在不犧牲性能的情況下重用代碼。

泛型的主要優(yōu)點(diǎn)是它可以在編譯時(shí)檢查類型安全,避免了運(yùn)行時(shí)類型轉(zhuǎn)換錯(cuò)誤。它還可以提高代碼的可讀性和可重用性。以下是C#中使用泛型的一些示例:

(1)泛型類:

public class MyClass<T>
{
    private T _value;
 
    public MyClass(T value)
    {
        _value = value;
    }
    
    public T GetValue()
    {
        return _value;
    }
}

(2)泛型方法:

public static T Swap<T>(ref T a, ref T b)
{
    T temp = a;
    a = b;
    b = temp;
}

(3)泛型委托:

public delegate T MyGenericDelegate<T>(T input);

(4)泛型接口:

public interface IMyGenericInterface<T>
{
    void DoSomething(T value);
}

(5)泛型約束:

public class MyClass<T> where T : struct
{
    // ...
}

在上面的示例中,T是一個(gè)類型參數(shù),它可以在創(chuàng)建類、方法或函數(shù)時(shí)指定。通過(guò)使用泛型,我們可以創(chuàng)建可以處理不同類型的數(shù)據(jù)的通用代碼,而不需要為每種類型編寫特定的代碼。

二、實(shí)例

本實(shí)例將使用泛型存儲(chǔ)不同類型的數(shù)據(jù),在實(shí)現(xiàn)時(shí),首先定義一個(gè)泛型類,并在泛型類中定義多個(gè)泛型變量;然后使用這些變量記錄不同類型的數(shù)據(jù),這樣就可以重復(fù)利用泛型變量來(lái)存儲(chǔ)不同類型的數(shù)據(jù)。本實(shí)例用來(lái)通過(guò)泛型獲取員工信息并顯示。

// 通過(guò)泛型獲取員工信息并顯示
namespace _126
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private Button? button1;
        private TextBox? textBox6;
        private TextBox? textBox5;
        private TextBox? textBox4;
        private Label? label6;
        private Label? label5;
        private Label? label4;
        private Label? label1;
        private Label? label2;
        private Label? label3;
        private TextBox? textBox1;
        private TextBox? textBox2;
        private TextBox? textBox3;
 
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }
 
        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(15, 19),
                Name = "label1",
                Size = new Size(44, 17),
                TabIndex = 1,
                Text = "編號(hào):"
            };
            // 
            // label2
            // 
            label2 = new Label
            {
                AutoSize = true,
                Location = new Point(15, 51),
                Name = "label2",
                Size = new Size(44, 17),
                TabIndex = 2,
                Text = "性別:"
            };
            // 
            // label3
            // 
            label3 = new Label
            {
                AutoSize = true,
                Location = new Point(15, 83),
                Name = "label3",
                Size = new Size(44, 17),
                TabIndex = 3,
                Text = "生日:"
            };
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(64, 13),
                Name = "textBox1",
                Size = new Size(100, 23),
                TabIndex = 4
            };
            // 
            // textBox2
            // 
            textBox2 = new TextBox
            {
                Location = new Point(64, 45),
                Name = "textBox2",
                Size = new Size(100, 23),
                TabIndex = 5
            };
            // 
            // textBox3
            // 
            textBox3 = new TextBox
            {
                Location = new Point(64, 77),
                Name = "textBox3",
                Size = new Size(100, 23),
                TabIndex = 6
            };
            // 
            // label4
            // 
            label4 = new Label
            {
                AutoSize = true,
                Location = new Point(173, 19),
                Name = "label4",
                Size = new Size(44, 17),
                TabIndex = 7,
                Text = "姓名:"
            };
            // 
            // label5
            // 
            label5 = new Label
            {
                AutoSize = true,
                Location = new Point(173, 51),
                Name = "label5",
                Size = new Size(44, 17),
                TabIndex = 8,
                Text = "年齡:"
            };
            // 
            // label6
            // 
            label6 = new Label
            {
                AutoSize = true,
                Location = new Point(173, 83),
                Name = "label6",
                Size = new Size(44, 17),
                TabIndex = 9,
                Text = "工資:"
            };
            // 
            // textBox4
            // 
            textBox4 = new TextBox
            {
                Location = new Point(222, 13),
                Name = "textBox4",
                Size = new Size(100, 23),
                TabIndex = 10
            };
            // 
            // textBox5
            // 
            textBox5 = new TextBox
            {
                Location = new Point(222, 45),
                Name = "textBox5",
                Size = new Size(100, 23),
                TabIndex = 11
            };
            // 
            // textBox6
            // 
            textBox6 = new TextBox
            {
                Location = new Point(222, 77),
                Name = "textBox6",
                Size = new Size(100, 23),
                TabIndex = 12
            };
            // 
            // groupBox1
            // 
            groupBox1 = new GroupBox
            {
                Location = new Point(12, 12),
                Name = "groupBox1",
                Size = new Size(335, 107),
                TabIndex = 0,
                TabStop = false,
                Text = "員工信息"
            };
            groupBox1.Controls.Add(textBox6);
            groupBox1.Controls.Add(textBox5);
            groupBox1.Controls.Add(textBox4);
            groupBox1.Controls.Add(label6);
            groupBox1.Controls.Add(label5);
            groupBox1.Controls.Add(label4);
            groupBox1.Controls.Add(label1);
            groupBox1.Controls.Add(label2);
            groupBox1.Controls.Add(label3);
            groupBox1.Controls.Add(textBox1);
            groupBox1.Controls.Add(textBox2);
            groupBox1.Controls.Add(textBox3);
            groupBox1.SuspendLayout();
 
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(272, 125),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 0,
                Text = "獲取",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
           
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(359, 156);
            Controls.Add(button1);
            Controls.Add(groupBox1);
            Name = "Form1";
            Text = "使用泛型存儲(chǔ)不同類型的數(shù)據(jù)列表";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
 
 
        }
        /// <summary>
        /// 為泛型類中聲明的字段進(jìn)行賦值,存儲(chǔ)不同類型的值
        /// 實(shí)例化泛型類對(duì)象
        /// 將泛型類中各字段的值顯示在文本框中
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            Types<object> Extecute = new()
            {
                ID = 1,
                Name = "王老師",
                Sex = "男",
                Age = 25,
                Birthday = Convert.ToDateTime("1986-06-08"),
                Salary = 1500.45F
            };
 
            textBox1!.Text = Extecute.ID.ToString();
            textBox2!.Text = Extecute.Sex.ToString();
            textBox3!.Text = Extecute.Birthday.ToString();
            textBox4!.Text = Extecute.Name.ToString();
            textBox5!.Text = Extecute.Age.ToString();
            textBox6!.Text = Extecute.Salary.ToString();
        }
        /// <summary>
        /// 聲明泛型類
        /// 在泛型類內(nèi)聲明泛型字段
        /// </summary>
        /// <typeparam name="T"></typeparam>
        class Types<T>
        {
            public T? ID;      //聲明編號(hào)字段 
            public T? Name;    //聲明姓名字段 
            public T? Sex;     //聲明性別字段 
            public T? Age;     //聲明年齡字段 
            public T? Birthday;//聲明生日字段 
            public T? Salary;  //聲明薪水字段 
        }
    }
}

以上就是C#使用泛型實(shí)現(xiàn)獲取并顯示員工信息的詳細(xì)內(nèi)容,更多關(guān)于C#泛型獲取信息的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論