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

c# return both list of string and list of generic type

I need to return multiple items from a function. How can I do that?

public List<string> GetInfo()
{
    var result = new List<string>();

    return result;
}

How can I return something like this? Basically List of string and maybe also a list of generic type.

public List<string> GetInfo()
{
    var result = new List<string>();

    return result and someType;
}

public class someType
{
    public List<SomeOthertype> someOthertype { get; set; }
}

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

>Solution :

If I understand you right, you can try using named tuples:

public (List<string> result, List<SomeOthertype> other) GetInfo()
{
    var result = new List<string>();

    someType instance = new someType();

    //TODO: put relevant code here

    // we return both result and someOthertype in one tuple 
    return (result, instance.someOthertype);
}

Usage:

var info = GetInfo();

var result = info.result;
var other = info.other;
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