Operators

In C#, operators have similar syntax to other C-style programming languages. Operators are used to do calculations, assign values to variables, test for equality or inequality, and perform other operations.

The following sections list some of the most commonly used operators in C#. For a complete list of all operators, see C# Operators.

Assignment and Equality Operators

In C#, the equals sign (=) operator has the same functionality as in C and C++:

Operator

Purpose

=

Assigns a value.

==

Tests for equality.

Example

int x = 100;
if (x == 100)
{
    System.Console.WriteLine("X is equal to 100");
}

Mathematical and Logical Operators

The following is a list of the basic mathematical operators, listed in order of precedence. Use parentheses to force other ordering.

Operator

Purpose

*, /, %

Multiplication, Division, Modulus

+, -

Addition , Subtraction

&

Logical AND

^

Logical XOR

|

Logical OR

Example

int x = 1;
int y = x + 10 * 100;      // multiplication first y = 1001 
int z = (x + 10) * 100;    // addition first       z = 1100

Increment and Decrement operators

C/C++ style shortcuts are supported, including postfix and prefix operators, as shown in these examples:

Operator

Purpose

v++

Increment variable v by 1.

v+=n

Increment variable v by n.

v*=n

Multiply variable v by n.

v-=n

Subtract n from variable v.

Example

int x = 0;

int y = x++;    // x is 1, y is 0

System.Console.WriteLine("{0} {1}", x, y);

int z = ++x;    // x is 2, z is 2

System.Console.WriteLine("{0} {1}", x, z);

Relational operators

The following operators compare two values and return a bool result:

Operator

Purpose

==

Checks for equality.

!=

Checks for inequality.

>

Greater than.

<

Less than.

>=

Greater than or equal to.

<=

Less than or equal to.

Example

int x = int.Parse(System.Console.ReadLine());

if (x > 100)
{
    System.Console.WriteLine("X is greater than 100");
}

Logical Condition Operators

The logical operators are used to create more flexible condition statements by combining multiple clauses:

Operator

Purpose

&&

Conditional AND.

||

Conditional OR.

!

Conditional NOT.

Example

int x = int.Parse(System.Console.ReadLine());

if ((x >= 100) && (x <= 200))
{
    System.Console.WriteLine("X is between 100 and 200");
}

More Advanced Math Operators

To perform more advanced mathematical operations, for example, trigonometry, use the Math Frameworks class. In this example, the Sin (sine) and Sqrt (square root) methods, and PI constant are being used:

Example

double d = System.Math.Sin(System.Math.PI/2);
double e = System.Math.Sqrt(144);

Operator Overloading

C# supports operator overloading; this allows you to redefine operators to be more meaningful when used with your own data types. In the following example, a struct is created, and it stores a single day of the week in a variable type defined by an enumeration. For more information, see Structs and Enumerations. The addition operator is overloaded to make it possible to add an integer number of days to the current day, and return a new day of the week. So, Sunday with one day added to it returns Monday.

Example

using System;

// Define an DayOfWeek data type 
enum DayOfWeek { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

// Define a struct to store the methods and operators 
struct Day 
{
    private DayOfWeek day;

    // The constructor for the struct 
    public Day(DayOfWeek initialDay)
    {
        day = initialDay;
    }

    // The overloaded + operator 
    public static Day operator +(Day lhs, int rhs)
    {
        int intDay = (int)lhs.day;
        return new Day((DayOfWeek)((intDay + rhs) % 7));
    }

    // An overloaded ToString method 
    public override string ToString()
    {
        return day.ToString();
    }
}

public class Program
{
    static void Main()
    {
        // Create a new Days object called "today"
        Day today = new Day(DayOfWeek.Sunday);
        Console.WriteLine(today.ToString());

        today = today + 1;
        Console.WriteLine(today.ToString());

        today = today + 14;
        Console.WriteLine(today.ToString());
    }
}

See Also

Concepts

C# Language Primer

Reference

C# Operators