This class is nothing more that an extension to StringCollection
enabling quick ToArray()()() type Functionality
Namespace:
CA.Common.TextAssembly: CA.Common (in CA.Common.dll) Version: 1.0.0.0 (1.0.0.0)
Remarks
Hopefully someone from Microsoft will read this as it is a very simple addition to the StringCollection class. The code to expose a StringCollection as a string Array is very simple and has been used in many utility type functions. In general when wanting to work with a collection of strings the string array is the last choice however it is compatible with many functions exposed such as string.Split and System.IO.Directory.GetFiles() it would be nice to expose this function as the ToArray()()() method does.
//Source code from the Code Associate C# code library, Full documentation and latest updates can be found //@ http://www.codeassociate.com/caapi/ namespace CA.Common.Text { public class StringCollection : System.Collections.Specialized.StringCollection { - #region ToArray public string[] ToArray() { string[] result = new string[Count]; CopyTo(result, 0); return result; } #endregion } }
Examples
Unit Tests to show how the ToArray works
[Test] public void CheckStringCollectionToArray() { StringCollection sc = new StringCollection(); sc.Add("Item1"); sc.Add("Item2"); sc.Add("Item3"); // create a string array from the collection string[] AsArray = sc.ToArray(); // test to see if the counts are the same Assert.AreEqual(sc.Count, AsArray.Length); for (int i =0;i< sc.Count; i++) { // check to see if every element is the same. Assert.AreEqual(sc[i], AsArray[i]); } } [Test] public void StringCollectionToAndFromArray() { // Dim up a new test array of countries String[] countryTestArr = new [] { "New Zealand", "Australia", "United States", "England", "India" }; StringCollection countryTestCol = new StringCollection(); // This is the inverse of the ToArray function ie putting a string array into the StringCollection. countryTestCol.AddRange(countryTestArr); // Now Assert that the array has copied correctly testing each value and it order for (int i = 0; i < countryTestCol.Count; i++) { // check to see if every element is the same. Assert.AreEqual(countryTestCol[i], countryTestArr[i]); } // Copy the data out of the array String[] countryTestArr2 = countryTestCol.ToArray(); // assert that the new array is the same as the orginal array in values. for (int i = 0; i < countryTestArr2.Length; i++) { // check to see if every element is the same. Assert.AreEqual(countryTestArr2[i], countryTestArr[i]); } }
Inheritance Hierarchy
System..::.Object
System.Collections.Specialized..::.StringCollection
CA.Common.Text..::.StringCollection
System.Collections.Specialized..::.StringCollection
CA.Common.Text..::.StringCollection