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
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.
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