当前位置:首页C# > 正文

C# 在Color[] colorTable中快速找到Color的索引位置

作者:野牛程序员:2023-12-25 12:43:48C#阅读 2586

可以使用Array.IndexOf方法来在Color[] colorTable中查找特定的Color对象,并返回其索引位置。以下是一个简单的示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Drawing;
 
class Program
{
    static void Main()
    {
        // 假设有一个Color数组
        Color[] colorTable = { Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Orange };
 
        // 要查找的Color对象
        Color targetColor = Color.Blue;
 
        // 使用Array.IndexOf方法查找Color对象的索引位置
        int index = Array.IndexOf(colorTable, targetColor);
 
        // 输出结果
        Console.WriteLine($"目标颜色 {targetColor} 的索引位置是: {index}");
    }
}

在这个例子中,定义了一个Color数组colorTable,并使用Array.IndexOf方法查找targetColor在数组中的索引位置。


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击