What is the total number of classes in .NET? The number that is in the downloaded runtime for .NET 2.0, .NET 3.0 and .NET 3.5 SP1.
We are writing a scientific paper about an application that is based on .NET and currently state that there are more than 6000 classes. But I am not sure if this is the correct number.
For instance this page states the number of assemblies, namespaces, methods, etc., but not the number of classes.
Test platform: Windows XP 64 bit SP2, 8 GB RAM.
Update 4: Our paper has been published! I used 9911 for the number of classes (see update 3 below). The journal is Journal of Proteome Research and the title is: "MSQuant, an Open Source Platform for Mass Spectrometry-Based Quantitative Proteomics". Unfortunately the full text of the paper is not freely available, only the abstract.
Update 3: I think I have come very close to a solution now: 9911 public classes for .NET 3.5 SP1. Extending on update 1, I have made the function recursive and extended it so the number of types, classes and public classes are reported for any sub-folder and its subfolders. Running this on C:\WINDOWS\Microsoft.NET gives 40414 types, only 0.2 % from the number in the referenced article. Full transcript - HTML source is tab separated so it can be imported into a spreadsheet, e.g. OpenOffice Calc. Here is a break-down for public classes:
Framework:
Total: 6025
v1.1.4322
0
v2.0.50727
5265
v3.0
641
v3.5
119
Framework64:
Total: 3886
v2.0.50727
3126
v3.0
641
v3.5
119
Update 2: I tried using NDepend and CQL suggested by lextm and it gave a 10% higher number for .NET 2.0 (89 DLLs in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727): 5855 classes. This was on a different system than for the programmatic solution (see below).
Procedure:
Download NDepend (NDepend_2_12_1_3122.zip), via http://www.ndepend.com/NDependDownload.aspx
Unzip with 7-Zip
Run VisualNDepend.exe
Menu File/Select .NET Assemblies to Analyze/ <Selected the 89 DLL files in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>/ <Selected all>/ OK.
Press "Create Query" (lower right) and type/paste:
SELECT TYPES FROM ASSEMBLIES "Accessibility", "cscompmgd", "CustomMarshalers", "IEExecRemote", "IEHost", "IIEHost", "ISymWrapper", "Microsoft.Build.Engine", "Microsoft.Build.Framework", "Microsoft.Build.Tasks", "Microsoft.Build.Utilities", "Microsoft.JScript", "Microsoft.VisualBasic", "Microsoft.VisualBasic.Compatibility", "Microsoft.VisualBasic.Compatibility.Data", "Microsoft.VisualBasic.Vsa", "Microsoft.VisualC", "Microsoft.Vsa", "Microsoft.Vsa.Vb.CodeDOMProcessor", "Microsoft_VsaVb", "mscorlib", "sysglobl", "System", "System.configuration", "System.Configuration.Install", "System.Data", "System.Data.OracleClient", "System.Data.SqlXml", "System.Deployment", "System.Design", "System.DirectoryServices", "System.DirectoryServices.Protocols", "System.Drawing", "System.Drawing.Design", "System.EnterpriseServices", "System.Management", "System.Messaging", "System.Runtime.Remoting", "System.Runtime.Serialization.Formatters.Soap", "System.Security", "System.ServiceProcess", "System.Transactions", "System.Web", "System.Web.Mobile", "System.Web.RegularExpressions", "System.Web.Services", "System.Windows.Forms", "System.XML" WHERE IsPublic AND IsClass
Update 1: based on Jon Skeet's answer I have developed a function (listed below). The preliminary result is 5265 public classes, 12626 classes in total, 18317 types for .NET 2.0. 802 public classes from mscorlib.dll and 678 public classes from System.dll. This is from 89 DLL files of which 40 fails with Assembly.LoadFrom(). But I am not sure I measure the right thing or in the right place.
Call:
DotNetClassCount("C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727")
Function:
Imports System.Reflection 'For Assembly
Imports System.IO 'For Path
Private Function DotNetClassCount(ByRef aBaseDirectory As String) _
As Integer
Dim classCount As Integer = 0
Dim failCount As Integer = 0 'For statistics only.
Dim folderItems As String() = Directory.GetFiles(aBaseDirectory)
Dim someFolderItem As String
For Each someFolderItem In folderItems
Dim fileName As String = Path.GetFileName(someFolderItem)
If Path.GetExtension(fileName) = ".dll" Then
Try
Dim asm3 As Assembly = _
Assembly.LoadFrom(someFolderItem)
Dim types As System.Type() = asm3.GetTypes()
Dim DLLclassCount As Integer = 0
Dim someType As System.Type
For Each someType In types
If someType.IsClass AndAlso someType.IsPublic Then
DLLclassCount += 1
End If
Next
classCount += DLLclassCount
Catch ex As Exception
'Fail silently...
failCount += 1
End Try
End If
Next
Return classCount
End Function 'DotNetClassCount()
NET Assembly file format. There are three types of class libraries that you can use: Platform-specific class libraries have access to all the APIs in a given platform (for example, . NET Framework on Windows, Xamarin iOS), but can only be used by apps and libraries that target that platform.
In this article. We will understand types of classes in c#. There are four different type of classes available in c#.
For vb6 we sum up the total number of standard and class modules which gives us the total number of classes in project. For . NET we calculate the total number of code behind files, and in standalone .
Two classes with the same name can be created inside 2 different namespaces in a single program. Inside a namespace, no two classes can have the same name.
That page gives the number of types (40513 in 3.5SP1) - is it really important to you to differentiate between classes and structs/enums/interfaces?
I would expect the vast majority of those 40K+ to be classes, so your 6000 figure is very conservative.
Given a list of assemblies, it's very easy to work out the number of classes:
int classes = assemblies.GetTypes()
.Where(t => t.IsClass)
.Count();
This assumes you want all classes though - are you actually only interested in public classes?
int classes = assemblies.GetTypes()
.Where(t => t.IsClass && t.IsPublic)
.Count();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With