C#使用泛型方法实现操作不同数据类型的数组

 更新时间:2024年02月22日 09:41:13   作者:wenchm  
这篇文章主要为大家详细介绍了C#如何使用一个泛型方法对不同类型的数组进行操作,文中的示例代码讲解详细,感兴趣的小伙伴可以学习一下

再发一个泛型方法的示例。

一、泛型方法及其存在的意义

实际应用中,查找或遍历数组中的值时,有时因为数组类型的不同,需要对不同的数组进行操作,那么,可以使用同一种方法对不同类型的数组进行操作吗?答案是肯定的,本实例将使用一个泛型方法对不同类型的数组进行操作。

二 、实例

1.源码

// 使用一个泛型方法对不同类型的数组进行操作
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 = "整数";
            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 = "泛型查找不同数组中的值";
        }
        /// <summary>
        /// 声明一个字符串类型的数组
        /// 调用泛型方法,查找字符串“三”在数组中的索引
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            string[] str = ["一", "二", "三", "四", "五", "六", "七", "八", "九"];
            MessageBox.Show(Finder.Find(str, "三").ToString());
        }
        /// <summary>
        /// 声明一个整数类型的数组
        /// 调用泛型方法,查找数字5在数组中的索引
        /// </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>
        /// 声明一个布尔类型的数组
        /// 调用泛型方法,查找false在数组中的索引
        /// </summary>
        private void Button3_Click(object? sender, EventArgs e)
        {
            bool[] IntArray = [true, false];
            MessageBox.Show(Finder.Find(IntArray, false).ToString());
        }
        /// <summary>
        /// 定义一个类
        /// 在类中,定义一个泛型方法,用来查找指定值在数组中的索引
        /// 遍历泛型数组
        /// </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;               //返回指定值在数组中的索引
                    }
                }
                return -1;                      //如果没有找到,返回-1
            }
        }
    }
}

2.生成效果

到此这篇关于C#使用泛型方法实现操作不同数据类型的数组的文章就介绍到这了,更多相关C#泛型方法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论