Reputation: 109
Hello let me describe i don't have any errors in my code , i am just asking a very basic question here RELATED TO list collection class let see i have a class called customer
class customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
}
in my main method i created an array of customer and initialize those properties that is present in my customer class
static void Main(string[] args)
{
customer[] customers = new customer[3];
customers[0] = new customer
{
Id = 1,
Name = "A",
Salary = 30000
};
customers[1]=new customer
{
Id = 2,
Name = " B",
Salary = 50000
};
customers[2] = new customer
{
Id = 3,
Name = "C",
Salary = 90000
};
List<customer> Cust= new List<customer>(2);
Cust.Add(customers[0]);
Cust.Add(customers[1]);
Cust.Add(customers[2]);
for (int i = 0; i < Cust.Count; i++)
{
customer C = Cust[i];
Console.WriteLine("Id = {0} & Name = {1} & Salary = {2}",C.Id,C.Name,C.Salary);
}
Console.ReadLine();
}
Okay! so this code is working so perfectly nice , but my question is that at last we created a list called cust and add all the custemers in to it , so why is it necessary to make another object with type customer as i did in for loop
customer C = Cust[i];
why can i don't call my code like this
console.WriteLine{Cust[i]}
As far as i know when we create object of the class than we can easily acces the code inside that class with that instance variable . so why not here?
Upvotes: 0
Views: 76
Reputation: 37020
To do what you were asking to do, you would first need to override the ToString()
method on your class. The ToString()
method will be called implicitly by Console.WriteLine
:
class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public int Salary { get; set; }
public override string ToString()
{
return string.Format("Id = {0} & Name = {1} & Salary = {2}", Id, Name, Salary);
}
}
Now that you have a default way to represent a customer as a string, you can do exactly what you were asking to do:
for (int i = 0; i < Cust.Count; i++)
{
Console.WriteLine(Cust[i]);
}
And, now that you have this, you can do it in an even easier way. The following will implicitly pass each item to Console.WriteLine()
, which will call the item's ToString()
method:
customers.ForEach(Console.WriteLine);
If for some reason you DON'T want to override the ToString
method, you can still access the instance properties using the index without creating a new object, like so:
for (int i = 0; i < Cust.Count; i++)
{
Console.WriteLine("Id = {0} & Name = {1} & Salary = {2}",
Cust[i].Id, Cust[i].Name, Cust[i].Salary);
}
Upvotes: 0
Reputation: 4135
In your for loop, you're not creating a new customer, you're just creating a reference to the existing one:
for (int i = 0; i < Cust.Count; i++)
{
customer C = Cust[i]; //<- not new, just a reference to the customer at index
Console.WriteLine("Id = {0} & Name = {1} & Salary = {2}",C.Id,C.Name,C.Salary);
}
A more concise way to loop is to use foreach instead of for (NOTE: using C# 6.0 string interpolation):
foreach(var c in Cust)
Console.WriteLine($"Id = {c.Id} & Name = {c.Name} & Salary = {c.Salary}");
Upvotes: 3