How to find the Number of CPU Cores in C#?

Finding the number of CPU cores in C# involves understanding different types of processor information. There are several ways to get processor details −

  • Physical processors − The actual number of CPU sockets
  • CPU cores − The number of physical processing units
  • Logical processors − The number of threads the CPU can handle simultaneously

These values can differ significantly. For example, a machine with 2 dual-core hyper-threading-enabled processors has 2 physical processors, 4 cores, and 8 logical processors.

CPU Architecture Example Physical CPU 1 Core 1 2 threads Core 2 2 threads Core 3 2 threads Core 4 2 threads 1 Physical 4 Cores 8 Logical Environment.ProcessorCount = 8 (Logical Processors) Most commonly used for performance tuning WMI required for physical processors and core count

The number of logical processors is easily available through the Environment class, but physical processor and core information requires WMI (Windows Management Instrumentation). Add a reference to System.Management.dll in your project. For .NET Core, install the System.Management NuGet package (Windows only).

Getting Logical Processors

The simplest method uses Environment.ProcessorCount to get the number of logical processors −

using System;

class Program {
    public static void Main() {
        Console.WriteLine("Number Of Logical Processors: {0}", 
            Environment.ProcessorCount);
    }
}

The output of the above code is −

Number Of Logical Processors: 4

Getting Physical Processors

To find the number of physical processors, use WMI to query the Win32_ComputerSystem class −

using System;
using System.Management;

class Program {
    public static void Main() {
        foreach (var item in new ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()) {
            Console.WriteLine("Number Of Physical Processors: {0}", 
                item["NumberOfProcessors"]);
        }
    }
}

The output of the above code is −

Number Of Physical Processors: 1

Getting CPU Cores

To get the total number of CPU cores, query the Win32_Processor class and sum up the core counts −

using System;
using System.Management;

class Program {
    public static void Main() {
        int coreCount = 0;
        foreach (var item in new ManagementObjectSearcher("Select * from Win32_Processor").Get()) {
            coreCount += int.Parse(item["NumberOfCores"].ToString());
        }
        Console.WriteLine("Number Of Cores: {0}", coreCount);
    }
}

The output of the above code is −

Number Of Cores: 2

Complete Example

Here's a comprehensive example that retrieves all processor information −

using System;
using System.Management;

class Program {
    public static void Main() {
        // Logical processors (easiest method)
        Console.WriteLine("Logical Processors: {0}", Environment.ProcessorCount);
        
        // Physical processors
        foreach (var item in new ManagementObjectSearcher("Select * from Win32_ComputerSystem").Get()) {
            Console.WriteLine("Physical Processors: {0}", item["NumberOfProcessors"]);
        }
        
        // CPU cores
        int coreCount = 0;
        foreach (var item in new ManagementObjectSearcher("Select * from Win32_Processor").Get()) {
            coreCount += int.Parse(item["NumberOfCores"].ToString());
        }
        Console.WriteLine("CPU Cores: {0}", coreCount);
        
        // Additional processor info
        foreach (var item in new ManagementObjectSearcher("Select * from Win32_Processor").Get()) {
            Console.WriteLine("Processor Name: {0}", item["Name"]);
            break; // Just show first processor name
        }
    }
}

The output of the above code is −

Logical Processors: 4
Physical Processors: 1
CPU Cores: 2
Processor Name: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz

Comparison of Methods

Information Type Method Requirements
Logical Processors Environment.ProcessorCount Built-in, no additional references
Physical Processors WMI Win32_ComputerSystem System.Management reference
CPU Cores WMI Win32_Processor System.Management reference

Conclusion

For most applications, Environment.ProcessorCount provides the logical processor count, which is ideal for determining optimal thread counts. Use WMI queries when you need detailed hardware information like physical processor count or core count for system analysis or performance monitoring.

Updated on: 2026-03-17T07:04:36+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements