Reputation: 197
I am a newbie for C#
Current code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Basic1
{
class Program
{
static void Main(string[] args)
{
List<Employee> empList = new List<Employee>()
{
new Employee { FName = "Bill", LName = "Lucas", Id = 101 },
new Employee { FName = "Steve", LName = "Lanes", Id = 102 },
new Employee { FName = "Jeff", LName = "Clark", Id = 103 },
new Employee { FName = "Joe", LName = "Thomas", Id = 104 },
new Employee { FName = "Joe", LName = "Wang", Id = 105 },
new Employee { FName = "Steven", LName = "Holmes", Id = 106 },
new Employee { FName = "Mark", LName = "Lark", Id = 107 },
new Employee { FName = "Larry", LName = "Good", Id = 108 },
new Employee { FName = "Tim", LName = "Whiles", Id = 109 },
new Employee { FName = "Jerry", LName = "Lee", Id = 110 }
};
List<Employee> empListJoe = new List<Employee>();
foreach (Employee employee in empList)
{
if (employee.FName == "Joe")
{
empListJoe.Add(employee);
}
}
Console.WriteLine(empListJoe);
Console.ReadLine();
}
}
public class Employee
{
public string FName { get; set; }
public string LName { get; set; }
public int Id { get; set; }
}
}
So I created a new list with all employees with the first name "Joe"
.
However, it seems I can't print the list of items properly, instead I get an output like this:
'System.Collections.Generic.List`1[Basic1.Employee]
or merely two lines of
Basic1.Employee.
I have tried each of this, but stil get an generic output than the actual list of items.
Console.WriteLine(empListJoe);
Console.WriteLine(String.Join(" \n", empListJoe));
empListJoe.ForEach(Console.WriteLine);
Console.WriteLine(empListJoe.ToString());
Btw, this one works:
Console.WriteLine($"Employee Name: {employee.FName} {employee.LName}, Employee ID: {employee.Id}");
But I am trying to print from the new list. I guess I am missing something basic.
Thanks.
Upvotes: 1
Views: 2735
Reputation:
I think you should ask yourself two questions:
The answer to both is simply NO.
C# only knows that each object has ToString() method that can be overriden. It calls it when you directly try to print the list and you see the default output.
You have to write the code to print just one element (wirte a function / method). Then you have to manually invoke this code (function / method) for each element in your list to print it - foreach loop will do fine.
If you are familiar with OOP then you can just override ToString method for your custom class.
Upvotes: 0
Reputation: 415810
Override the ToString()
method in your Employee type:
public class Employee
{
public string FName { get; set; }
public string LName { get; set; }
public int Id { get; set; }
public overrides string ToString()
{
return $"Name: {FName} {LName}, Id: {Id}";
}
}
.Net doesn't know how to convert your random type into a string, but overriding the ToString()
method will give it a better default when nothing else is available.
Once you have that, you must also understand that .Net will not automatically convert collections to strings. You have to iterate the collection yourself and output the string value for each item. There are many ways to do this, including basic for
or foreach
loops and the string.Join
method.
Upvotes: 1
Reputation: 36
List<Employee> empListJoe = new List<Employee>();
foreach (Employee employee in empList)
{
if (employee.FName == "Joe")
{
empListJoe.Add(employee);
}
}
foreach (Employee employeee in empListJoe)
{
Console.WriteLine($"{employeee.FName} {employeee.LName}");
}
Console.ReadLine();
Or you could just use the first loop, put Console.WriteLine...line under the empListJoe.Add(employee); line.
Upvotes: 1
Reputation: 37367
All this:
Console.WriteLine(empListJoe);
Console.WriteLine(String.Join(" \n", empListJoe));
empListJoe.ForEach(Console.WriteLine);
Console.WriteLine(empListJoe.ToString());
use implictly or explicitly ToString
method of your class. Since you didn't override it, you get unexpected errors (default implementation return class name). You could override this method in your class, like this:
public override string ToString()
{
return $"{FName} {LName}";
}
After that, your best guess to display your list is:
Console.WriteLine(String.Join(" \n", empListJoe));
Alternatively, you could use LINQ method Select
to have list of strings of your desired format, like this:
Console.WriteLine(String.Join(" \n", empListJoe.Select(e => $"{e.FName} {e.LName}")).ToArray());
You could also use LINQ method to filter out employees based on name. Putting all these together, working code could be:
var empListJoe = empList
.Where(e => e.FName = "Joe")
.Select(e => $"{e.FName} {e.LName}"))
.ToArray();
Console.WriteLine(String.Join(" \n", empListJoe));
Upvotes: 2