C#: Enumeration

This entry is part 69 of 71 in the series C#

The enum keyword is used to declare an enumeration, a distinct type that consists of a set of constants called the enumerator list.

By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

Enumerators can use initializers to override the default values, as shown in the following example.

enum Months {Jan=1, Feb, Mar, Apr, May, Jun, July };

using System;

namespace Enumeration
{
    public class EnumTest
    {
        enum Months {Jan, Feb, Mar, Apr, May, Jun, July };

        static void Main()
        {
            int x = (int)Months.Mar;
            int y = (int)Months.Jun;
            Console.WriteLine("Mar = {0}", x);
            Console.WriteLine("Months.Mar = {0}", Months.Mar);
            Console.WriteLine("Jun = {0}", y);
            Console.WriteLine("Months.Jun = {0}", Months.Jun);

            Console.ReadKey();
        }
    }
}

enum1

If we change the default value of the first enumerator.
using System;

namespace Enumeration
{
    public class EnumTest
    {
        enum Months {Jan = 1, Feb, Mar, Apr, May, Jun, July };

        static void Main()
        {
            int x = (int)Months.Mar;
            int y = (int)Months.Jun;
            Console.WriteLine("Mar = {0}", x);
            Console.WriteLine("Months.Mar = {0}", Months.Mar);
            Console.WriteLine("Jun = {0}", y);
            Console.WriteLine("Months.Jun = {0}", Months.Jun);

            Console.ReadKey();
        }
    }
}

The output will be:

enum2

Posted in

C#: Try catch block

This entry is part 68 of 71 in the series C#

If an application handles exceptions that occur during the execution of a block of application code, the code must be placed within a try statement and is called a try block.

Application code that handles exceptions thrown by a try block is placed within a catch statement and is called a catch block.

Here using the previous example, we roll out a simple try catch exception block.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace GetProcess
{
    /// <summary> 
    /// Shell for the sample. 
    /// </summary> 
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();
            Console.WriteLine(currentProcess.ProcessName);

            try
            {
                Process[] localByName = Process.GetProcessesByName("notepad");
                Console.WriteLine(localByName[0].ProcessName);
            }
            catch
            {
                Console.WriteLine("Notepad not running");
            }


            // Get a process on the local computer, using the process id.  

            try
            {
                Process localById = Process.GetProcessById(6132);
                Console.WriteLine(localById.ProcessName);
            }
            catch
            {
                Console.WriteLine("Process id 6132 not running");
            }
        }

        static void Main()
        {
            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
            Console.ReadKey();
        }
    }
}

trycatch

C#: Checking if a process is running

This entry is part 67 of 71 in the series C#

For some server running process, we might actually have to monitor if a process.

If it is not running, we might actually need to restart the process.

We can check if a particular program is runing by using GetProcessesByName.  Here we check if notepad is running.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace GetProcess
{
    /// <summary> 
    /// Shell for the sample. 
    /// </summary> 
    class MyProcess
    {
        void BindToRunningProcesses()
        {
            // Get the current process.
            Process currentProcess = Process.GetCurrentProcess();
            Console.WriteLine(currentProcess.ProcessName);


            // Get all instances of Notepad running on the local 
            // computer.
            Process[] localByName = Process.GetProcessesByName("notepad");
            Console.WriteLine(localByName[0].ProcessName);


            // Get a process on the local computer, using the process id.  
            // Change the ID according to the process ID running in your Desktop
             Process localById = Process.GetProcessById(6132);
             Console.WriteLine(localById.ProcessName);

        }

        static void Main()
        {

            MyProcess myProcess = new MyProcess();
            myProcess.BindToRunningProcesses();
            Console.ReadKey();

        }
    }
}

processname

C#: Display all processes running

This entry is part 66 of 71 in the series C#

The GetProcesses method allows us to display processes running in the local computer.

We can use this method to create an array of Process components running in your computer.

We have included System.Diagnostics as GetProcesses is a method under this namespace.

using System;
using System.Diagnostics;

namespace Processes
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] processlist = Process.GetProcesses();

            foreach (Process process in processlist)
            {
                Console.WriteLine("Process: {0} ID: {1}", process.ProcessName, process.Id);
            }
            Console.ReadKey();
        }
    }
}

processes

C#: Random number generation

This entry is part 65 of 71 in the series C#

In C#, random number is generated using Random.Next method.  Usually we give it a range with a lower and upper limit.

In this example, we also make use of modulus % to arrange the outputs in lines.

This technique of using modulus % to output only 5 numbers in a line is just one of the techniques that we can use for the modulus operator.

using System;

public class RandomNumber
{
    public static void Main()
    {
        Random rnd = new Random();

        Console.WriteLine("\n5 random integers from -100 to 100:");
        for (int ctr = 1; ctr <= 5; ctr++)
        {
            Console.Write("{0,12}", rnd.Next(-100, 101));
            if (ctr % 5 == 0) 
                Console.WriteLine();
        }

        Console.WriteLine("\n10 random integers from 1000 to 10000:");
        for (int ctr = 1; ctr <= 10; ctr++)
        {
            Console.Write("{0,-8}", rnd.Next(1000, 10001));
            if (ctr % 5 == 0) 
                Console.WriteLine();
        }

        Console.WriteLine("\n20 random integers from 1 to 10:");
        for (int ctr = 1; ctr <= 20; ctr++)
        {
            Console.Write("{0,6}", rnd.Next(1, 11));
            if (ctr % 5 == 0) 
                Console.WriteLine();
        }
        Console.ReadKey();
    }
}

random