C Sharp

checked and unchecked

These two operators control overflow checking for mathematical operations. Because these operators deal specifically with error handling, they are covered in Chapter 12, "Error Handling with Exceptions." -

Mathematical Operators

The C# language supports the basic mathematical operators that almost all programming languages support: multiplication (*), division (/), addition (+), subtraction (-), and modulus (%). The first four operators are obvious in their meaning; the modulus operator produces the remainder from integer division. The following code illustrates these mathematical operators in use: -

using System;
class MathOpsApp
{
    public static void Main()
    {
        // The System.Random class is part of the .NET
        // Framework class library. Its default constructor
        // seeds the Next method using the current date/time.
        Random rand = new Random();
        int a, b, c;
        a = rand.Next() % 100; // Limit max to 99.
        b = rand.Next() % 100; // Limit max to 99.
        Console.WriteLine("a={0} b={1}", a, b);
        c = a * b;
        Console.WriteLine("a * b = {0}", c);
        // Note the following code uses integers. Therefore,
        // if a is less than  b, the result will always
        // be 0. To get a more accurate result, you would
        // need to use variables of type double or float.
        c = a / b;
        Console.WriteLine("a / b = {0}", c);
        c = a + b;
        Console.WriteLine("a + b = {0}", c);
        c = a - b;
        Console.WriteLine("a - b = {0}", c);
        c = a % b;
        Console.WriteLine("a % b = {0}", c);
    }
}

by BrainBellupdated
Advertisement: