어제 메서드를 작업하고 이상한 뭔가를 만났습니다. 여기 코드의 다운 된 버전이 있습니다. 기본적으로 문제는 OrderBy가 Bar.PopulateList 메서드가 지속되지 않습니다.메서드에 개체 전달 및 해당 개체에 대한 확장 메서드 호출
class Foo
{
List MyObjects;
public void PopulateMyObjects()
{
//Items are added to my list but the OrderBy is not persisting.
Bar.PopulateList(MyObjects);
}
}
class Bar
{
public static int PopulateList(List theList)
{
foreach(var in WebSerbiceCall)
{
theList.Add(var);
}
// the OrderBy call only sorts 'theList' in the context of this method.
// When I return from this method theList has been populated but the Ordering has
// reverted back to the order that the items were added to the list.
theList.OrderBy(obj => obj.ID);
return theList.Count;
}
}
지금은 코드를 업데이트하고 모든 작품은 아래에 따라 심판 키워드를 추가하는 경우 : 예를 들어, 공공 정적 int PopulateList (ref 목록 theList) 및 Bar.PopulateList (ref MyObjects);
누구든지 나를 계몽시킬 수 있습니까? 나는 물체가 항상 심판에 의해 지나간 줄 알았지? OrderBy가 확장 메소드인가요?
감사합니다, 기술자에게
나는 너무 그때, 나는 시도 : theList = thisList.OrderBy (obj => obj.ID) .ToList(); 아래 Matthew에 의해 언급되었지만 그것은 작동하지 않았다. 또한 ref 키워드를 추가하는 것이 왜 효과가 있습니까? – CianM
@CianM'theList'가'out' 또는'ref' 매개 변수가 아니라면 여전히 작동하지 않을 것입니다. 그것 없이는 메서드 내에서 참조 복사본을 수정하는 것입니다. – JaredPar
고마워요! 나는 이것을 이해할 수있는 다른 사람들을 위해 예쁜 그림으로 훌륭한 기사를 찾았습니다. http://rapidapplicationdevelopment.blogspot.com/2007/01/parameter-passing-in-c.html – CianM