18

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:

  1. Download NDepend (NDepend_2_12_1_3122.zip), via http://www.ndepend.com/NDependDownload.aspx

  2. Unzip with 7-Zip

  3. Run VisualNDepend.exe

  4. Menu File/Select .NET Assemblies to Analyze/ <Selected the 89 DLL files in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>/ <Selected all>/ OK.

  5. 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()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Depending how you count private and internal code as well as enums, structs and interfaces, the number may vary a lot. – Lucero Sep 10 '09 at 18:47
  • There's also iterator block, anonymous classes and closures which may cause quite a few compiler-generated classe to be created - so looking at non-public classes is probably less meaningful. Then there are probably various machine generated classes using techniques like T4 that often encapsulate very similar behaviour but end up in distinct classes. Api's that work via factory methods can hide a bunch of relevant classes, exposing mostly only interfaces - an interface may have no public implementers yet still be usable. All in all, tricky to measure... – Eamon Nerbonne Sep 10 '09 at 21:26
  • I would suggest using Assembly.ReflectionOnlyLoadFrom rather than Assembly.LoadFrom, just for the sake of speed really. – Jon Skeet Sep 10 '09 at 21:36
  • I have now tried using Assembly.ReflectionOnlyLoadFrom instead. With a different result: 1785 public classes. This is probably due to 68 failures (instead of 40). mscorlib.dll and System.dll do not fail and give the same number of public classes. – Peter Mortensen Sep 10 '09 at 21:59
  • Note to update 4: the full text of the paper may actually be available at http://pubs.acs.org/doi/full/10.1021/pr900721e But I have only tested this at work. Does it work for any of you? Otherwise it should be possible by registering at ACS (as "Nonmember/Global Partner"), login and open the page above. – Peter Mortensen Jan 05 '10 at 09:34
  • Related: *[Per version (1.0,1.1,2.0,3.0,3.5), how many classes are in the .NET framework?](http://stackoverflow.com/questions/469984)* – Peter Mortensen Aug 31 '12 at 19:23
  • Related: *[Exactly how large is the .NET (3.5) Framework Class Library?](http://stackoverflow.com/questions/2137444)* – Peter Mortensen Aug 31 '12 at 19:24

1 Answers1

19

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();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Would the list of assemblies come from the 89 DLL files in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 ? (For .NET 2.0 on my system) – Peter Mortensen Sep 10 '09 at 19:26
  • Could be - for .NET 2.0. It seems to change for every version. The "Reference Assemblies" directory is promising, but for v3.5 that misses out mscorlib.dll and System.dll... – Jon Skeet Sep 10 '09 at 19:48
  • I get the result of 5265 public classes for .NET 2.0 - I have added a code listing to the question. I will try with .NET 3.5 SP1 tomorrow. – Peter Mortensen Sep 10 '09 at 21:15
  • For .NET 2.0: 5265 public classes (29 %), 12626 classes in total (69%), 18317 types. If 29% are public classes in .NET 3.5 SP1 then the number of public classes in .NET 3.5 SP1 is 0.29 x 40513 = 11749. – Peter Mortensen Sep 14 '09 at 19:12
  • I think the actual number is significantly lower than the previous estimate: 9911 public classes for .NET 3.5 SP1. See updated question for details. – Peter Mortensen Sep 17 '09 at 22:52