.Net – String Collection – Dumping list’s contents

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:-

  1. Array of String
  2. System.Collections.Generic.List<string>
  3. System.Collections.Specialized.StringCollection
  4. System.Collections.Specialized.StringCollection Convert

 

Array of String

Outline

  1. Declare Array of String
    • string[] arrayString = new string[2]
  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

  1. Declare Collections.Generic.List<string>
    • System.Collections.Generic.List<string> objListofFiles
  2. Initialize Variable
    • objListofFiles = new System.Collections.Generic.List<string>();
  3. 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

  1. Declare System.Collections.Specialized.StringCollection
    • System.Collections.Specialized.StringCollection objListofFiles
  2. Initialize Variable
    • objListofFiles = new System.Collections.Specialized.StringCollection();
  3. 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

  1. Declare System.Collections.Specialized.StringCollection
    • System.Collections.Specialized.StringCollection objListofFiles
  2. Initialize Variable
    • objListofFiles = new System.Collections.Specialized.StringCollection();
  3. To concatenate all the strings, we issued string.join
  4. 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);
    •  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();

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

  1. vurdalakov
    • [C#] How to convert StringCollection to List<String> or other IEnumerable<String> type
      Link

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s