c# - 将随机生成的颜色分配给值

标签 c# list hyperlink colors

我在连接 2 个代码块时遇到问题。 我有这段代码可以生成 4 种随机颜色。

static Color[] colors = { Color.Red, Color.Green, Color.Yellow, Color.Purple}; // Escolher as cores disponiveis
static Color GetRandomColor()
{
    var random = new Random();
    return colors[random.Next(colors.Length)];
}

我认为代码很好,但如果不好,请告诉我。

我需要将其与此代码关联:

List<Utente> ListaUtente = new List<Utente>();
        ListaUtente.Add(new Utente(22, "Pedro", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0b7b6e6f79644b6c666a626725686466" rel="noreferrer noopener nofollow">[email protected]</a>"));
        ListaUtente.Add(new Utente(22, "Lucas", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e524b5d5f4d070f7e59535f5752105d5153" rel="noreferrer noopener nofollow">[email protected]</a>"));
        ListaUtente.Add(new Utente(22, "Rodrigo", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="07756863756e6068373747606a666e6b2964686a" rel="noreferrer noopener nofollow">[email protected]</a>"));
        ListaUtente.Add(new Utente(22, "Gaspar", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b3c3a282b3a291b3c363a323775383436" rel="noreferrer noopener nofollow">[email protected]</a>"));
        ListaUtente.Add(new Utente(22, "Assis", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3e5f4d4d574d7e59535f5752105d5153" rel="noreferrer noopener nofollow">[email protected]</a>"));

我需要程序输出其中一种随机颜色(紫色、红色、黄色和绿色)并将其写在屏幕上。

类似于:

Lucas - Green
Rodrigo - Red

如何链接这两个 block ?我可以完全访问列表以及“Utente”类属性。

最佳答案

我不确定您在这里寻找什么。您可以将 Color 属性添加到 Utente 类并更新构造函数,以便可以调用它

ListaUtente.Add(new Utente(22, "Pedro", 555444333, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="69190c0d1b06290e04080005470a0604" rel="noreferrer noopener nofollow">[email protected]</a>", GetRandomColor()));

然后

foreach(Utente utente in ListaUtente)
{
    Console.WriteLine(utente.Name + " " + utente.Color);
}

关于c# - 将随机生成的颜色分配给值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43878184/

相关文章:

c# - <customErrors mode=RemoteOnly/> 行为的等效实现

c# - 使用正则表达式删除字典条目

c# - MySQL 命令 - 日期错误

python - 在python中对两个列表进行分组

R 列出数据框中的一列

list - 将标记数组添加到Flutter_Map仅在移动 map 后显示

c# - 检查滚动条是否在数据 GridView 中可见

html - 如何使图像成为 'jump to' 按钮?

javascript - YouTube 视频导航小部件

Excel 超链接 - 跳转到单元格并滚动窗口