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
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:
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:
As seen above, the Longest Type Name as in this example is 38 characters long ‘ManifestSignatureInformationCollection’
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#
VB.NET
Hope that was some fun! I hope the article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
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!
Was this article worth reading? Share it with fellow developers too. Thanks!
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