Fun with LINQ - Find the Longest and Shortest Type Name in .NET 3.5 using LINQ

Posted by: Suprotim Agarwal , on 2/14/2009, in Category LINQ
Views: 31453
Abstract: While browsing through the forums a couple of days ago, I came across this query where the user wanted to list down the types in .NET 3.5 in the order of the length of their names. He also wanted to find out the total number of types in .NET 3.5 and 2.0. Here’s how to do so using LINQ.
Fun with LINQ - Find the Longest and Shortest Type Name in .NET 3.5 using LINQ
 
While browsing through the forums a couple of days ago, I came across this query where the user wanted to list down the types in .NET 3.5 in the order of the length of their names. He also wanted to find out the total number of types in .NET 3.5 and 2.0. Here’s my attempt of how to do so using LINQ. If you know of a better way or find any discrepancy in the results displayed, please do let me know.
I have created a Console Application in Visual Studio 2008. Let’s take the problem one at a time.
Before I begin, here’s a disclaimer. The results may vary on your machine. Since we are referring to the GetExportedTypes() which returns type visible outside the assembly, you can get different results by adding new references (Right click project > Add Reference ) or by changing the access modifiers of the types. Having said that, let’s get started:
Listing down the public types in .NET 3.5
We can use reflection and a simple LINQ query to get the Types in .NET 3.5 as shown below:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
     from aType in assembly.GetExportedTypes()
     select aType;
 
var aList = assemb
.Where(x => x.Assembly.FullName.Contains("Version=3.5.0.0"))
 
foreach (var a in aList)
{
Console.WriteLine("{0}", a.Name);
}
 
Console.ReadLine();
}
VB.NET
    Sub Main()
        Dim assemb = _
         From assembly In AppDomain.CurrentDomain.GetAssemblies(),aType In assembly.GetExportedTypes() _
         Select aType
 
        Dim aList = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=3.5.0.0"))
        For Each a In aList
            Console.WriteLine("{0}", a.Name)
        Next
 
        Console.ReadLine()
    End Sub
Note: If you want to Fully qualify the type name, use v.FullName instead of v.Name
Output:
List Assemblies
Listing down the types in the order of the length of their names
An OrderBy Descending would do the trick here as shown below:
C#
static void Main(string[] args)
{
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
     from aType in assembly.GetExportedTypes()
     select aType;
 
var aList = assemb
.Where(x => x.Assembly.FullName.Contains("Version=3.5.0.0"))
.OrderByDescending(x => x.Name.Length);
 
foreach (var a in aList)
{
Console.WriteLine("Length: {0} {1}", a.Name.Length, a.Name);
}
 
Console.ReadLine();
}
 
VB.NET
    Sub Main(ByVal args() As String)
        Dim assemb = _
         From assembly In AppDomain.CurrentDomain.GetAssemblies(),aType In assembly.GetExportedTypes() _
         Select aType
 
        Dim aList = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=3.5.0.0")).OrderByDescending(Function(x) x.Name.Length)
 
        For Each a In aList
            Console.WriteLine("Length: {0} {1}", a.Name.Length, a.Name)
        Next a
 
        Console.ReadLine()
    End Sub
Output:
Longest Type
As seen above, the Longest Type Name as in this example is 38 characters long ‘ManifestSignatureInformationCollection’
Shortest Type
and the Shortest Type Name is 3 characters long ‘Aes’
Total types in .NET 2.0 and .NET 3.5
In order to find the total types in .NET 2.0 and .NET 3.5, we will use the GroupBy clause as shown here:
C#
static void Main(string[] args)
{
    var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
                 from aType in assembly.GetExportedTypes()
                 select aType;
  
 
    var vers = assemb.Select(
                    m => m.Assembly.FullName.Split(",".ToCharArray())[1])
           .GroupBy(x => x)
           .Select(x => new { VerName = x.Key, Count = x.Count() });
 
    foreach (var ver in vers)
    {
        Console.WriteLine(".NET {0} has {1} types\n",ver.VerName, ver.Count);
    }
 
 
    Console.ReadLine();
}
 
 
 
VB.NET
 
    Sub Main(ByVal args() As String)
        Dim assemb = _
         From assembly In AppDomain.CurrentDomain.GetAssemblies(), aType In assembly.GetExportedTypes() _
         Select aType
 
 
        Dim vers = assemb.Select(Function(m) m.Assembly.FullName.Split(",".ToCharArray())(1)) _
        .GroupBy(Function(x) x) _
        .Select(Function(x) New With {Key .VerName = x.Key, Key .Count = x.Count()})
 
        For Each ver In vers
            Console.WriteLine(".NET {0} has {1} types" & Constants.vbLf, ver.VerName, ver.Count)
        Next ver
 
 
        Console.ReadLine()
    End Sub
Output:
C#
Csharp Output
 
VB.NET
VB Output
 
Hope that was some fun! I hope the article was useful and I thank you for viewing it.
If you liked the article,  Subscribe to the RSS Feed or Subscribe Via Email 
 
 
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+
Further Reading - Articles You May Like!
Author
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Jakur on Saturday, February 14, 2009 10:59 AM
Very good demo of LINQ. Thanks.
Comment posted by Piyush Goriya on Wednesday, February 25, 2009 7:43 AM
This Article is not good because i will already send this article to dotnetcurry so it's the same as my article but now a time my article is not see to every one people whose element with the databased.....
Comment posted by Suprotim Agarwal on Thursday, February 26, 2009 6:31 AM
Piyush: I am not sure I follow you. Same as your article? Have you written one of a similar kind? Please post the url.
Comment posted by mellamokb on Thursday, July 30, 2009 11:47 AM
I've always wondered what is the longest name of a fully-qualified type or static member, i.e., what can be typed using the Visual Studio intellisense without creating any instances.  Here is an interesting one that I found:
System.ServiceModel.MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10.  That has got to be the longest property name I have ever seen!

Cheers,
~ mellamokb
Comment posted by Gaurav Dixit on Friday, November 13, 2009 12:34 AM
I like this post very much and i like to share this information on my blgo <a href="http://ignou-student.blogspot.com">http://ignou-student.blogspot.com </a>
Comment posted by Thanigainathan on Tuesday, November 17, 2009 2:36 PM
Very good explanation of how we can start using LINQ to ease our regular works.

Thanks,
Thani

Categories

JOIN OUR COMMUNITY

POPULAR ARTICLES

FREE .NET MAGAZINES

Free DNC .NET Magazine

Tags

JQUERY COOKBOOK

jQuery CookBook