C#: Use of unassigned local variable, using a foreach and if

前端 未结 6 988
说谎
说谎 2020-11-30 14:19

I have this following code:
I get the error, \"Use of un-Assigned Local variable\" I\'m sure this is dead simple, but im baffled..

    public string ret         


        
6条回答
  •  死守一世寂寞
    2020-11-30 15:08

    That is because the compiler can't know that there always are any items in RssData. If it would be empty, the code in the loop would never be executed, and the variable would never be assigned.

    Just set the variable to null when you create it, so that it always has a value:

    string result = null;
    

提交回复
热议问题