C#使用泛型方法實(shí)現(xiàn)操作不同數(shù)據(jù)類型的數(shù)組
再發(fā)一個(gè)泛型方法的示例。
一、泛型方法及其存在的意義
實(shí)際應(yīng)用中,查找或遍歷數(shù)組中的值時(shí),有時(shí)因?yàn)閿?shù)組類型的不同,需要對不同的數(shù)組進(jìn)行操作,那么,可以使用同一種方法對不同類型的數(shù)組進(jìn)行操作嗎?答案是肯定的,本實(shí)例將使用一個(gè)泛型方法對不同類型的數(shù)組進(jìn)行操作。
二 、實(shí)例
1.源碼
// 使用一個(gè)泛型方法對不同類型的數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用Task實(shí)現(xiàn)執(zhí)行并行任務(wù)的原理的示例詳解
Task是一個(gè)表示異步操作的類,它提供了一種簡單、輕量級的方式來創(chuàng)建多線程應(yīng)用程序。本文就來和大家聊聊在C#中如何使用Task執(zhí)行并行任務(wù)吧2023-04-04
WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法
這篇文章主要介紹了WinForm實(shí)現(xiàn)移除控件某個(gè)事件的方法,對C#初學(xué)者有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08

