c sharp

C# Environment.ProcessorCount Property

If you want to get know the total processors available in your machine using C# application, ProcessorCount property is used. It is available in Environment class.

Environment Class in C# provides information about the working environment like variables, methods used, and system related information.

Environment.ProcessorCount

This property will return the total number of processors that are available in your machine/system to process the current application.

The number of processors can be based on the following:

    1. The number of processors available in the machine (can be logical also).
    2. If any of the processes that are running with a CPU utilization limit, then the total number of processors with the CPU utilization limit can be the next integer.

Syntax:

int Environment.ProcessorCount

 
Return Type:

It returns the integer type of 32 bit that specifies the processor count.

Example 1:

Let’s see how many processors are available in the system.

using System;
class Linuxhint
{
    //let's implement the ProcessorCount  property inside main method
    static public  void Main()
    {
        //get the total number of processors
        Console.WriteLine("Total Processors that are available in my machine: "+Environment.ProcessorCount);
    }
}

 
Output:


We can see that the total number of processors that are available in my machine is 8.

Explanation:

Line 5:

We are implementing our property inside the Main method.

Line 8:

   Console.WriteLine("Total Processors that are available in my machine: "+Environment.ProcessorCount);

 
Here, we are directly using the ProcessorCount property to return the total number of processors.

Example 2:

Here, we will return the processor count by storing it in an integer variable.

using System;
class Linuxhint
{
    //let's implement the ProcessorCount  property inside main method
    static public  void Main()
    {
        //get the total number of processors
        int totalprocessor_count=Environment.ProcessorCount;
        Console.WriteLine("Total Processors that are available in my machine: "+totalprocessor_count);
    }
}

 
Output:

Explanation:

Line 8:

int totalprocessor_count=Environment.ProcessorCount;

 
Here, we are storing the count inside the integer variable – totalprocessor_count.

Line 9:

Console.WriteLine("Total Processors that are available in my machine: "+totalprocessor_count);

 
Finally, we are displaying the totalprocessor_count using the Console.WriteLine() function.

Conclusion

By the end of this article, we came to know that the ProcessorCount property is used to return the processors that are available in our system. It returns the count in the form of an integer with 32 bytes. We discussed two examples by implementing this property in two ways.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain