white smartphone

Using Implicit Method Group Conversion in C#

This post will explain what exactly is Implicit Method Group Conversion and how to use them in your C# application.

Its great to learn something new everyday that keeps me updated in .NET which always makes me feel fresh . With every topic that i come across everyday , it make me feel that i am still a fresher .

Today , i got to know something about Implicit Method Group Conversion in C# .

Well , if you are still looking to know whet it is , the example below will clearly explain you this .

Assume that the List contains the Names Senthil Kumar,Developer,Trivium like below.

List<string> LstNames = new List<string>();

LstNames.Add("Senthil Kumar");

LstNames.Add("Developer");

LstNames.Add("Trivium");

How will you display the Names in the list ?

Most Developers (including me ) will end up doing something like this .

foreach (string str in LstNames)
{

     Console.WriteLine(str);

}

or

for (int i = 0; i < LstNames.Count ; i++)
{

     Console.WriteLine(LstNames[i]);

}

Now , the same can be achieved with just one line of code .

LstNames.ForEach(str => Console.WriteLine(str));

Further , i can remove the lambda expression and display the same names with the Implicit Method Group Conversion like below

LstNames.ForEach(Console.WriteLine);

This results in the same output as the one as above.

List<string> LstNames = new List<string>();

LstNames.Add("Senthil Kumar");

LstNames.Add("Developer");

LstNames.Add("Trivium");

LstNames.ForEach(Console.WriteLine);

    5 Comments

  1. Parwej
    March 21, 2011
    Reply

    Oh it’s really great knowledge sharing. Thanks 🙂

  2. Ryukk
    March 25, 2011
    Reply

    You have error in post:

    Correct variant:
    for (int i = 0; i < LstNames.Count; i++)
    {
    Console.WriteLine(LstNames[i]);
    }

    or

    for (int i = 0; i <= LstNames.Count – 1; i++)
    {
    Console.WriteLine(LstNames[i]);
    }

  3. March 25, 2011
    Reply

    Error .. i dont see anything… Did you mean usage of foreach ??

  4. Ryukk
    March 25, 2011
    Reply

    your example:

    List LstNames = new List();

    LstNames.Add(“Senthil Kumar”);

    LstNames.Add(“Developer”);

    LstNames.Add(“Trivium”);

    for (int i = 0; i < LstNames.Count – 1; i++)
    {
    Console.WriteLine(LstNames[i]);
    }

    Result:
    Senthil Kumar
    Developer

    Missing: Trivium

    The correct iteration of list is:

    for (int i = 0; i < LstNames.Count; i++)
    {
    Console.WriteLine(LstNames[i]);
    }

  5. March 27, 2011
    Reply

    Thanks Ryukk , i have updated it .

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...