Operators C Sharp Language

Operators C# Language

Operators play a vital role in any programming language, including C#. They allow you to perform various ope

rations on data, from simple arithmetic calculations to more complex comparisons and logical operations. In this post, we’ll explore some of the most common operators in the C# language, along with examples of how to use them.

Arithmetic Operators: These operators are used for basic mathematical operations.

  • Addition (+): Adds two numbers together. int result = 5 + 3; // result is 8
  • Subtraction (-): Subtracts one number from another. int result = 10 - 5; // result is 5
  • Multiplication (*): Multiplies two numbers. int result = 4 * 6; // result is 24
  • Division (/): Divides one number by another. double result = 10.0 / 3.0; // result is 3.333...
  • Modulus (%): Returns the remainder of a division. int result = 10 % 3; // result is 1

Comparison Operators: These operators are used to compare values and return a Boolean result.

  • Equal (==): Checks if two values are equal. bool isEqual = (5 == 5); // isEqual is true
  • Not Equal (!=): Checks if two values are not equal. bool isNotEqual = (5 != 10); // isNotEqual is true
  • Greater Than (>): Checks if one value is greater than another. bool isGreaterThan = (10 > 5); // isGreaterThan is true
  • Less Than (<): Checks if one value is less than another. bool isLessThan = (3 < 7); // isLessThan is true

Logical Operators: These operators are used for combining and manipulating Boolean values.

  • Logical AND (&&): Returns true if both conditions are true. bool result = (true && true); // result is true
  • Logical OR (||): Returns true if at least one condition is true. bool result = (true || false); // result is true
  • Logical NOT (!): Inverts the value of a Boolean expression. bool result = !true; // result is false

Assignment Operators: These operators are used to assign values to variables.

  • Assignment (=): Assigns a value to a variable. int number = 42;
  • Addition Assignment (+=): Adds a value to the current value of a variable and assigns the result. int total = 10; total += 5; // total is now 15
  • Subtraction Assignment (-=): Subtracts a value from the current value of a variable and assigns the result. int total = 20; total -= 8; // total is now 12

Discover more from PiEmbSysTech

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from PiEmbSysTech

Subscribe now to keep reading and get access to the full archive.

Continue reading