Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a function return IEnumerable<string> instead of a string in C#

Tags:

c#

I have the following function that returns a string:

public static string GetFormattedErrorMessage(this Exception e)
{
    if (e == null)
    {
        throw new ArgumentNullException("e");
    }

    var exError = e.Message;
    if (e.InnerException != null)
    {
        exError += "<br>" + e.InnerException.Message;
        if (e.InnerException.InnerException != null)
        {
            exError += "<br>" + e.InnerException.InnerException.Message;
        }
    }

    return exError;
}

Can someone help and tell me how I could make this same function return a IEnumerable<string> with just one element?

like image 837
Samantha J T Star Avatar asked Dec 16 '22 23:12

Samantha J T Star


1 Answers

public static IEnumerable<string> GetFormattedErrorMessage(this Exception e)
        {
            if (e == null)
            {
                throw new ArgumentNullException("e");
            }

            var exError = e.Message;
            if (e.InnerException != null)
            {
                exError += "<br>" + e.InnerException.Message;
                if (e.InnerException.InnerException != null)
                {
                    exError += "<br>" + e.InnerException.InnerException.Message;
                }
            }

            yield return exError;
        }
like image 187
Tim Jarvis Avatar answered Dec 22 '22 00:12

Tim Jarvis