I have a List that contains all databases names. I have to display the items contained in that list in the Console (using Console.WriteLine()). How can I achieve this?
I have a List that contains all databases names. I have to display the items contained in that list in the Console (using Console.WriteLine()). How can I achieve this?
Actually you can do it pretty simple, since the list have a ForEach method and since you can pass in Console.WriteLine as a method group. The compiler will then use an implicit conversion to convert the method group to, in this case, an Action<int> and pick the most specific method from the group, in this case Console.WriteLine(int):
var list = new List<int>(Enumerable.Range(0, 50));
list.ForEach(Console.WriteLine);
Works with strings too =)
To be utterly pedantic (and I'm not suggesting a change to your answer - just commenting for the sake of interest) Console.WriteLine is a method group. The compiler then uses an implicit conversion from the method group to Action<int>, picking the most specific method (Console.WriteLine(int) in this case).
Assuming the items override ToString appropriately:
public void WriteToConsole(IEnumerable items)
{
foreach (object o in items)
{
Console.WriteLine(o);
}
}
(There'd be no advantage in using generics in this loop - we'd end up calling Console.WriteLine(object) anyway, so it would still box just as it does in the foreach part in this case.)
EDIT: The answers using List<T>.ForEach are very good.
My loop above is more flexible in the case where you have an arbitrary sequence (e.g. as the result of a LINQ expression), but if you definitely have a List<T> I'd say that List<T>.ForEach is a better option.
One advantage of List<T>.ForEach is that if you have a concrete list type, it will use the most appropriate overload. For example:
List<int> integers = new List<int> { 1, 2, 3 };
List<string> strings = new List<string> { "a", "b", "c" };
integers.ForEach(Console.WriteLine);
strings.ForEach(Console.WriteLine);
When writing out the integers, this will use Console.WriteLine(int), whereas when writing out the strings it will use Console.WriteLine(string). If no specific overload is available (or if you're just using a generic List<T> and the compiler doesn't know what T is) it will use Console.WriteLine(object).
Note the use of Console.WriteLine as a method group, by the way. This is more concise than using a lambda expression, and actually slightly more efficient (as the delegate will just be a call to Console.WriteLine, rather than a call to a method which in turn just calls Console.WriteLine).
You can also use List's inbuilt foreach, such as:
List<T>.ForEach(item => Console.Write(item));
This code also runs significantly faster!
The above code also makes you able to manipulate Console.WriteLine, such as doing:
List<T>.ForEach(item => Console.Write(item + ",")); //Put a,b etc.
Assume that we need to view some data in command prompt which are coming from a database table. First we create a list. Team_Details is my property class.
List<Team_Details> teamDetails = new List<Team_Details>();
Then you can connect to the database and do the data retrieving part and save it to the list as follows.
string connetionString = "Data Source=.;Initial Catalog=your DB name;Integrated Security=True;MultipleActiveResultSets=True";
using (SqlConnection conn = new SqlConnection(connetionString)){
string getTeamDetailsQuery = "select * from Team";
conn.Open();
using (SqlCommand cmd = new SqlCommand(getTeamDetailsQuery, conn))
{
SqlDataReader rdr = cmd.ExecuteReader();
{
teamDetails.Add(new Team_Details
{
Team_Name = rdr.GetString(rdr.GetOrdinal("Team_Name")),
Team_Lead = rdr.GetString(rdr.GetOrdinal("Team_Lead")),
});
}
Then you can print this list in command prompt as follows.
foreach (Team_Details i in teamDetails)
{
Console.WriteLine(i.Team_Name);
Console.WriteLine(i.Team_Lead);
}