나는 어리석은 실수를 저지르고 있다고 확신합니다. 그러나 그것은 저를 상대로합니다. 이 날이 오류를 제공 컴파일재귀. 모든 코드 경로가 값을 반환하지는 않습니다.
void Main()
{
//Key is a parent while the list contains its children
Dictionary<string,List<string>> d = new Dictionary<string,List<string>>();
d.Add("1",new List<string>(){"2","3"});//valid.should return false
d.Add("2",new List<string>(){"4"});//valid.should return false
d.Add("3",new List<string>(){"5"});//valid.should return false
d.Add("4",new List<string>(){"1"});//invalid.should return true
IsChildAlreadyAParent("4","2",d);
}
private bool IsChildAlreadyAParent(string child, string parent, Dictionary<string, List<string>> d)
{
if(!d.ContainsKey(child) || (d.ContainsKey(child) && d[child].Count == 0))
{
return false;
}
foreach(string childOfChild in d[child])
{
if(childOfChild == parent)
return true;
if(IsChildAlreadyAParent(childOfChild, parent, d)) return true;
}
}
: 내가 코드를 몇 번 읽고 내가 복귀 조건이 놓친 것이 얼마나 볼 수 없습니다
IsChildAlreadyAParent(string, string, System.Collections.Generic.Dictionary<string,System.Collections.Generic.List<string>>)'
: not all code paths return a value
여기에 코드 조각이다. 메서드 끝 전에 메서드 return 문을 추가하여 수정할 수는 있지만이 문제를 즉시 이해할 수는 없습니다. 간격은 어디 있습니까 ??
의 부부와 함께 코드입니다 여기에 ... – umbersar
하지만 제기 된 요점은 "첫 번째 자식 만 테스트하는 것이 옳은 해결책이 아닐 수도 있습니다"라고 도움이되었습니다 – umbersar
@wanderer : 죄송합니다. 컴파일러는 이미 빈 목록을 테스트했습니다. 따라서 여전히 루프 아래에서 일어나는 일과 관련이 있습니다. –