gpt4 book ai didi

c# - list.ToString 没有显示正确的输出

转载 作者:行者123 更新时间:2023-11-30 19:39:54 25 4
gpt4 key购买 nike

我正在尝试将列表中的项目转换为字符串。但每次我转换它或显示它时,它都显示为“TwitchIrcChar.user”。如果有人可以帮助解决这个问题,那将非常有帮助。对不起,如果菜鸟问题,但我是列表的新手。我试过使用 convert.ToString 和 userlist.tostring。两者都给出了相同的输出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace TwitchIrcChat
{
class User
{

static Random random = new Random();
public string UserName { get; set; }
public SolidColorBrush Color { get; set; }
public bool IsMod { get; set; }

public User(string username)
{
IsMod = false;
UserName = username;
randomColor();
}

public void setColor(SolidColorBrush color)
{
Color = color;
}

private void randomColor()
{
var temp = Brushes.White;
int randomColor = random.Next(0, 10);
switch (randomColor)
{
case 0:
temp = Brushes.Blue;
break;
case 1:
temp = Brushes.Green;
break;
case 2:
temp = Brushes.Red;
break;
case 3:
temp = Brushes.Purple;
break;
case 4:
temp = Brushes.Orange;
break;
case 5:
temp = Brushes.Yellow;
break;
case 6:
temp = Brushes.Gold;
break;
case 7:
temp = Brushes.Teal;
break;
case 8:
temp = Brushes.Cyan;
break;
case 9:
temp = Brushes.LightBlue;
break;
case 10:
temp = Brushes.Pink;
break;
}
Color = temp;
}
}

class UserList
{
public moderation q = new moderation();
public List<User> userList { get; set; }

public UserList()
{
userList = new List<User>();
}

public void Add(string userName)
{
bool isInList = false;
foreach (var item in userList)
{
if (item.UserName.Equals(userName))
{
isInList = true;
break;
}
}
if (!isInList)
{
var tempUser = new User(userName);
userList.Add(tempUser);
}
}

public void Remove(string userName)
{
int userLocation = -1;
for (int i = 0; i < userList.Count; i++)
{
if (userName.Equals(userList[i].UserName))
{
userLocation = i;
break;
}
}
try
{
userList.RemoveAt(userLocation);
}
catch (Exception)
{
}
}

public SolidColorBrush getColor(string username)
{
var temp = Brushes.White;
foreach (var item in userList)
{
if (item.UserName.Equals(username))
{
temp = item.Color;
}
}
return temp;
}

public void setColor(string username, string color)
{
if (userList.Count(s => s.UserName == username) == 0)
{
Add(username);
}
var user = userList.First(s => s.UserName == username);
var converter = new BrushConverter();
var brush = (SolidColorBrush)converter.ConvertFromString(color);
user.Color = brush;
}

public void Clear()
{
userList.Clear();
}

public void list()
{
Console.WriteLine("qweqweqweqwe");
for (int i = 0; i < userList.Count; i++) // Loop through List with for
{
Console.WriteLine(userList[i].ToString());
Console.WriteLine("qweqweqweqwe");

}

}

public void AddMod(string userName)
{
foreach (var item in userList)
{
//string a = item.ToString();
//q.writeToFile(a);
if (item.UserName.Equals(userName))
{

item.IsMod = true;
}
}
}

}
}

最佳答案

您可以像其他人建议的那样覆盖 ToString,或者如果 UserName 是您所希望的,您就可以这样做。

Console.WriteLine(userList[i].UserName.ToString());

Console.WriteLine(userList[i].UserName);

因为它已经是一个字符串

关于c# - list.ToString 没有显示正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25791442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com