C#使用泛型方法實(shí)現(xiàn)操作不同數(shù)據(jù)類型的數(shù)組
再發(fā)一個(gè)泛型方法的示例。
一、泛型方法及其存在的意義
實(shí)際應(yīng)用中,查找或遍歷數(shù)組中的值時(shí),有時(shí)因?yàn)閿?shù)組類型的不同,需要對(duì)不同的數(shù)組進(jìn)行操作,那么,可以使用同一種方法對(duì)不同類型的數(shù)組進(jìn)行操作嗎?答案是肯定的,本實(shí)例將使用一個(gè)泛型方法對(duì)不同類型的數(shù)組進(jìn)行操作。
二 、實(shí)例
1.源碼
// 使用一個(gè)泛型方法對(duì)不同類型的數(shù)組進(jìn)行操作 namespace _127 { public partial class Form1 : Form { private Button? button1; private Button? button2; private Button? button3; public Form1() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { button1 = new Button(); button2 = new Button(); button3 = new Button(); SuspendLayout(); // // button1 // button1.Location = new Point(21, 28); button1.Name = "button1"; button1.Size = new Size(75, 23); button1.TabIndex = 0; button1.Text = "字符串"; button1.UseVisualStyleBackColor = true; button1.Click += Button1_Click; // // button2 // button2.Location = new Point(106, 28); button2.Name = "button2"; button2.Size = new Size(75, 23); button2.TabIndex = 1; button2.Text = "整數(shù)"; button2.UseVisualStyleBackColor = true; button2.Click += Button2_Click; // // button3 // button3.Location = new Point(191, 28); button3.Name = "button3"; button3.Size = new Size(75, 23); button3.TabIndex = 2; button3.Text = "布爾"; button3.UseVisualStyleBackColor = true; button3.Click += Button3_Click; // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(284, 81); Controls.Add(button3); Controls.Add(button2); Controls.Add(button1); Name = "Form1"; Text = "泛型查找不同數(shù)組中的值"; } /// <summary> /// 聲明一個(gè)字符串類型的數(shù)組 /// 調(diào)用泛型方法,查找字符串“三”在數(shù)組中的索引 /// </summary> private void Button1_Click(object? sender, EventArgs e) { string[] str = ["一", "二", "三", "四", "五", "六", "七", "八", "九"]; MessageBox.Show(Finder.Find(str, "三").ToString()); } /// <summary> /// 聲明一個(gè)整數(shù)類型的數(shù)組 /// 調(diào)用泛型方法,查找數(shù)字5在數(shù)組中的索引 /// </summary> private void Button2_Click(object? sender, EventArgs e) { int[] IntArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]; MessageBox.Show(Finder.Find(IntArray, 5).ToString()); } /// <summary> /// 聲明一個(gè)布爾類型的數(shù)組 /// 調(diào)用泛型方法,查找false在數(shù)組中的索引 /// </summary> private void Button3_Click(object? sender, EventArgs e) { bool[] IntArray = [true, false]; MessageBox.Show(Finder.Find(IntArray, false).ToString()); } /// <summary> /// 定義一個(gè)類 /// 在類中,定義一個(gè)泛型方法,用來查找指定值在數(shù)組中的索引 /// 遍歷泛型數(shù)組 /// </summary> public class Finder { public static int Find<T>(T[] items, T item) { for (int i = 0; i < items.Length; i++) { if (items[i]!.Equals(item)) //判斷是否找到了指定值 { return i; //返回指定值在數(shù)組中的索引 } } return -1; //如果沒有找到,返回-1 } } } }
2.生成效果
到此這篇關(guān)于C#使用泛型方法實(shí)現(xiàn)操作不同數(shù)據(jù)類型的數(shù)組的文章就介紹到這了,更多相關(guān)C#泛型方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于async和await的一些誤區(qū)實(shí)例詳解
這篇文章主要介紹了關(guān)于async和await的一些誤區(qū)實(shí)例詳解,有助于更加深入的理解C#程序設(shè)計(jì),需要的朋友可以參考下2014-08-08C# 利用ICSharpCode.SharpZipLib實(shí)現(xiàn)在線壓縮和解壓縮
本文主要主要介紹了利用ICSharpCode.SharpZipLib第三方的DLL庫(kù)實(shí)現(xiàn)在線壓縮和解壓縮的功能,并做了相關(guān)的代碼演示。2016-04-04C# FileStream實(shí)現(xiàn)大文件復(fù)制
這篇文章主要為大家詳細(xì)介紹了C# FileStream實(shí)現(xiàn)大文件復(fù)制,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05WPF使用Accord實(shí)現(xiàn)屏幕錄制功能
這篇文章主要為大家詳細(xì)介紹了WPF如何使用Accord實(shí)現(xiàn)屏幕錄制,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,感興趣的小伙伴可以了解一下2024-03-03C#獲取變更過的DataTable記錄的實(shí)現(xiàn)方法
這篇文章主要介紹了C#獲取變更過的DataTable記錄的實(shí)現(xiàn)方法,對(duì)初學(xué)者很有學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08C#使用SQLDMO操作數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#使用SQLDMO操作數(shù)據(jù)庫(kù)的方法,實(shí)例分析了基于SQLDMO.dll動(dòng)態(tài)鏈接庫(kù)操作數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下2015-06-06