Print List of objects to Console
I would override ToString
in your Listobj
class.
public class Listobj
{
private int age;
private string name;
public int Age
{
get { return age; }
set { age = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public override string ToString()
{
return "Person: " + Name + " " + Age;
}
}
Then you can print like so:
foreach (var item in newlist.OrderBy(person => person.Age)) Console.WriteLine(item);
C# print list of items for object with 3 properties
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.
Print out 1 object in a List of objects in C#
You have two ways.
Access the properties separately
var element = myList.ElementAt(1);
Console.WriteLine("ID:{0}, Name:{1}", element.ID, element.Name);
or overload the
ToString()
for the classpublic class myObject
{
public int ID { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("ID:{0}, Name:{1}", ID, Name);
}
}so you can
Console.WriteLine(myList.ElementAt(1));
How to print a list of objects with properties?
First, let each class (Dipendente
) instance speak for itself, .ToString() is the very place for this:
Returns a string that represents the current object.
...It converts an object to its string representation so that it is suitable for display...
public class Dipendente
{
...
public override string ToString()
{
// Put here all the fields / properties you mant to see in the desired format
// Here we have "Id = 123; Nome = John; Cognome = Smith" format
return string.Join("; ",
$"Id = {Id}",
$"Nome = {Nome}",
$"Cognome = {Cognome}"
);
}
}
then you can put
foreach (Dipendente dip in lstDipendenti)
{
// Or even System.Diagnostics.Debug.WriteLine(dip);
System.Diagnostics.Debug.WriteLine(dip.ToString());
}
Print info stored in a list of classes to console
you need to override the ToString method in the Person
class and handle the output there.
The Object.ToString method returns:
A string that represents the current object.
This is basically a string representation of your object (Person). By default it will return the full classname including the namespace. When you pass an object like a person to Console.WriteLine
(which expects a string
as parameter) it will call automatically the ToString
method which every object in the framework inherits from Object
. This should explain your output:
System.Collections.Generic.List
when you used Console.WriteLine(people.ToString());
. It called the ToString
method that List
inherits from Object
and printed the full name.
If you override it in your class it will take your version and you can determine how the output is supposed to look like
class Person
{
public string name;
public int age;
public double height;
public Person(string name, int age, double height)
{
this.name = name;
this.age = age;
this.height = height;
}
public override string ToString()
{
return $"Name: {name} Age: {age} Height: {height}";
}
}
now you can use string.Join
Console.WriteLine(String.Join(Environment.NewLine, people));
Print list Object c#
Printing a user defined object will not work this way. As you could read in the documentation of Console.WriteLine it calls the "ToString()" method of the object to print and the standard implementation prints the name of the class of this object.
If you want to output the contents of your object you have to override the ToString() method.
Example:
public class Pessoa {
....
override ToString() {
return $"ID = {ID}, IIdade = {Idade}, ....";
}
}
Unable to print whole list to console
You are trying to print the object directly to console try iterating over list and print them wherever returned.
for (var item in returned)
Console.WriteLine(item)
if using a custom Type. keep in mind that you have defined its to string method.
Related Topics
How to Get the Correct Ip from Http_X_Forwarded_For If It Contains Multiple Ip Addresses
C#: How to Get First Char of a String
Adding or Subtracting Color from an Image in a Picturebox Using C#
How to Get the Current Date Without the Time
String.Isnullorwhitespace in Linq Expression
How to Check If a Property Exists on a Dynamic Anonymous Type in C#
Getting Current Directory in .Net Web Application
How to Use a Dataadapter with Stored Procedure and Parameter
How to Display a Windows Form in Full Screen on Top of the Taskbar
What Is the Command to Exit a Console Application in C#
Differencebetween Streamwriter.Flush() and Streamwriter.Close()
How to Add Data Table with Legend Keys to a Ms Chart in C#
Httpwebrequest & Native Gzip Compression
Datetime2' Error When Using Entity Framework in VS 2010 .Net 4.0