Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Hi Developers,

I am having simple function as mentioned below:

C#
public static string GetCodeName(int machineId)
        {
            var dc = new CEWBDataContext();
            var codename = ModelTypes(machineId, dc).Select(x => x.CodeName).ToList();
            if (codename.Any())
                return codename.FirstOrDefault();
        }


I am facing the signature error saying
"
Not all code paths return a value
"

what is wrong in the above code.

What I have tried:

I have tried below and its works fine. But what is the problem with the above code
if (codename.Count > 0)
               return codename.ToList()[0];
           else
               return null;
Posted
Updated 15-Nov-20 7:43am
v2

1 solution

Your problem is exactly as it states and what you've tried is your exact solution.

You've got a condition in your code that says if(codename.Any()) { return codename.FirstOrDefault(); } but you have no else condition.

Your method has to return a string regardless of what fashion it is in. If there is no codename you should return string.Empty or null.

Ex: if(codename.Any()) { return codename.FirstOrDefault(); } else { return string.Empty; }

Then in the portion of your code that consumes this method, you need to implement some sort of error handling to say var codename = MyClass.GetCodeName(1); if(string.IsNullOrEmpty(codename) { //do some sort of error here, throw an exception, do something about the error...etc }

You must have an else condition or a ternary operator in your GetCodeName method. There is no way around it.
 
Share this answer
 
v2
Comments
D-Kishore 12-Nov-19 1:12am    
Thanks you, But the we are using that FirstOrdefault(), I thought that this will take care if the value had null also.
David_Wimbley 12-Nov-19 2:46am    
First or default has nothing to do with how a method works or available return values.

A method must have a return value regardless of its value. Your IF statement is preventing any values being returned if codename.Any() is false. If its true, you have a return value.

Based on your comment it seems you need to get rid of your if statement and just return FirstOrDefault as the .Any() check is redundant.

Ex: Change
if (codename.Any())
return codename.FirstOrDefault();

To just

return codename.FirstOrDefault();

That should accomplish what you seem to be indicating you need. Regardless of any contents within the codename List, it will return first or default (in this case null if the list is empty).
D-Kishore 12-Nov-19 3:50am    
Thanks David, for clearing my clarifications.
Richard Deeming 12-Nov-19 11:08am    
NB: Since you're using FirstOrDefault, you don't need that if block at all. Neither do you need the ToList call.
var dc = new CEWBDataContext();
return ModelTypes(machineId, dc).Select(x => x.CodeName).FirstOrDefault();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900