Trying to understand what this mean...types(lstTypes[i].AvailableDatatype)

I'm having a hard time understanding this simple line of

 : types(lstTypes[i].AvailableDatatype);

Open in new window



using System;
using System.Collections.Generic;

namespace ConsoleApplicationTest
{
    class Program
    {
        static void Main(string[] args)
        {

            List<DotNet> lstTypes = new List<DotNet>();
            DotNet bltypes = new DotNet();
            bltypes.AvailableDatatype = "bool";
            lstTypes.Add(bltypes);


            DotNet strTypes = new DotNet();
            strTypes.AvailableDatatype = "string";
            lstTypes.Add(strTypes);

            DotNet intTypes = new DotNet();
            intTypes.AvailableDatatype = "int";
            lstTypes.Add(intTypes);

            DotNet decTypes = new DotNet();
            decTypes.AvailableDatatype = "decimal";
            lstTypes.Add(decTypes);


            types += x =>
            {
                Console.WriteLine(x);
                Console.ReadLine();
            };

            for (int i = 0; i <= lstTypes.Count - 1; i++)
            {
                types(lstTypes[i].AvailableDatatype);
            }




        }

        static event Action<string> types;



    }


    public class DotNet
    {
        public string AvailableDatatype { get; set; }
    }


}

Open in new window

yguyon28Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nitin SontakkeDeveloperCommented:
Watch carefully and you will understand it clearly.

If you look for types in the code above, you will notice that there is a static event declared at line number 46.

You will also notice that an inline function is attached to this event at line number between 30-34.

The line you have mentioned simply calls that event handler, which in turn prints the type passed as string.

Here is another implementation which you might be comfortable with:

First define a method which corresponds with the event signature.

        public static void PrintType(String str)
        {
          Console.WriteLine("New implementation " + str);
          Console.ReadLine();
        }

Open in new window


Then simply register it with the event as you would normally do:

types += PrintType;

Open in new window


Then run the code again and notice what happens!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard LeeSoftware EnthusiastCommented:
I would recommend researching the following:

  • Events including Subscribing & Unsubscribing
  • Action & Func
  • Lambda expressions

This, including the answer above will help you understand what is going on.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.