いいえ、あなたがすることはできません。メソッドに渡された変数の名前は、メソッド内では使用できません。
編集:他の場所でのあなたのコメントに基づいて、私は明らかにあなたがやろうとしていたことについて間違った印象を持っていました。SortedListに名前を追加する代わりに、辞書を渡すことができます。
public void CallsDoStuffWithLists()
{
SortedList<int, string> theFirstList = new SortedList<int, string>();
SortedList<int, string> aSecondList = new SortedList<int, string>();
SortedList<int, string> thirdList = new SortedList<int, string>();
SortedList<int, string> theLastList = new SortedList<int, string>();
PopulateTheFirstList(theFirstList);
PopulateTheSecondList(aSecondList);
//etc
// call do stuff with lists.
DoStuffWithLists(new Dictionary<string, SortedList<int, string>>{{"theFirstList", theFirstList}, {"aSecondList",aSecondList}, {"thirdList", thirdList}, {"theLastList", theLastList}});
}
public void DoStuffWithLists(Dictionary<string, SortedList<int,string>> lists)
{
// does not loop through all,
// does not throw exceptions..
// if there is a list that was misnamed, it will not be handled.
SortedList<int, string> temp;
if(lists.TryGetValue("theFirstList", out temp)) DoStuffWithSubList(temp);
if(lists.TryGetValue("aSecondList", out temp)) DoStuffWithSubList(temp);
// loops through each and acts on them accordingly.
// if DoStuffWithLists is
foreach (var list in lists)
{
//or use switch statement.
if(list.Key == "theFirstList")
{
DoStuffWithSubList(list.Value);
}
else if(list.Key == "aSecondList")
{
DoOtherStuffWithSublist(list.Value);
}
//etc...
else
{
//we got an unexpected list, what do we do with it?
}
}
補足:私はあなたが望むことをどのように行うことができるかをあなたに示しましたが、それはそれが良い考えであるという意味ではありません。この場合、何の利益もなく複雑さを増しているようです。はるかに複雑でエラーが発生しやすいコードがあり、リストごとのデータ処理からデータ取得を分離していますが、すべてのリストのデータ処理を組み合わせています。
DoStuffWithListsに実際にデータの取得も行わせることを検討しましたか?エラーが発生しにくく、名前を一致させる必要がありません。
btn_Click(object sender, EventArgs e)
{
DoStuffWithLists();
}
public void DoStuffWithLists()
{
//this is far simpler and less error prone.
SortedList<int, string> theFirstList = new SortedList<int, string>();
PopulateTheFirstList(theFirstList);
DoStuffWithSubList(theFirstList);
SortedList<int, string> aSecondList = new SortedList<int, string>();
PopulateTheSecondList(aSecondList);
DoOtherStuffWithSublist(aSecondList);
}
または、各リストを完全に処理するためのメソッドをまだ持っていて、代わりにそれを呼び出す方が良いです...
btn_Click(object sender, EventArgs e)
{
//this is simpler yet, separates the concerns of each type of list.
HandlePayGrade();
HandleSecondList();
HandleThirdList();
}
public void HandlePayGrade()
{
// you are still separating your data access and processing concerns here.
SortedList<int, string> paygrades = new SortedList<int, string>();
populatePaygrades(paygrades, sqlconnection);
DoStuffWithPaygrades(paygrades);
}
編集:以下の元の回答-
名前の付いたもの(リスト)に基づいて、doStuffWithListsに渡されたすべてのリストが同時に処理されることを期待しているように見えます。これは当てはまりません。呼び出されるたびに、渡された1つのリストを処理します。
整数を使用した例...
//Double an int is invoked 3 times, each time dealing with one integer.
public int DoubleAnInt(int x)
{
return x+x;
}
public void CallsDoubleAnInt()
{
int a = 1;
int b = DoubleAnInt(a);
int c = DoubleAnInt(b);
int d = DoubleAnInt(c);
}
また、doStuffWithListsがどこから呼び出されたかによって、動作が異なるようにしようとしているようです。この場合、異なるメソッドが必要です。
protected void doStuffWithPaygradeLists(SortedList<int,string> list)
{
...
}
protected void doStuffWithSomeOtherLists(SortedList<int,string> list)
{
...
}