Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I Want to get an Element from my List<Func<Object>> at a specified index?

I Want to get an Element from my List<Func> at a specified index ?

public classe Exemple
{
    private readonly List<Func<Type>> _element ;

    public Exemple(List<Func<Type>> element)
    {
        _element = element;
    }

    public void Method(int index)
    {
        Type type= _element();
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The reason the statement _element() is invalid is because _element is a list and not a Func, therefore lacking a () or .Invoke(). You need to use the indexer of List<> to retrieve the Func that you are trying to invoke.

public void Method(int index)
{
    Type type = _element[index]();
    
    /* do stuff with 'type' */
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading