Answer the question
In order to leave comments, you need to log in
How to correctly define the return path to the function value code?
Good afternoon!
At the beginning of the question, I provide the code for the function for determining the existence of a specialty, written in C#:
public static bool isProf(string special)
{
dynamic sl = JObject.Parse(File.ReadAllText(@"..\..\..\..\data\source\specialization.json"));
foreach (var property in sl.specialization)
{
if (property == special)
{
return true;
}
else
{
return false;
}
}
}
'"Program.isProf(string)": не все пути к коду возвращают значение. [writecurrentdata]
Answer the question
In order to leave comments, you need to log in
After the loop, you need to write
return false;
because if there is a situation that the program does not get into the cycle, then xs what to return.
In general, the algorithm is also not correct - your loop will stop after the first check.
The method can be rewritten like this:
public static bool isProf(string special)
{
dynamic sl = JObject.Parse(File.ReadAllText(@"..\..\..\..\data\source\specialization.json"));
foreach (var property in sl.specialization)
{
if (property == special)
{
return true;
}
}
return false;
}
public static bool isProf(string special)
{
var sl = JObject.Parse(File.ReadAllText(@"..\..\..\..\data\source\specialization.json"));
return sl.specialization.Any(e => e == special);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question