Note that there are some explanatory texts on larger screens.

plurals
  1. POInitialize new strings inside string array
    text
    copied!<p>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 <code>new</code> 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.</p> <pre><code> foreach (var unit in unitList) { //Sort units by each army string unitName = unit.UnitName; armyUnits.Add(unitName, unit); //Sort unit properties by unit List&lt;string&gt; properites = new List&lt;string&gt;(); 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 }); } </code></pre> <p>edit: So it looks like you can do <code>new String(unit.Composition.ToCharArray())</code> inside the array. I don't think that is any more readable or quicker to write though.</p> <pre><code> properites.AddRange(new string[1]{ new String(unit.Composition.ToCharArray())} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload