Longest and Shortest Type Name in .NET 4.0 using LINQ

Posted by: Suprotim Agarwal , on 7/6/2010, in Category LINQ
Views: 70986
Abstract: In this article, we will use LINQ to print the Longest and Shortest Type Name in .NET 4.0.
One of the ways of learning a technology is to add some fun to the learning. LINQ is fun! In this article, we will use LINQ to print the Longest and Shortest Type Name in .NET 4.0.
Disclaimer: The results I have shown in this article may vary on your machine. Since we are referring to the GetExportedTypes() which returns type visible outside the assembly [public types], you can get different results by adding new references (Right click project > Add Reference ) or by changing the access modifiers of the types.
Let us see the steps to use LINQ and extract information about .NET 4.0 types. This article uses a Console Application targetting .NET Framework 4.0.
Step 1: The first step is to get a list of public types defined in the assembly in the current application domain.
C#
var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
                from aType in assembly.GetExportedTypes()
                select aType;
 
VB.NET
Dim assemb = From assembly In AppDomain.CurrentDomain.GetAssemblies() , aType In assembly.GetExportedTypes() _
             Select aType
Step 2: We then filter the assembly name, group the records by length and then apply an orderby on the filtered records. We use the GroupBy and then the OrderBy as we can have multiple types with the same length.
C#
var filtered = assemb
.Where(x => x.Assembly.FullName.Contains("Version=4.0.0.0"))
.GroupBy(a => a.Name.Length)
.OrderByDescending(x => x.Key);
 
VB.NET
Dim filtered = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=4.0.0.0")).GroupBy(Function(a) a.Name.Length).OrderByDescending(Function(x) x.Key)
 
Step 3: Since the records are OrderByDescending, the longest type will be listed first and the shortest, the last.
C#
 
// Find the Longest and Shortest Types
var longestType = filtered.First();
var shortestType = filtered.Last();
 
VB.NET
Dim longestType = filtered.First()
Dim shortestType = filtered.Last()
Step 4: The final step is to loop through the records and print the TypeName, FullName and the Length.
C#
// Loop and print the Longest Type Names
foreach (var nm in longestType)
{
    Console.WriteLine("Longest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
        nm.Name, nm.FullName, nm.Name.Length);
    Console.WriteLine("-------------------------------------------");
}
 
           
// Loop and print the Shortest Type Names
foreach (var nm in shortestType)
{
    Console.WriteLine("Shortest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
        nm.Name, nm.FullName, nm.Name.Length);
    Console.WriteLine("-------------------------------------------");
}
 
VB.NET
Imports Microsoft.VisualBasic
 
For Each nm In longestType
      Console.WriteLine("Longest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
      Console.WriteLine("-------------------------------------------")
Next nm
For Each nm In shortestType
      Console.WriteLine("Shortest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
      Console.WriteLine("-------------------------------------------")
Next nm
 
OUTPUT
Output
Here’s the entire source code:
C#
    static void Main(string[] args)
    {
        var assemb = from assembly in AppDomain.CurrentDomain.GetAssemblies()
                        from aType in assembly.GetExportedTypes()
                        select aType;
 
        var filtered = assemb
        .Where(x => x.Assembly.FullName.Contains("Version=4.0.0.0"))
        .GroupBy(a => a.Name.Length)
        .OrderByDescending(x => x.Key);
 
        // Find the Longest and Shortest Types
        var longestType = filtered.First();
        var shortestType = filtered.Last();
 
        // Loop and print the Longest Type Names
        foreach (var nm in longestType)
        {
            Console.WriteLine("Longest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
                nm.Name, nm.FullName, nm.Name.Length);
            Console.WriteLine("-------------------------------------------");
        }
 
           
        // Loop and print the Shortest Type Names
        foreach (var nm in shortestType)
        {
            Console.WriteLine("Shortest Type Name: {0} \nFullName: {1} \nTypeLength: {2}",
                nm.Name, nm.FullName, nm.Name.Length);
            Console.WriteLine("-------------------------------------------");
        }
 
        Console.ReadLine();
    }
 
VB.NET
    Sub Main()
        Dim assemb = From assembly In AppDomain.CurrentDomain.GetAssemblies(), aType In assembly.GetExportedTypes()
                     Select aType
 
        Dim filtered = assemb.Where(Function(x) x.Assembly.FullName.Contains("Version=4.0.0.0")).GroupBy(Function(a) a.Name.Length).OrderByDescending(Function(x) x.Key)
 
        ' Find the Longest and Shortest Types
        Dim longestType = filtered.First()
        Dim shortestType = filtered.Last()
 
        ' Loop and print the Longest Type Names
        For Each nm In longestType
            Console.WriteLine("Longest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
            Console.WriteLine("-------------------------------------------")
        Next nm
 
 
        ' Loop and print the Shortest Type Names
        For Each nm In shortestType
            Console.WriteLine("Shortest Type Name: {0} " & vbLf & "FullName: {1} " & vbLf & "TypeLength: {2}", nm.Name, nm.FullName, nm.Name.Length)
            Console.WriteLine("-------------------------------------------")
        Next nm
 
        Console.ReadLine()
    End Sub
 
I hope you liked this article and I thank you for viewing it. The entire source code of this article can be downloaded over here
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Daniel Markham on Saturday, August 14, 2010 12:34 PM
Just for comparison, here it is in F# (without the print, which is trivial)

let currentNetTypes = System.AppDomain.CurrentDomain.GetAssemblies() |> Seq.filter(fun x->x.FullName.Contains("Version=4.0.0.0")) |> Seq.map(fun x->x.GetExportedTypes()) |> Seq.concat |> Seq.sortBy(fun x->x.Name.Length);;
Comment posted by Suprotim Agarwal on Monday, August 16, 2010 3:20 AM
Daniel: That's a nice piece of code. Does it print more than one types having the same length?
Comment posted by Adesh T C on Tuesday, November 19, 2013 10:19 PM
Good article..

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel