C#从另一个类打印列表

时间:2015-07-18 10:05:42

标签: c# list class

我是C#的新手,我正在尝试打印已在另一个类中创建的列表。该列表由枚举器中的值以及整数和字符串组成。

以下是列表的代码:

using System.Collections.Generic;

namespace Risk
{
    public class RiskService
    {
        private readonly Resource _matt = new Resource { Id = 1, Name = "Matt Sharpe" };
        private readonly Resource _john = new Resource { Id = 2, Name = "John Hillhouse" };
        private readonly Resource _julian = new Resource { Id = 3, Name = "Julian Jelfs" };
        private readonly Resource _darren = new Resource { Id = 4, Name = "Darren Thorpe" };
        private readonly Resource _jonm = new Resource { Id = 5, Name = "Jon Moore" };

        public List<Risk> GetRisks()
        {
            var risks = new List<Risk>();
            risks.Add(new Risk { Id = 1, Owner = _matt, RiskScore = 5, Status = RiskStatus.Approved, Title = "Lack Build Capacity In Dockyard" });
            risks.Add(new Risk { Id = 2, Owner = _john, RiskScore = 10, Status = RiskStatus.Unapproved, Title = "Small scale fire in warehouse" });
            risks.Add(new Risk { Id = 3, Owner = _matt, RiskScore = 17, Status = RiskStatus.Mitigated, Title = "Contract delays" });
            risks.Add(new Risk { Id = 4, Owner = _julian, RiskScore = 23, Status = RiskStatus.Open, Title = "Supplier insolvency" });
            risks.Add(new Risk { Id = 5, Owner = _darren, RiskScore = 13, Status = RiskStatus.Closed, Title = "Loss of key staff" });
            risks.Add(new Risk { Id = 6, Owner = _jonm, RiskScore = 97, Status = RiskStatus.Open, Title = "Fire in plant" });
            risks.Add(new Risk { Id = 7, Owner = _jonm, RiskScore = 97, Status = RiskStatus.Open, Title = "Fire in backup plant" });
            risks.Add(new Risk { Id = 8, Owner = _jonm, RiskScore = 45, Status = RiskStatus.Unapproved, Title = "Disaster recovery doesn't cover fire" });
            risks.Add(new Risk { Id = 9, Owner = _darren, RiskScore = 36, Status = RiskStatus.Approved, Title = "Component fails to meet performance" });
            risks.Add(new Risk { Id = 10, Owner = _john, RiskScore = 36, Status = RiskStatus.Approved, Title = "Component fails to meet performance" });
            return risks;
        }
    }

    public class Risk
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public Resource Owner { get; set; }
        public RiskStatus Status { get; set; }
        public int RiskScore { get; set; }
    }

    public class Resource
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public enum RiskStatus
    {
        Unapproved,
        Approved,
        Open,
        Closed,
        Mitigated
    }
}

这是我要打印列表的类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Risk;

namespace printRisk
{
    public class Program
    {

        RiskService myRisk = new RiskService();

        public void Risks()
        {
            List<Risk> riskList = myRisk.GetRisks();
        }

        static void Main(string[] args)
        {
        }
    }
}

4 个答案:

答案 0 :(得分:2)

您可以通过使用foreach遍历列表来执行此操作:

static void Main(string[] args)
{
    foreach (var risk in myRisk.GetRisks()) {
        Console.WriteLine("ID: " + risk.Id + ", Title: " + risk.Title); 
    }
}

答案 1 :(得分:1)

你也可以通过覆盖Risk类中的ToString方法来即兴创作Glorfindel提供的解决方案......

public class Risk
{
    public int Id { get; set; }
    public string Title { get; set; }
    public Resource Owner { get; set; }
    public RiskStatus Status { get; set; }
    public int RiskScore { get; set; }

    public override string ToString()
    {
        return string.Format("Risk Id: {0}, Risk Title: {1}, Owner Id: {2}, Owner Name: {3}, Risk Status: {4}, Risk score: {5}",
                                this.Id, this.Title, this.Owner.Id, this.Owner.Name, this.Status.ToString(), this.RiskScore);
    }
}

现在您可以使用for循环打印这些内容,如下所示

foreach (Risk risk in riskList)
{
    Console.WriteLine(risk.ToString());
}

答案 2 :(得分:0)

Console.WriteLine(string combindedString = string.Join( ", ", myList.ToArray() ))

应该解决您的问题,也可以看到一些方法也改善您的代码结构

答案 3 :(得分:0)

foreach (var r in riskList) {
    Console.WriteLine(String.Format("Id = {0} Name = {1}",r.Id, r.Name));
 }