C# Program to Print the List of Non-Generic Collections Using LINQ
The non-Generic collection is defined in System.Collections namespace. It is a general-purpose data structure that works on object references, so it can handle any type of object, but not in a safe-type manner. Non-generic collections are defined by the set of interfaces and classes. We can get all types that implement an interface by using AppDomain along with CurrentDomain. These two domains will get the list of collections. Along with that, we will use IEnumerable. IEnumerable is an interface in C# which is used to define one method that only allows readonly access to a collection and then a collection that implements this interface can be used with a for-each statement. This interface contains the System.Collections.Generic namespace. IEnumerable interface is a generic interface that allows looping over generic or non-generic lists.
Syntax:
public IEnumerator method()
{
// Statements
}
Return: This method returns an enumerator that iterates through the collection.
Example:
C#
using System;
using System.IO;
using System.Linq;
using System.Collections;
class GFG{
public static void Main( string [] args)
{
var result = typeof (IEnumerable);
var nonGenCollection = AppDomain.CurrentDomain.GetAssemblies().SelectMany(
x => x.GetTypes()).Where(x => result.IsAssignableFrom(x));
foreach ( var detail in nonGenCollection)
{
Console.WriteLine( "---> " + detail.FullName);
}
}
}
|
Explanation: In this example, we defined a “nonGenCollection” variable that will get all the IEnumTypes by using “AppDomain.CurrentDomain.GetAssemblies()” method. This method will get all the lists of non-generic collections. Then we have iterated a for each loop to display the full name of the collection
Output:
---> Mono.Security.X509.X509CertificateCollection
---> Mono.Security.X509.X509ExtensionCollection
---> System.ArraySegment`1
---> System.String
---> System.Array
---> System.Resources.IResourceReader
---> System.Resources.ResourceFallbackManager
---> System.Resources.ResourceReader
---> System.Resources.ResourceSet
---> System.Resources.RuntimeResourceSet
---> System.Reflection.TypeInfo+<GetDeclaredMethods>d__9
---> System.Reflection.TypeInfo+<get_DeclaredNestedTypes>d__23
---> System.Reflection.Assembly+<get_DefinedTypes>d__150
---> System.Threading.ThreadPool+<EnumerateQueuedWorkItems>d__21
---> System.Threading.Tasks.IProducerConsumerQueue`1
---> System.Threading.Tasks.MultiProducerMultiConsumerQueue`1
---> System.Threading.Tasks.SingleProducerSingleConsumerQueue`1
---> System.Threading.Tasks.ThreadPoolTaskScheduler+<FilterTasksFromWorkItems>d__7
---> System.IO.Iterator`1
---> System.IO.FileSystemEnumerableIterator`1
---> System.IO.DirectoryInfo+<CreateEnumerateDirectoriesIterator>d__39
---> System.IO.DirectoryInfo+<CreateEnumerateFilesIterator>d__43
---> System.IO.DirectoryInfo+<EnumerateFileSystemInfos>d__47
---> System.IO.File+<ReadLines>d__58
---> System.Runtime.Serialization.SurrogateHashtable
---> System.Runtime.Remoting.Channels.AggregateDictionary
---> System.Runtime.Remoting.Channels.BaseChannelObjectWithProperties
---> System.Runtime.Remoting.Channels.BaseChannelSinkWithProperties
---> System.Runtime.Remoting.Channels.BaseChannelWithProperties
---> System.Runtime.Remoting.Messaging.ConstructionCallDictionary
---> System.Runtime.Remoting.Messaging.MCMDictionary
---> System.Runtime.Remoting.Messaging.MethodCallMessageWrapper+DictionaryWrapper
---> System.Runtime.Remoting.Messaging.MessageDictionary
---> System.Runtime.Remoting.Messaging.MethodReturnDictionary
---> System.Runtime.Remoting.Messaging.MethodReturnMessageWrapper+DictionaryWrapper
---> System.Security.NamedPermissionSet
---> System.Security.PermissionSet
---> System.Security.Policy.ApplicationTrustCollection
Press enter to continue ......
Last Updated :
01 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...