Background
A few days ago I needed to examine the contents of a string collection.
I was surprised that the contents of the collection were not dumped.
What was dumped instead is the collections’ class name.
Examination
Outline
Let us give a couple of use cases:-
- Array of String
- System.Collections.Generic.List<string>
- System.Collections.Specialized.StringCollection
- System.Collections.Specialized.StringCollection Convert
Array of String
Outline
- Declare Array of String
- string[] arrayString = new string[2]
- To concatenate all the strings, we can issue string.join
Code
private static String prepareFilelistUsingArrayofString() { String strListofFileNames = ""; string[] arrayString = new string[2]; arrayString[0] = "file -01"; arrayString[1] = "file -02"; strListofFileNames = string.Join ( System.Environment.NewLine , arrayString ); return (strListofFileNames); }
Output
Output Image
System.Collections.Generic.List<string>
Outline
- Declare Collections.Generic.List<string>
- System.Collections.Generic.List<string> objListofFiles
- Initialize Variable
- objListofFiles = new System.Collections.Generic.List<string>();
- To concatenate all the strings, we can issue string.join
Code
private static String prepareFilelistUsingListString() { System.Collections.Generic.List<string> objListofFiles; objListofFiles = new System.Collections.Generic.List<string>(); String strListofFileNames=""; objListofFiles.Add("file -01"); objListofFiles.Add("file -02"); strListofFileNames = string.Join ( System.Environment.NewLine , objListofFiles ); return (strListofFileNames); }
Output
Output Image
System.Collections.Generic.List<string>
Outline
- Declare System.Collections.Specialized.StringCollection
- System.Collections.Specialized.StringCollection objListofFiles
- Initialize Variable
- objListofFiles = new System.Collections.Specialized.StringCollection();
- To concatenate all the strings, issuing string.join returns the class’s name
Code
private static String prepareFilelistUsingStringCollection() { String strListofFileNames = ""; System.Collections.Specialized.StringCollection objListofFiles = null; objListofFiles = new System.Collections.Specialized.StringCollection(); objListofFiles.Add("file -01"); objListofFiles.Add("file -02"); strListofFileNames = string.Join ( System.Environment.NewLine , objListofFiles ); objListofFiles = null; return (strListofFileNames); }
Output
Output Image
Explanation
The list’s contents are not displayed.
What is displayed is the object’s name.
System.Collections.Generic.List<string>
Outline
- Declare System.Collections.Specialized.StringCollection
- System.Collections.Specialized.StringCollection objListofFiles
- Initialize Variable
- objListofFiles = new System.Collections.Specialized.StringCollection();
- To concatenate all the strings, we issued string.join
- We have to convert the StringCollection
- StringArray
- Declare String Array
- string[] strArray;
- Allocate String Array based on the size of the StringCollection
- strArray = new string[objListofFiles.Count];
- Copy stringCollection to initialized Array
- objListofFiles.CopyTo(strArray,0);
- Declare String Array
- Use System.Linq namespace, Enumerable.Cast<TResult>
- Import System.Linq
- using System.Linq;
- Declare System.Collections.Generic.List
- System.Collections.Generic.List<string> listString;
- Cast StringCollection to StringCollection and then get convert the result to List
- listString = objListofFiles.Cast<String>().ToList();
- Import System.Linq
- StringArray
Code
using System.IO; using System; using System.Linq; class Program { enum enumConversionChoice { convertToStringArray , convertToIEnumerable }; static void Main() { Console.WriteLine ( prepareFilelistUsingStringCollectionConverted ( enumConversionChoice.convertToStringArray ) ); Console.WriteLine(""); Console.WriteLine(""); Console.WriteLine ( prepareFilelistUsingStringCollectionConverted ( enumConversionChoice.convertToIEnumerable ) ); } private static String prepareFilelistUsingStringCollectionConverted ( enumConversionChoice choice ) { String strListofFileNames = ""; System.Collections.Specialized.StringCollection objListofFiles = new System.Collections.Specialized.StringCollection(); objListofFiles.Add("file -01"); objListofFiles.Add("file -02"); if (choice == enumConversionChoice.convertToStringArray) { string[] strArray; strArray = new string[objListofFiles.Count]; objListofFiles.CopyTo(strArray,0); strListofFileNames = string.Join ( System.Environment.NewLine , strArray ); } else if (choice == enumConversionChoice.convertToIEnumerable) { System.Collections.Generic.List<string> listString = null; listString = objListofFiles.Cast<String>().ToList(); strListofFileNames = string.Join ( System.Environment.NewLine , listString ); } return (strListofFileNames); } }
Hints
Errors
‘System.Collections.Specialized.StringCollection’ does not contain a definition for ‘Cast’ and no extension method ‘Cast’ accepting a first argument of type ‘System.Collections.Specialized.StringCollection’ could be found (are you missing a using directive or an assembly reference?)
If you get the error posted above, please be sure to add “using System.Linq“;
Source Code
GitHub
DanielAdeniji/stringCollectionDump.cs
Link
Summary
There are quite a few ways to store strings.
Strings arrays have some limitations.
Unfortunately, the System.Collections.Specialized.StringCollection class does not have a method to directly dump an object’s instance.
References
- vurdalakov
- [C#] How to convert StringCollection to List<String> or other IEnumerable<String> type
Link
- [C#] How to convert StringCollection to List<String> or other IEnumerable<String> type