Longest and Shortest Type Name in .NET 4.0 using LINQ

Posted by: Suprotim Agarwal , on 7/6/2010, in Category LINQ
Views: 92569
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

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

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 The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn



Page copy protected against web site content infringement 	by Copyscape




Feedback - Leave us some adulation, criticism and everything in between!
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..