get system cpu count in csharp

To get the system CPU count in C#, you can use the Environment.ProcessorCount property. This property returns the number of processors on the current machine.

Here's an example code snippet:

main.cs
using System;

class Program
{
    static void Main(string[] args)
    {
        int cpuCount = Environment.ProcessorCount;
        Console.WriteLine("CPU count: {0}", cpuCount);
    }
}
187 chars
11 lines

The Environment.ProcessorCount property returns an integer representing the number of processors on the current machine. This value can be useful for certain types of multithreaded programming, as you can tailor the number of threads you use to the number of available processors.

Note that the Environment.ProcessorCount property returns the number of logical processors on the machine, which includes hyper-threaded cores. In many cases, this will be twice the number of physical cores.

related categories

gistlibby LogSnag