C# 实现重新排序方法的最佳方式是什么?
我有一些代码,应该根据给定的顺序给它的属性赋值 下面是一些执行此任务的示例代码:C# 实现重新排序方法的最佳方式是什么?,c#,code-readability,C#,Code Readability,我有一些代码,应该根据给定的顺序给它的属性赋值 下面是一些执行此任务的示例代码: public class AnimalCount { public int Dogs; public int Cats; public int Fish; public int Birds; public void RankValues(string first, string second, string third, string fourth) {
public class AnimalCount
{
public int Dogs;
public int Cats;
public int Fish;
public int Birds;
public void RankValues(string first, string second, string third, string fourth)
{
string property = "";
int value = -1;
for (int i = 0; i < 4; i++)
{
switch (i)
{
case 0: property = first; value = 10; break;
case 1: property = second; value = 12; break;
case 2: property = third; value = 19; break;
case 3: property = fourth; value = 20; break;
}
switch (property)
{
case "dogs": Dogs = value; break;
case "cats": Cats = value; break;
case "fish": Fish = value; break;
case "birds": Birds = value; break;
}
}
}
}
公共类AnimalCount
{
公共犬类;
公共猫;
公众鱼;
公共雀鸟;
公共void rankvalue(字符串第一、字符串第二、字符串第三、字符串第四)
{
字符串属性=”;
int值=-1;
对于(int i=0;i<4;i++)
{
开关(一)
{
案例0:property=first;value=10;break;
案例1:property=second;value=12;break;
案例2:property=third;value=19;break;
案例3:属性=第四;值=20;中断;
}
交换机(属性)
{
案例“dogs”:dogs=值;break;
案例“cats”:cats=值;break;
案例“fish”:fish=值;break;
案例“birds”:birds=值;break;
}
}
}
}
但是,此代码存在一些问题
如果你一定要知道的话,我正在尝试编写一个函数,该函数接受地下城和龙的能力分数的请求顺序,并按照你选择的顺序对它们进行掷骰。不完全确定我是否在跟踪你,但你不能简单地接受4个参数的有序集合吗
public void doWhatever(String[] orderedParams) {
this.animals = orderedParams;
// ...
this.doTheThing(animals[0], 10);
this.doTheThing(animals[1], 12);
// etc
}
不完全确定我是否在跟踪您,但您能否不简单地接受4个参数的有序集合
public void doWhatever(String[] orderedParams) {
this.animals = orderedParams;
// ...
this.doTheThing(animals[0], 10);
this.doTheThing(animals[1], 12);
// etc
}
字典将是一个很好的结果容器,因为您实际上需要一个键/值对。如果将两个集合输入到秩函数中
public Dictionary<string, int> Rank(string[] orderedKeys, int[] orderedValues)
{
Dictionary<string, int> rankedDictionary = new Dictionary<string, int>();
for (int i = 0; i < orderedKeys.Length; i++)
{
rankedDictionary.Add(orderedKeys[i], orderedValues[i]);
}
return rankedDictionary;
}
public void CallRank()
{
string[] orderedKeys = new[] { "dogs", "cats", "fish", "birds" };
int[] orderedValues = new[] { 10, 12, 19, 20 };
Dictionary<string,int> rankedResults = Rank(orderedKeys, orderedValues);
int catsValue = rankedResults["cats"];
}
所以你的字典应该是
Dictionary<Animals, int>
字典将是一个很好的结果容器,因为您实际上需要一个键/值对。如果将两个集合输入到秩函数中
public Dictionary<string, int> Rank(string[] orderedKeys, int[] orderedValues)
{
Dictionary<string, int> rankedDictionary = new Dictionary<string, int>();
for (int i = 0; i < orderedKeys.Length; i++)
{
rankedDictionary.Add(orderedKeys[i], orderedValues[i]);
}
return rankedDictionary;
}
public void CallRank()
{
string[] orderedKeys = new[] { "dogs", "cats", "fish", "birds" };
int[] orderedValues = new[] { 10, 12, 19, 20 };
Dictionary<string,int> rankedResults = Rank(orderedKeys, orderedValues);
int catsValue = rankedResults["cats"];
}
所以你的字典应该是
Dictionary<Animals, int>
我会这样做:
public class AnimalCount
{
public int Dogs;
public int Cats;
public int Fish;
public int Birds;
private Dictionary<string, Action<int>> rankers
= new Dictionary<string, Action<int>>()
{
{ "dogs", v => Dogs = v },
{ "cats", v => Cats = v },
{ "fish", v => Fish = v },
{ "birds", v => Birds = v },
};
private Action<string, int> setRank = (t, v) =>
{
if (rankers.ContainsKey(t))
{
rankers[t](v);
}
};
public RankValues(string first, string second, string third, string fourth)
{
setRank(first, 10);
setRank(second, 12);
setRank(third, 19);
setRank(fourth, 20);
}
}
公共类AnimalCount
{
公共犬类;
公共猫;
公众鱼;
公共雀鸟;
私用词典
=新字典()
{
{“狗”,v=>dogs=v},
{“猫”,v=>cats=v},
{“鱼”,v=>fish=v},
{“birds”,v=>birds=v},
};
私人行动setRank=(t,v)=>
{
if(rankers.ContainsKey(t))
{
rankers[t](v);
}
};
公共RankValues(字符串第一、字符串第二、字符串第三、字符串第四)
{
setRank(第一,10);
setRank(第二,12);
setRank(第三名,19岁);
setRank(第四,20);
}
}
我会这样做:
public class AnimalCount
{
public int Dogs;
public int Cats;
public int Fish;
public int Birds;
private Dictionary<string, Action<int>> rankers
= new Dictionary<string, Action<int>>()
{
{ "dogs", v => Dogs = v },
{ "cats", v => Cats = v },
{ "fish", v => Fish = v },
{ "birds", v => Birds = v },
};
private Action<string, int> setRank = (t, v) =>
{
if (rankers.ContainsKey(t))
{
rankers[t](v);
}
};
public RankValues(string first, string second, string third, string fourth)
{
setRank(first, 10);
setRank(second, 12);
setRank(third, 19);
setRank(fourth, 20);
}
}
公共类AnimalCount
{
公共犬类;
公共猫;
公众鱼;
公共雀鸟;
私用词典
=新字典()
{
{“狗”,v=>dogs=v},
{“猫”,v=>cats=v},
{“鱼”,v=>fish=v},
{“birds”,v=>birds=v},
};
私人行动setRank=(t,v)=>
{
if(rankers.ContainsKey(t))
{
rankers[t](v);
}
};
公共RankValues(字符串第一、字符串第二、字符串第三、字符串第四)
{
setRank(第一,10);
setRank(第二,12);
setRank(第三名,19岁);
setRank(第四,20);
}
}
从其他答案中汲取灵感,我认为实现这一点的最佳方法如下:
using System.Collections.Generic;
public class AnimalCount
{
public int Dogs { get { return animals["dogs"]; } }
public int Cats { get { return animals["cats"]; } }
public int Fish { get { return animals["fish"]; } }
public int Birds { get { return animals["birds"]; } }
private Dictionary<string, int> animals = new Dictionary<string, int>();
public void RankValues(string first, string second, string third, string fourth)
{
animals[first] = 10;
animals[second] = 12;
animals[third] = 19;
animals[fourth] = 20;
}
}
使用System.Collections.Generic;
公共类动物帐户
{
公共int狗{获取{返回动物[“狗”];}
公共int猫{获取{返回动物[“猫”];}
公共int Fish{获取{返回动物[“鱼”];}
公共int鸟类{获取{返回动物[“鸟类”];}
私有字典动物=新字典();
公共void rankvalue(字符串第一、字符串第二、字符串第三、字符串第四)
{
动物[第一]=10;
动物[秒]=12;
动物[第三]=19;
动物[第四]=20;
}
}
并带有用于类型安全的枚举:
using System.Collections.Generic;
public enum Animals
{
Dogs, Cats, Fish, Birds
}
public class AnimalCount
{
public int Dogs { get { return animals[Animals.Dogs]; } }
public int Cats { get { return animals[Animals.Cats]; } }
public int Fish { get { return animals[Animals.Fish]; } }
public int Birds { get { return animals[Animals.Birds]; } }
private Dictionary<Animals, int> animals = new Dictionary<Animals, int>();
public void RankValues(Animals first, Animals second, Animals third, Animals fourth)
{
animals[first] = 10;
animals[second] = 12;
animals[third] = 19;
animals[fourth] = 20;
}
}
使用System.Collections.Generic;
公众动物
{
狗、猫、鱼、鸟
}
公共类动物帐户
{
公共int狗{获取{返回动物[动物.狗];}
公共int猫{获取{返回动物[动物.猫];}
公共int鱼{获取{返回动物[动物.鱼];}
公共int鸟类{获取{返回动物[动物.鸟类];}
私有字典动物=新字典();
公共无效RankValues(动物第一,动物第二,动物第三,动物第四)
{
动物[第一]=10;
动物[秒]=12;
动物[第三]=19;
动物[第四]=20;
}
}
从其他答案中汲取灵感,我认为实现这一点的最佳方法如下:
using System.Collections.Generic;
public class AnimalCount
{
public int Dogs { get { return animals["dogs"]; } }
public int Cats { get { return animals["cats"]; } }
public int Fish { get { return animals["fish"]; } }
public int Birds { get { return animals["birds"]; } }
private Dictionary<string, int> animals = new Dictionary<string, int>();
public void RankValues(string first, string second, string third, string fourth)
{
animals[first] = 10;
animals[second] = 12;
animals[third] = 19;
animals[fourth] = 20;
}
}
使用System.Collections.Generic;
公共类动物帐户
{
公共int狗{获取{返回动物[“狗”];}
公共int猫{获取{返回动物[“猫”];}
公共int Fish{获取{返回动物[“鱼”];}
公共int鸟类{获取{返回动物[“鸟类”];}
私有字典动物=新字典();
公共void rankvalue(字符串第一、字符串第二、字符串第三、字符串第四)
{
动物[第一]=10;
动物[秒]=12;
动物[第三]=19;
动物[第四]=20;
}
}
并带有用于类型安全的枚举:
using System.Collections.Generic;
public enum Animals
{
Dogs, Cats, Fish, Birds
}
public class AnimalCount
{
public int Dogs { get { return animals[Animals.Dogs]; } }
public int Cats { get { return animals[Animals.Cats]; } }
public int Fish { get { return animals[Animals.Fish]; } }
public int Birds { get { return animals[Animals.Birds]; } }
private Dictionary<Animals, int> animals = new Dictionary<Animals, int>();
public void RankValues(Animals first, Animals second, Animals third, Animals fourth)
{
animals[first] = 10;
animals[second] = 12;
animals[third] = 19;
animals[fourth] = 20;
}
}
使用