Environment.ProcessorCount Property

Definition

Gets the number of processors available to the current process.

public static int ProcessorCount { get; }

Property Value

The 32-bit signed integer that specifies the number of processors that are available.

Examples

The following example demonstrates the ProcessorCount property.

// This example demonstrates the
//     Environment.ProcessorCount property.
using System;

class Sample
{
    public static void Main()
    {
    Console.WriteLine("The number of processors " +
        "on this computer is {0}.",
        Environment.ProcessorCount);
    }
}
/*
This example produces the following results:

The number of processors on this computer is 1.
*/

Remarks

On Linux and macOS systems for all .NET versions and on Windows systems starting with .NET 6, this API returns the minimum of:

  • The number of logical processors on the machine.
  • If the process is running with CPU affinity, the number of processors that the process is affinitized to.
  • If the process is running with a CPU utilization limit, the CPU utilization limit rounded up to the next whole number.

The value returned by this API is fixed at .NET runtime startup for the process lifetime. It does not reflect changes in the environment settings while the process is running.

For more information about processor groups and logical processors, see Processor Groups.

Applies to

Produto Versões
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0