Home > database >  How to WriteLine all properties stored in a list with C#?
How to WriteLine all properties stored in a list with C#?

Time:11-02

I have a mini assignment where I have to review the fundamentals of classes and properties. In the context of this code, I am trying to figure out how to print all the properties for each task that is stored in a list.

Here is the code. The commented out code is what I tried so far to print all the properties for each task stored in TaskList.

namespace FunProject
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var person1 = new Person
            {
                FirstName = "Mister",
                LastName = "Programmer",
                Age = 26
            };
            Console.WriteLine(person1.FullName());

            var Task1 = new Task
            {
                TaskName = "read",
                Description = "gain knowledge",
                Id = 1,
                IsDone = true
            };

            var Task2 = new Task
            {
                TaskName = "eat",
                Description = "gain sustenance",
                Id = 2,
                IsDone = false
            };

            person1.TaskList = new List<Task>();
            person1.TaskList.Add(Task1);
            person1.TaskList.Add(Task2);

            //Person1.TaskList.ForEach(i => Console.Write("{0}\t", i));

            //Person1.TaskList.ForEach (x => Console.WriteLine(x));

            //Console.WriteLine(String.Join("{0}\t", Person1.TaskList.ToString()));
            //foreach (Task t in Person1.TaskList)
            //{
            //    Console.WriteLine(t);
            //}

            Console.Read();
        }
    }

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set;}
        public int Age { get; set; }
        public List<Task>TaskList { get; set; }
        public string FullName()
        {
            return ($"{FirstName} {LastName}");
        }
    }

    public class Task
    {
        public int Id { get; set; }
        public string TaskName { get; set;}
        public string Description { get; set; }
        public bool ?IsDone { get; set;}
    }
}

Output should be something like:

Mister Programmer
Current Tasks:
TaskName: read
TaskDescription: gain knowledge
Id: 1
IsDone: true

TaskName: eat
TaskDescription: gain sustenance
Id: 2
IsDone: false  

CodePudding user response:

If you want to resolve this problem what I would do first, I will create task list something like this:

var list = new List<Task>();
list.Add(Task1);
list.Add(Task2);

And then I will add this list to the person that you are creating

var person1 = new Person
            {
                FirstName = "Mister",
                LastName = "Programmer",
                Age = 26,
                TaskList = list
            };

So now you have a person with the list of the tasks. Which is everything you need to create for the output.

So for the output you will need to format something like this:

Console.WriteLine(person1.FirstName    " "    person1.LastName);
Console.WriteLine("Current Tasks:");

person1.TaskList.ForEach(x => Console.WriteLine("TaskName:"   " "   
x.TaskName   "\n"   "TaskDescription:"   " "   x.Description   "\n"   "Id:"   " "   x.Id   "\n"   "IsDone:"   " "   x.IsDone.ToString().ToLower()));

Console.Read();

But if you want to have the properties names without writing the properties in the Console.WriteLine("TaskDescription") or etc., you will need to use System.Reflection:

Type t = typeof(Person);
PropertyInfo[] properties = t.GetProperties();
foreach (PropertyInfo prop in properties)
{
 Console.WriteLine(prop.Name);
 if (prop.Name.Equals("TaskList"))
 {
  Type ts = typeof(Task);
  PropertyInfo[] propertiesTask = ts.GetProperties();
  foreach (var propTask in propertiesTask)
  {
    Console.WriteLine(propTask.Name);
  }
 }
}

CodePudding user response:

I would use System.Reflection for this...

foreach (var t in person1.TaskList)
{
    foreach (var prop in t.GetType().GetProperties())
    {
        Console.WriteLine($"{prop.Name}: {prop.GetValue(t, null).ToString()}");
    }
    Console.WriteLine("\n");
}
  • Related