C# - Error “not all code paths return a value” with an array as out parameter

后端 未结 6 1290
走了就别回头了
走了就别回头了 2021-01-24 05:49

I currently have the below code:

public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)

    {
        strSe         


        
相关标签:
6条回答
  • 2021-01-24 05:54

    You have no final return before the method exits. You are exiting if there are no elements but you need a return at the end. If you are not interested in the value then why not set the return type to void?

    0 讨论(0)
  • 2021-01-24 06:01

    You need to return an integer value according to your methods signature.

    After the for loop is where a value should be returned.

    0 讨论(0)
  • 2021-01-24 06:02

    You are only returning a value if count <= 0. Either you need to return a value after the for loop, or change the method signature to be void, depending on what you want the return value to represent.

    If you want to return the array with the counts in, then change the return type to string[] and remove the out argument.

    0 讨论(0)
  • 2021-01-24 06:02

    What value do you want to return from this function? I guess that you need to add this line to the end:

    return i;

    0 讨论(0)
  • 2021-01-24 06:05

    The error isn't anything to do with your out parameter.

    Your method

    public int GetSeatInfoString(
                       DisplayOptions choice, out string[] strSeatInfoStrings)
    

    is declared as returning an int, and doesn't do this for all code paths.

    0 讨论(0)
  • 2021-01-24 06:09

    You can add return strSeatInfoStrings.Length at the end

    public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)
    
        {
            strSeatInfoStrings = null;
            int count = GetNumOfSeats(choice);
    
            if ((count <= 0))
                return 0;
    
            strSeatInfoStrings = new string[count];
    
            int i = 0;
    
            for (int index = 0; index <= m_totNumOfSeats - 1; index++)
            {
                if (string.IsNullOrEmpty(m_nameList[index]))
                    strSeatInfoStrings[i++] =
    m_nameList[index].ToString(); }
    
        return strSeatInfoStrings.Length;
    
        }
    
    0 讨论(0)
提交回复
热议问题