【问题标题】:C# Calling variables from a class and assigning to a listC#从类中调用变量并分配给列表
【发布时间】:2017-09-20 23:14:30
【问题描述】:

我想知道一种将类中的变量分配给另一个类中的列表的方法。我有以下代码,所以

public class Names
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
       var playerNames = new List<Names>();
       playerNames = getPlayerList(ref totalPlayers);
       //??
    }
}

totalPlayers 是来自另一段代码的用户输入整数。

getPlayerList 是一段代码,它要求用户输入一些名称,然后使用for 循环将其分配给Names 类中的Name'。

到目前为止一切正常,我只想知道如何将Names 类的Name 中的变量分配给Main 中的列表。

现在我将所有变量分配给字符串,然后将这些字符串添加到列表中,但这最终会导致大量浪费代码。

string player1 = playerNames[0].Name, player2 = playerNames[1].Name;
// and so on
List<string> players = new List<string>();
players.Add(player1); 
players.Add(player2); // and so on

提前致谢。

【问题讨论】:

  • 你知道loop是什么吗?
  • 不清楚你需要什么样的答案。显而易见的答案是players.AddRange(playerNames.Select(p =&gt; p.Name));,它将Name 属性值作为单个对象引用复制到players 列表。但是,你没有提供一个很好的minimal reproducible example 来展示真实的代码,而且还不清楚你到底遇到了什么困难。给你一行代码不一定是你想要或需要的。请澄清您的问题并加以改进。另请参阅How to Ask,了解有关以清晰、可回答的方式提出问题的建议。
  • 您的代码无法编译 - totalPlayers 是什么?如果您将playerNames 分配给调用getPlayerList 的结果,则无需在声明时创建一个新的空List

标签: c# list class


【解决方案1】:

您可以使用 LINQ 创建列表:

static void Main(string[] args) {
    var players = getPlayerList(ref totalPlayers);
    var playerNames = players.Select(p => p.Name).ToList();
}

否则你会遍历你的List:

static void Main(string[] args) {
    var players = getPlayerList(ref totalPlayers);
    var playerNames = new List<string>();
    foreach (var p in players)
        playerNames.Add(p.Name);
}

【讨论】:

  • 谢谢,正是我想要的!
【解决方案2】:

您可以使用循环来遍历您的列表。

static void Main( string[] args )
{
   var playerNames = new List<Names>(); 
   //I assume that getPlayerList() is a function that will populate your list with objects of type Names
   playerNames = getPlayerList( ref totalPlayers );

   //you can iterate through the entire list of objects and retrieve whatever properties you need 
    foreach ( var p in players )
    {
       Console.WriteLine( "Id " + p.Id );
       Console.WriteLine( "Name " + p.Name );
    }

    //Or create just a list of names using the Name property on 'Names'
    var listOfNames = new list<string>();
    foreach ( var p in players )
    {
       //and populate using the Name property from each 'Names' item
       //in your list
       listOfNames.add( p.Name );
    }

    //This Will print the first index in the list
    Console.WriteLine( listOfNames[0] );
}

不过,正如 NetMage 已经指出的那样,在这里使用 linq 会更简洁。


更新

当你说

getPlayerList 是一段要求用户输入的代码 一些名称,然后分配给“名称”类中的“名称” 使用 for 循环。

为什么不使用dictionary 而不是名称列表呢?看起来您想要做的只是将名称 (string) 与唯一 ID (int) 一起存储。

Dictionary<int, string> playerNames = new Dictionary<int, string>();
playerNames,add( 0, "John Smith" );
playerNames,add( 1, "Wil Wheaton" );
playerNames,add( 2, "Jon Skeet" );

那么你只需要遍历字典就可以了

foreach( var p in playerNames )
{
   Console.WriteLine( "Id " + p.Key );
   Console.WriteLine( "Name " + p.Value );
}

如果您发现自己有很多用户并且知道他们的确切 ID,那么搜索字典会更快 很多

【讨论】:

  • 这是一个作业,需要使用课程,我想不出任何其他地方可以使用课程。但是谢谢,这绝对有帮助,我会在其他编码项目中记住这一点。
猜你喜欢
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 2021-02-22
  • 2018-01-22
  • 1970-01-01
相关资源
最近更新 更多