私は昨日メソッドに取り組んでいて、何か奇妙なことに遭遇しました。ここにコードのダムバージョンがあります。基本的に問題は、Bar.PopulateListメソッドに適用されたOrderByが持続しないことです。
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;
}
}
ここで、コードを更新し、以下のようにrefキーワードを追加すると、すべて機能します。たとえば、public static int PopulateList(ref List theList)およびBar.PopulateList(ref MyObjects);
誰かが私を啓発できますか?オブジェクトは常にrefによって渡されると思いましたか?OrderByが拡張メソッドであるというのは事実ですか?
ありがとう、Cian