I have been trying to find out if there is a better way to initialize the string array I am using in this code snippet.
I was wondering if there was a function, or perhaps some use of new that would make the calling and assignment of all the empty strings in the code unnecessary. So I can initialize them at the same time I create the array.
foreach (var unit in unitList)
{
//Sort units by each army
string unitName = unit.UnitName;
armyUnits.Add(unitName, unit);
//Sort unit properties by unit
List<string> properites = new List<string>();
string composition ="";
string weaponSkill ="";
string ballisticSkill ="";
string strength ="";
string initiative ="";
string toughness ="";
string wounds ="";
string attacks ="";
string leadership ="";
string savingThrow ="";
string specialRules ="";
string dedicatedTransport ="";
string options ="";
string armour ="";
string weapons ="";
properites.AddRange(new string[15]{
composition = unit.Composition,
weaponSkill = unit.WeaponSkill,
ballisticSkill = unit.BallisticSkill,
strength = unit.Strength,
initiative = unit.Initiative,
toughness = unit.Toughness,
wounds = unit.Wounds,
attacks = unit.Attacks,
leadership = unit.Leadership,
savingThrow = unit.SaveThrow,
specialRules = unit.SpecialRules,
dedicatedTransport = unit.DedicatedTransport,
options = unit.Options,
armour = unit.Armour,
weapons = unit.Weapons
});
}
edit:
So it looks like you can do new String(unit.Composition.ToCharArray()) inside the array. I don't think that is any more readable or quicker to write though.
properites.AddRange(new string[1]{
new String(unit.Composition.ToCharArray())}