This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Addition operators - + and +=
- 11 contributors
The + and += operators are supported by the built-in integral and floating-point numeric types, the string type, and delegate types.
For information about the arithmetic + operator, see the Unary plus and minus operators and Addition operator + sections of the Arithmetic operators article.
String concatenation
When one or both operands are of type string , the + operator concatenates the string representations of its operands (the string representation of null is an empty string):
String interpolation provides a more convenient way to format strings:
Beginning with C# 10, you can use string interpolation to initialize a constant string when all the expressions used for placeholders are also constant strings.
Beginning with C# 11, the + operator performs string concatenation for UTF-8 literal strings. This operator concatenates two ReadOnlySpan<byte> objects.
Delegate combination
For operands of the same delegate type, the + operator returns a new delegate instance that, when invoked, invokes the left-hand operand and then invokes the right-hand operand. If any of the operands is null , the + operator returns the value of another operand (which also might be null ). The following example shows how delegates can be combined with the + operator:
To perform delegate removal, use the - operator .
For more information about delegate types, see Delegates .
Addition assignment operator +=
An expression using the += operator, such as
is equivalent to
except that x is only evaluated once.
The following example demonstrates the usage of the += operator:
You also use the += operator to specify an event handler method when you subscribe to an event . For more information, see How to: subscribe to and unsubscribe from events .
Operator overloadability
A user-defined type can overload the + operator. When a binary + operator is overloaded, the += operator is also implicitly overloaded. A user-defined type can't explicitly overload the += operator.
C# language specification
For more information, see the Unary plus operator and Addition operator sections of the C# language specification .
- C# operators and expressions
- How to concatenate multiple strings
- Arithmetic operators
- - and -= operators
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .
Submit and view feedback for
Additional resources
- C# Basic Tutorial
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
C# - Assignment Operators
There are following assignment operators supported by C# −
Operator | Description | Example |
---|---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand | C = A + B assigns value of A + B into C |
+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | C += A is equivalent to C = C + A |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | C -= A is equivalent to C = C - A |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | C *= A is equivalent to C = C * A |
/= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | C /= A is equivalent to C = C / A |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | C %= A is equivalent to C = C % A |
<<= | Left shift AND assignment operator | C <<= 2 is same as C = C << 2 |
>>= | Right shift AND assignment operator | C >>= 2 is same as C = C >> 2 |
&= | Bitwise AND assignment operator | C &= 2 is same as C = C & 2 |
^= | bitwise exclusive OR and assignment operator | C ^= 2 is same as C = C ^ 2 |
|= | bitwise inclusive OR and assignment operator | C |= 2 is same as C = C | 2 |
The following example demonstrates all the assignment operators available in C# −
When the above code is compiled and executed, it produces the following result −
To Continue Learning Please Login
C# Assignment Operators with Examples
In c#, Assignment Operators are useful to assign a new value to the operand, and these operators will work with only one operand.
For example, we can declare and assign a value to the variable using the assignment operator ( = ) like as shown below.
If you observe the above sample, we defined a variable called “ a ” and assigned a new value using an assignment operator ( = ) based on our requirements.
The following table lists the different types of operators available in c# assignment operators.
Operator | Name | Description | Example |
---|---|---|---|
= | Equal to | It is used to assign the values to variables. | int a; a = 10 |
+= | Addition Assignment | It performs the addition of left and right operands and assigns a result to the left operand. | a += 10 is equals to a = a + 10 |
-= | Subtraction Assignment | It performs the subtraction of left and right operands and assigns a result to the left operand. | a -= 10 is equals to a = a - 10 |
*= | Multiplication Assignment | It performs the multiplication of left and right operands and assigns a result to the left operand. | a *= 10 is equals to a = a * 10 |
/= | Division Assignment | It performs the division of left and right operands and assigns a result to the left operand. | a /= 10 is equals to a = a / 10 |
%= | Modulo Assignment | It performs the modulo operation on two operands and assigns a result to the left operand. | a %= 10 is equals to a = a % 10 |
&= | Bitwise AND Assignment | It performs the Bitwise AND operation on two operands and assigns a result to the left operand. | a &= 10 is equals to a = a & 10 |
|= | Bitwise OR Assignment | It performs the Bitwise OR operation on two operands and assigns a result to the left operand. | a |= 10 is equals to a = a | 10 |
^= | Bitwise Exclusive OR Assignment | It performs the Bitwise XOR operation on two operands and assigns a result to the left operand. | a ^= 10 is equals to a = a ^ 10 |
>>= | Right Shift Assignment | It moves the left operand bit values to the right based on the number of positions specified by the second operand. | a >>= 2 is equals to a = a >> 2 |
<<= | Left Shift Assignment | It moves the left operand bit values to the left based on the number of positions specified by the second operand. | a <<= 2 is equals to a = a << 2 |
C# Assignment Operators Example
Following is the example of using assignment Operators in the c# programming language.
If you observe the above example, we defined a variable or operand “ x ” and assigning new values to that variable by using assignment operators in the c# programming language.
Output of C# Assignment Operators Example
When we execute the above c# program, we will get the result as shown below.
This is how we can use assignment operators in c# to assign new values to the variable based on our requirements.
Table of Contents
- Assignment Operators in C# with Examples
- C# Assignment Operator Example
- Output of C# Assignment Operator Example
Chapter 3 C# Operators
We have 5 types of operators in c#:
Arithmetic Operators
Comparison Operators
Assignment Operators
Logical Operators
Bitwise Operators
Arithmetic Operators: Add, subtract, multiply, divide, and remainder.
We also have increment and decrement operators, which would add or remove values.
Incrementing Values:
int x=5; //assigns x value as 5
x = x + 1; //increments x value by adding 1 to it so x = 6
x+=1;// x=7 same as above statement
all above statements add 1 to x value.
int y = ++x; // increment x and assign it to y. y=9, x=9 here.
int y = x++; //assign value x to y, and increment x value. y=9, x=10 here.
Decrementing Values:
int y = –x;
All work same as incrementing expressions shown above but these expressions will decrease values.
Comparison Operators:
Equal ==, Not Equal !=, Greater than >, Greater than or equal to >=, less than <, less than or equal to <=.
int a = 10;
console.writeline(a>b); //output would be true.
console.writeline(!(a>b));//output false
You can also add complex statements like console.writeline(!(a>b) &&(b>c))//output true.
Assignment Operators:
Assignment =, Addition assignment +=, Subtraction assignment -=, multiplication assignment *=, division assignment /=
Logical Operators: These are used in boolean operators, in condition statements.
And &&, Or ||, Not !
Logical AND:
Let’s assume we have two variables: x and y. In C#, the logical AND operator is indicated by &&.
Boolean expressions are defined as:
z = x && y
In this expression, z is true if both x and y are true; otherwise, it’ll be false.
Logical OR:
In C#, logical OR is indicated by two vertical lines (||). Considering the following expression:
a will be true, if either x or y is true.
Logical NOT
The NOT operator in C# is indicated by an exclamation mark (!) and it reverses the value of a
given variable or expression:
So, here, if x is true, y will be false, and if x is false, y will be true.
Bitwise Operators: These are used in low level operations.
And &, or |
c# uses BODMAS rule while evaluating equations like a+b*c
Last updated 5 years ago
Navigation Menu
Search code, repositories, users, issues, pull requests..., provide feedback.
We read every piece of feedback, and take your input very seriously.
Saved searches
Use saved searches to filter your results more quickly.
To see all available qualifiers, see our documentation .
- Notifications You must be signed in to change notification settings
addition-operator.md
Latest commit, file metadata and controls.
title | ms.custom | ms.date | f1_keywords | helpviewer_keywords | ms.assetid |
---|---|---|---|---|---|
+ and += operators (C# reference)
The + operator is supported by the built-in numeric types, string type, and delegate types.
For information about the arithmetic + operator, see the Unary plus and minus operators and Addition operator + sections of the Arithmetic operators article.
String concatenation
When one or both operands are of type string , the + operator concatenates the string representations of its operands:
[!code-csharp-interactive string concatenation ]
Starting with C# 6, string interpolation provides a more convenient way to format strings:
[!code-csharp-interactive string interpolation ]
Delegate combination
For operands of the same delegate type, the + operator returns a new delegate instance that, when invoked, invokes the left-hand operand and then invokes the right-hand operand. If any of the operands is null , the + operator returns the value of another operand (which also might be null ). The following example shows how delegates can be combined with the + operator:
[!code-csharp-interactive delegate combination ]
To perform delegate removal, use the - operator .
For more information about delegate types, see Delegates .
Addition assignment operator +=
An expression using the += operator, such as
is equivalent to
except that x is only evaluated once.
The following example demonstrates the usage of the += operator:
[!code-csharp-interactive += examples ]
You also use the += operator to specify an event handler method when you subscribe to an event . For more information, see How to: subscribe to and unsubscribe from events .
Operator overloadability
A user-defined type can overload the + operator. When a binary + operator is overloaded, the += operator is also implicitly overloaded. A user-defined type cannot explicitly overload the += operator.
C# language specification
For more information, see the Unary plus operator and Addition operator sections of the C# language specification .
- C# reference
- C# operators
- String interpolation
- How to: concatenate multiple strings
- Arithmetic operators
- - and -= operators
Operators are used to perform various operations on variables and values.
The following code snippet uses the assignment operator, = , to set myVariable to the value of num1 and num2 with an arithmetic operator operating on them. For example, if operator represented * , myVariable would be assigned a value of num1 * num2 .
Operators can be organized into the following groups:
- Arithmetic operators for performing traditional math evaluations.
- Assignment operators for assigning values to variables.
- Comparison operators for comparing two values.
- Logical operators for combining Boolean values.
- Bitwise operators for manipulating the bits of a number.
Arithmetic Operators
C# has the following arithmetic operators:
- Addition, + , returns the sum of two numbers.
- Subtraction, - , returns the difference between two numbers.
- Multiplication, * , returns the product of two numbers.
- Division, / , returns the quotient of two numbers.
- Modulus, % , returns the remainder of one number divided by another.
The ones above operate on two values. C# also has two unary operators:
- Increment, ++ , which increments its single operand by one.
- Decrement, -- , which decrements its single operand by one.
Unlike the other arithmetic operators, the increment and decrement operators change the value of their operand as well as return a value. They also return different results depending on if they precede or follow the operand. Preceding the operand returns the value of the operand before the operation. Following the operand returns the value of the operand after the operation.
Logical Operators
C# has the following logical operators:
- The & (and) operator returns true if both operands are true .
- The | (or) operator returns true if either operand is true .
- The ^ (xor) operator returns true if only one of its operands are true
- The ! (not) operator returns true if its single operand is false .
Note: & , | , and ^ are logical operators when the operands are bool types. When the operands are numbers they perform bitwise operations. See Bitwise Operators below.
The above operators always evaluate both operands. There are also these conditional “short circuiting” operators:
- The && (conditional and) operator returns true if both operands are true . If the first operand is false , the second operand is not evaluated.
- The || (conditional or) operator returns true if either operand is true . If the first operand is true the second operand is not evaluated.
Assignment Operators
C# includes the following assignment operators:
- The = operator assigns the value on the right to the variable on the left.
- The += operator updates a variable by incrementing its value and reassigning it.
- The -= operator updates a variable by decrementing its value and reassigning it.
- The *= operator updates a variable by multiplying its value and reassigning it.
- The /= operator updates a variable by dividing its value and reassigning it.
- The %= operator updates a variable by calculating its modulus against another value and reassigning it.
The assignment operators of the form op= , where op is a binary arithmetic operator, is a shorthand. The expression x = x op y; can be shortened to x op= y; . This compound assignment also works with the logical operators & , | and ^ .
- The ??= operator assigns the value on the right to the variable on the left if the variable on the left is null .
Comparison Operators
C# has the following comparison operators:
- Equal, == , for returning true if two values are equal.
- Not equal, != , for returning true if two values are not equal.
- Less than, < , for returning true if the left value is less than the right value.
- Less than or equal to, <= , for returning true if the left value is less than or equal to the right value.
- Greater than, > , for returning true if the left value is greater than the right value.
- Greater than or equal to, >= , for returning true if the left value is greater than or equal to the right value.
Note: for these comparison operators, if any operand is not a number ( Double.NaN or Single.NaN ) the result of the operation is false .
Bitwise Operators
C# has the following operators that perform operations on the individual bits of a number.
- Bitwise complement, ~ , inverts each bit in a number.
- Left-shift, << , shifts its left operand left by the number of bits specified in its right operand. New right positions are zero-filled.
- Right-shift, >> , shifts its left operand right by the number of bits specified in its right operand. For signed integers, left positions are filled with the value of the high-order bit. For unsigned integers, left positions are filled with zero.
- Unsigned right-shift, >>> , same as >> except left positions are always zero-filled.
- Logical AND, & , performs a bitwise logical AND of its operands.
- Logical OR, | , performs a bitwise logical OR on its operands.
- Logical XOR, ^ , performs a bitwise logical XOR on its operands.
All contributors
- StevenSwiniarski
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
Computer science.
C# operator
last modified July 5, 2023
An operator is a special symbol which indicates a certain process is carried out. Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data. An operand is one of the inputs (arguments) of an operator.
C# operator list
Category | Symbol |
---|---|
Sign operators | |
Arithmetic | |
Logical (boolean and bitwise) | |
String concatenation | |
Increment, decrement | |
Shift | |
Relational | |
Assignment | |
Member access | |
Indexing | |
Cast | |
Ternary | |
Delegate concatenation and removal | |
Object creation | |
Type information | |
Overflow exception control | |
Indirection and address | |
Lambda |
An operator usually has one or two operands. Those operators that work with only one operand are called unary operators . Those who work with two operands are called binary operators . There is also one ternary operator ?: , which works with three operands.
Certain operators may be used in different contexts. For example the + operator. From the above table we can see that it is used in different cases. It adds numbers, concatenates strings or delegates; indicates the sign of a number. We say that the operator is overloaded .
C# unary operators
C# unary operators include: +, -, ++, --, cast operator (), and negation !.
C# sign operators
The + and - signs indicate the sign of a value. The plus sign can be used to indicate that we have a positive number. It can be omitted and it is mostly done so.
The minus sign changes the sign of a value.
C# increment and decrement operators
The above two pairs of expressions do the same.
In the above example, we demonstrate the usage of both operators.
C# explicit cast operator
The explicit cast operator () can be used to cast a type to another type. Note that this operator works only on certain types.
In the example, we explicitly cast a float type to int .
Negation operator
In the example, we build a negative condition: it is executed if the inverse of the expression is valid.
C# assignment operator
The previous expression does not make sense in mathematics. But it is legal in programming. The expression adds 1 to the x variable. The right side is equal to 2 and 2 is assigned to x .
C# concatenating strings
The + operator is also used to concatenate strings.
C# arithmetic operators
Symbol | Name |
---|---|
Addition | |
Subtraction | |
Multiplication | |
/ | Division |
Remainder |
C# Boolean operators
Symbol | Name |
---|---|
logical and | |
logical or | |
negation |
Boolean operators are also called logical.
Relational operators always result in a boolean value. These two lines print false and true.
The body of the if statement is executed only if the condition inside the parentheses is met. The y > x returns true, so the message "y is greater than x" is printed to the terminal.
Three of four expressions result in true.
An example may clarify this a bit more.
In the second case, we use the || operator and use the Two method as the first operand. In this case, "Inside two" and "Pass" strings are printed to the terminal. It is again not necessary to evaluate the second operand, since once the first operand evaluates to true , the logical or is always true .
C# relational operators
Relational operators are used to compare values. These operators always result in boolean value.
Symbol | Meaning |
---|---|
less than | |
less than or equal to | |
greater than | |
greater than or equal to | |
equal to | |
not equal to |
In the code example, we have four expressions. These expressions compare integer values. The result of each of the expressions is either true or false. In C# we use == to compare numbers. Some languages like Ada, Visual Basic, or Pascal use = for comparing numbers.
C# bitwise operators
Decimal numbers are natural to humans. Binary numbers are native to computers. Binary, octal, decimal, or hexadecimal symbols are only notations of the same number. Bitwise operators work with bits of a binary number. Bitwise operators are seldom used in higher level languages like C#.
Symbol | Meaning |
---|---|
bitwise negation | |
bitwise exclusive or | |
bitwise and | |
bitwise or |
The bitwise negation operator changes each 1 to 0 and 0 to 1.
The bitwise and operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 only if both corresponding bits in the operands are 1.
The bitwise exclusive or operator performs bit-by-bit comparison between two numbers. The result for a bit position is 1 if one or the other (but not both) of the corresponding bits in the operands is 1.
C# compound assignment operators
The compound assignment operators consist of two operators. They are shorthand operators.
The a variable is initiated to one. 1 is added to the variable using the non-shorthand notation.
Using a += compound operator, we add 5 to the a variable. The statement is equal to a = a + 5; .
C# new operator
C# access operator.
The access operator [] is used with arrays, indexers, and attributes.
A dictionary is created. With domains["de"] , we get the value of the pair that has the "de" key.
C# index from end operator ^
In the example, we apply the operator on an array and a string.
We print the last letter of the word.
C# range operator ..
C# type information.
We use the sizeof and typeof operators.
We create two objects from user defined types.
We have a Base and a Derived class. The Derived class inherits from the Base class.
The as operator is used to perform conversions between compatible reference types. When the conversion is not possible, the operator returns null. Unlike the cast operation which raises an exception.
C# operator precedence
The following table shows common C# operators ordered by precedence (highest precedence first):
Operator(s) | Category | Associativity |
---|---|---|
Primary | Left | |
Unary | Left | |
Multiplicative | Left | |
Additive | Left | |
Shift | Left | |
Equality | Right | |
Logical AND | Left | |
Logical XOR | Left | |
Logical OR | Left | |
Conditional AND | Left | |
Conditional OR | Left | |
Null Coalescing | Left | |
Ternary | Right | |
Assignment | Right |
In this case, the negation operator has a higher precedence. First, the first true value is negated to false, then the | operator combines false and true, which gives true in the end.
C# associativity rule
The compound assignment operators are right to left associated. We might expect the result to be 1. But the actual result is 0. Because of the associativity. The expression on the right is evaluated first and than the compound assignment operator is applied.
C# null-conditional operator
A null-conditional operator applies a member access, ?. , or element access, ?[] , operation to its operand only if that operand evaluates to non-null. If the operand evaluates to null , the result of applying the operator is null.
We use the ?. to access the Name member and call the ToUpper method. The ?. prevents the System.NullReferenceException by not calling the ToUpper on the null value.
C# null-coalescing operator
The null-coalescing operator ?? is used to define a default value for a nullable type. It returns the left-hand operand if it is not null; otherwise it returns the right operand. When we work with databases, we often deal with absent values. These values come as nulls to the program. This operator is a convenient way to deal with such situations.
C# null-coalescing assignment operator
First, the list is assigned to null .
C# ternary operator
The ternary operator ?: is a conditional operator. It is a convenient operator for cases where we want to pick up one of two values, depending on the conditional expression.
In most countries the adulthood is based on your age. You are adult if you are older than a certain age. This is a situation for a ternary operator.
C# Lambda operator
Wherever we can use a delegate, we also can use a lambda expression. A definition for a lambda expression is: a lambda expression is an anonymous function that can contain expressions and statements. On the left side we have a group of data and on the right side an expression or a block of statements. These statements are applied on each item of the data.
In lambda expressions we do not have a return keyword. The last statement is automatically returned. And we do not need to specify types for our parameters. The compiler will guess the correct parameter type. This is called type inference.
The items of the sublist collection are printed to the terminal.
C# calculating prime numbers
In the above example, we deal with many various operators. A prime number (or a prime) is a natural number that has exactly two distinct natural number divisors: 1 and itself. We pick up a number and divide it by numbers, from 1 up to the picked up number. Actually, we do not have to try all smaller numbers; we can divide by numbers up to the square root of the chosen number. The formula will work. We use the remainder division operator.
By definition, 1 is not a prime
We skip the calculations for 2 and 3: they are primes. Note the usage of the equality and conditional or operators. The == has a higher precedence than the || operator. So we do not need to use parentheses.
This is a while loop. The i is the calculated square root of the number. We use the decrement operator to decrease the i by one each loop cycle. When the i is smaller than 1, we terminate the loop. For example, we have number 9. The square root of 9 is 3. We divide the 9 number by 3 and 2.
C# operators and expressions
In this article we covered C# operators.
List all C# tutorials .
C# Tutorial
C# examples, c# operators.
Operators are used to perform operations on variables and values.
In the example below, we use the + operator to add together two values:
Try it Yourself »
Although the + operator is often used to add together two values, like in the example above, it can also be used to add together a variable and a value, or a variable and another variable:
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations:
Operator | Name | Description | Example | Try it |
---|---|---|---|---|
+ | Addition | Adds together two values | x + y | |
- | Subtraction | Subtracts one value from another | x - y | |
* | Multiplication | Multiplies two values | x * y | |
/ | Division | Divides one value by another | x / y | |
% | Modulus | Returns the division remainder | x % y | |
++ | Increment | Increases the value of a variable by 1 | x++ | |
-- | Decrement | Decreases the value of a variable by 1 | x-- |
C# Exercises
Test yourself with exercises.
Multiply 10 with 5 , and print the result.
Start the Exercise
COLOR PICKER
Contact Sales
If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]
Report Error
If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]
Top Tutorials
Top references, top examples, get certified.
01 Career Opportunities
- Top 50 C# Interview Questions and Answers To Get Hired
02 Beginner
- C# Developer Roadmap
- Introduction to C Sharp
- New features added to C# 5.0
- What is C#: C# for Beginners
- Keywords in C#: Types of Keywords with Examples
- Identifiers in C# - A Beginner's Guide ( With Examples )
- Variables in C#: Types of Variables with Examples
- Scope of Variables in C# : Class Level, Method Level and Block Level Scope
- Data Types in C# with Examples: Value and Reference Data Type
Operators in C#: Arithmetic, Comparison, Logical and More...
- C# Best Practices for Writing Clean and Efficient Code
- Conditional Statements in C#: if , if..else, Nested if, if-else-if
- Switch Statement in C#: Difference between if-else and Switch
- Loops in C#: For, While, Do..While Loops
- Jump Statement in C#: Break, Continue, Goto, Return and Throw
- Method in C#: Learn How to Use Methods in C#
- Method Parameters in C#: Types Explained with Examples
- Arrays in C#: Create, Declare and Initialize the Arrays
03 Intermediate
- Properties in C#
- C Sharp Anonymous Method
- C Sharp Delegates and Plug-in Methods with Delegates
- C Sharp Extension method
- C Sharp Lambda Expression
- C Sharp Var data type and Anonymous Type: An Overview
- Difference Between C# Const and ReadOnly and Static
- Difference between c sharp generics and Collections with example
- Difference between int, Int16, Int32 and Int64
- Difference between ref and out parameters
- Differences between Object, Var and Dynamic type
- Partial Class, Interface or Struct in C Sharp with example
- Partial Methods in C Sharp
- Safe Type Casting with IS and AS Operator
- Understanding Delegates in C#
- Understanding Type Casting or Type Conversion in C#
- Understanding virtual, override and new keyword in C#
- Call By Value and Call by Reference in C#
- Types of Arrays in C#: Single-dimensional, Multi-dimensional and Jagged Array
- Access Modifiers in C# with Program Examples
04 Advanced
- C# Class Abstraction
- Understanding C# Interface
- Errors and Exceptions Handling in C#
- C Sharp Generic delegates Func, Action and Predicate with anonymous method
- Top 20 C# Features Every .NET Developer Must Know in 2024
- Understanding Collections and Collections Interfaces
- Method Overloading and Method Overriding in C#
- Objects and Classes in C#: Examples and Differences
- C# Constructor
05 Questions
- OOPs Interview Questions and Answers in C#
06 Training Programs
- C# Programming Course
- Full-Stack .NET Developer Certification Training
- .NET Certification Training
- Data Structures and Algorithms Training
- Operators In C#: Arithmet..
C# Programming For Beginners Free Course
Operators in c#: an overview.
Operators play a crucial role in any Programming Language, including C#. They allow us to perform various operations on data types, making our code more efficient and concise. In this article, we will explore the different types of Operators in C# and understand how they can enhance our coding efficiency.
Different Types of Operators in C# *{padding:0;margin:0;overflow:hidden}html,body{height:100%}img,span{position:absolute;width:100%;top:0;bottom:0;margin:auto}span{height:1.5em;text-align:center;font:48px/1.5 sans-serif;color:white;text-shadow:0 0 0.5em black} ▶ " frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen="">
C# provides a wide range of operators that cater to different requirements. Let's take a look at the different types of operators in C#:
Read More - C# Interview Questions For Freshers
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations in C#. They include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators allow us to manipulate numerical values and perform calculations with ease. For example, if we want to add two numbers together, we can simply use the addition operator (+).
Arithmetic Operators Example
Using system; namespace arithmetic { class dnt { static void main (string[] args) { int result; int x = 10 , y = 5 ; result = (x + y); console. writeline ( "addition operator: " + result); result = (x - y); console. writeline ( "subtraction operator: " + result); result = (x * y); console. writeline ( "multiplication operator: " + result); result = (x / y); console. writeline ( "division operator: " + result); result = (x % y); console. writeline ( "modulo operator: " + result); } } } run code >>, explanation.
This C# code in the C# Compiler defines a console application that performs basic arithmetic operations. It initializes two integers (x=10 and y=5), then calculates and prints the results of addition, subtraction, multiplication, division, and modulo operations between these two numbers using appropriate operators.
Read More - C# Interview Questions And Answers
Assignment Operators
Assignment operators are used to assign values to variables in C#. They include the simple assignment operator (=), as well as compound assignment operators such as +=, -=, *=, and /=. These operators not only assign values but also perform an operation simultaneously. For instance, the += operator adds the right-hand operand to the left-hand operand and assigns the result to the left-hand operand.
Assignment Operators Example
This C# code demonstrates various assignment operators and their usage. It initializes an integer variable "x" and then performs operations like addition, subtraction, multiplication, division, modulo, left shift, right shift, bitwise AND, bitwise XOR, and bitwise OR using their corresponding assignment operators, modifying and printing the value of "x" at each step.
Comparison Operators
Comparison operators are used to compare values in C#. They include == (equality), != (inequality), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to). These operators return a Boolean value (true or false) based on the comparison result. For example, if we want to check if two numbers are equal, we can use the equality operator (==).
Comparison Operators: Example
Using system; class program { static void main ( string [] args ) { int num1 = 10 ; int num2 = 5 ; // equal to bool isequal = num1 == num2; console.writeline( $" {num1} == {num2} : {isequal} " ); // not equal to bool isnotequal = num1 = num2; console.writeline( $" {num1} = {num2} : {isnotequal} " ); // greater than bool isgreater = num1 > num2; console.writeline( $" {num1} > {num2} : {isgreater} " ); // less than bool isless = num1 < num2; console.writeline( $" {num1} < {num2} : {isless} " ); // greater than or equal to bool isgreaterorequal = num1 >= num2; console.writeline( $" {num1} >= {num2} : {isgreaterorequal} " ); // less than or equal to bool islessorequal = num1 <= num2; console.writeline( $" {num1} <= {num2} : {islessorequal} " ); } } run code >>.
This C# program compares two integer variables, num1 and num2, using relational operators. It checks if num1 is equal to num2, not equal, greater than, less than, greater than or equal to, and less than or equal to num2. The results of these comparisons are printed on the console.
Logical Operators
Logical operators are used to perform logical operations in C#. They include && (logical AND), || (logical OR), and ! (logical NOT). These operators are commonly used in conditional statements and loops. They allow us to combine multiple conditions and control the flow of our program based on the result. For instance, if we want to check if both condition A and condition B are true, we can use the logical AND operator (&&).
Logical Operators: Example
This C# code in the C# Editor demonstrates the usage of logical operators. It initializes two boolean variables, 'a' and 'b', and then computes and prints the results of logical AND, OR, and NOT operations between these variables. The code shows how these operators combine and negate boolean values to produce logical results.
Bitwise Operators
Bitwise operators are used to perform operations at the bit level in C#. They include &, |, ^, ~, <<, and >>. These operators manipulate individual bits of an operand, allowing us to perform operations such as bitwise AND, bitwise OR, bitwise XOR, bitwise complement, left shift, and right shift. Bitwise operators are particularly useful in scenarios where we need to work with binary data or perform low-level operations.
Unary Operators
Unary operators are used to perform operations on a single operand in C#. They include ++ (increment), -- (decrement), + (positive), - (negative), ! (logical NOT), and ~ (bitwise complement). These operators allow us to modify the value of a variable or change its state based on specific conditions. For example, the increment operator (++) increases the value of a variable by 1.
Unary Operators: Example
This C# code illustrates the post-increment (a++) and post-decrement (a--) operators, as well as the pre-increment (++a) and pre-decrement (--a) operators. It initializes an integer 'a', performs these operations, and prints 'a' and the result of each operation to demonstrate how these operators affect the variable's value.
Ternary Operators
Ternary operators are unique to C# and allow us to write concise conditional expressions. The ternary operator (?:) takes three operands: a condition, a true expression, and a false expression. It evaluates the condition and returns the true expression if the condition is true, otherwise, it returns the false expression. Ternary operators are handy when we need to assign a value to a variable based on a condition in a single line of code.
Ternary Operators: Example
This C# program uses ternary operators to perform conditional operations in a concise way. It checks if 'number' is even or odd, finds the maximum of 'a' and 'b', and determines if a person can vote based on their 'age'. The results are then printed to the console. Ternary operators provide a compact way to write conditional expressions with a true/false outcome.
Precedence of Operators in C#
Operators in C# have different levels of precedence, which determines the order in which they are evaluated. It is essential to understand the precedence rules to avoid unexpected results in our code. For example, multiplication (*) has higher precedence than addition (+), so an expression like 2 + 3 * 4 will evaluate to 14, not 20. By understanding the precedence of operators, we can write code that accurately reflects our intended calculations.
Here is the Precedence of Operators in C#
In conclusion, operators are a fundamental aspect of C# programming. They allow us to perform various operations on various data types, enhancing our coding efficiency. By understanding the different types of operators in C#, their functionality, and the precedence rules, we can write code that is concise, efficient, and easy to understand. So, next time you write code in C#, remember to leverage the power of operators and take your coding efficiency to new heights!
Resources for further learning and practice
Online tutorials and courses: Explore Additional Learning and Practice Resources with us at https://www.scholarhat.com/training/csharp-certification-training
There are numerous online C# tutorials and courses available that specifically focus on C# language.
Take our Csharp skill challenge to evaluate yourself!
In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.
GET FREE CHALLENGE
Live Classes Schedule
ASP.NET Core Certification Training | Jun 14 | MON, WED, FRI | Filling Fast | ||
Advanced Full-Stack .NET Developer Certification Training | Jun 14 | MON, WED, FRI | Filling Fast | ||
Angular Certification Course | Jun 16 | SAT, SUN | Filling Fast | ||
ASP.NET Core (Project) | Jun 23 | SAT, SUN | Filling Fast | ||
Azure Developer Certification Training | Jun 23 | SAT, SUN | Filling Fast | ||
Generative AI For Software Developers | Jun 23 | SAT, SUN | Filling Fast | ||
React JS Certification Training | Best React Training Course | Jun 30 | SAT, SUN | Filling Fast |
Can't find convenient schedule? Let us know
About Author
We use cookies to make interactions with our websites and services easy and meaningful. Please read our Privacy Policy for more details.
- Python Basics
- Interview Questions
- Python Quiz
- Popular Packages
- Python Projects
- Practice Python
- AI With Python
- Learn Python3
- Python Automation
- Python Web Dev
- DSA with Python
- Python OOPs
- Dictionaries
Python Operators
Precedence and associativity of operators in python.
- Python Arithmetic Operators
- Difference between / vs. // operator in Python
- Python - Star or Asterisk operator ( * )
- What does the Double Star operator mean in Python?
- Division Operators in Python
- Modulo operator (%) in Python
- Python Logical Operators
- Python OR Operator
- Difference between 'and' and '&' in Python
- not Operator in Python | Boolean Logic
Ternary Operator in Python
- Python Bitwise Operators
Python Assignment Operators
Assignment operators in python.
- Walrus Operator in Python 3.8
- Increment += and Decrement -= Assignment Operators in Python
- Merging and Updating Dictionary Operators in Python 3.9
- New '=' Operator in Python3.8 f-string
Python Relational Operators
- Comparison Operators in Python
- Python NOT EQUAL operator
- Difference between == and is operator in Python
- Chaining comparison operators in Python
- Python Membership and Identity Operators
- Difference between != and is not operator in Python
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators.
- OPERATORS: These are the special symbols. Eg- + , * , /, etc.
- OPERAND: It is the value on which the operator is applied.
Types of Operators in Python
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Identity Operators and Membership Operators
Arithmetic Operators in Python
Python Arithmetic operators are used to perform basic mathematical operations like addition, subtraction, multiplication , and division .
In Python 3.x the result of division is a floating-point while in Python 2.x division of 2 integers was an integer. To obtain an integer result in Python 3.x floored (// integer) is used.
Operator | Description | Syntax |
---|---|---|
+ | Addition: adds two operands | x + y |
– | Subtraction: subtracts two operands | x – y |
* | Multiplication: multiplies two operands | x * y |
/ | Division (float): divides the first operand by the second | x / y |
// | Division (floor): divides the first operand by the second | x // y |
% | Modulus: returns the remainder when the first operand is divided by the second | x % y |
** | Power: Returns first raised to power second | x ** y |
Example of Arithmetic Operators in Python
Division operators.
In Python programming language Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient.
There are two types of division operators:
Float division
- Floor division
The quotient returned by this operator is always a float number, no matter if two numbers are integers. For example:
Example: The code performs division operations and prints the results. It demonstrates that both integer and floating-point divisions return accurate results. For example, ’10/2′ results in ‘5.0’ , and ‘-10/2’ results in ‘-5.0’ .
Integer division( Floor division)
The quotient returned by this operator is dependent on the argument being passed. If any of the numbers is float, it returns output in float. It is also known as Floor division because, if any number is negative, then the output will be floored. For example:
Example: The code demonstrates integer (floor) division operations using the // in Python operators . It provides results as follows: ’10//3′ equals ‘3’ , ‘-5//2’ equals ‘-3’ , ‘ 5.0//2′ equals ‘2.0’ , and ‘-5.0//2’ equals ‘-3.0’ . Integer division returns the largest integer less than or equal to the division result.
Precedence of Arithmetic Operators in Python
The precedence of Arithmetic Operators in Python is as follows:
- P – Parentheses
- E – Exponentiation
- M – Multiplication (Multiplication and division have the same precedence)
- D – Division
- A – Addition (Addition and subtraction have the same precedence)
- S – Subtraction
The modulus of Python operators helps us extract the last digit/s of a number. For example:
- x % 10 -> yields the last digit
- x % 100 -> yield last two digits
Arithmetic Operators With Addition, Subtraction, Multiplication, Modulo and Power
Here is an example showing how different Arithmetic Operators in Python work:
Example: The code performs basic arithmetic operations with the values of ‘a’ and ‘b’ . It adds (‘+’) , subtracts (‘-‘) , multiplies (‘*’) , computes the remainder (‘%’) , and raises a to the power of ‘b (**)’ . The results of these operations are printed.
Note: Refer to Differences between / and // for some interesting facts about these two Python operators.
Comparison of Python Operators
In Python Comparison of Relational operators compares the values. It either returns True or False according to the condition.
Operator | Description | Syntax |
---|---|---|
> | Greater than: True if the left operand is greater than the right | x > y |
< | Less than: True if the left operand is less than the right | x < y |
== | Equal to: True if both operands are equal | x == y |
!= | Not equal to – True if operands are not equal | x != y |
>= | Greater than or equal to True if the left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to True if the left operand is less than or equal to the right | x <= y |
= is an assignment operator and == comparison operator.
Precedence of Comparison Operators in Python
In Python, the comparison operators have lower precedence than the arithmetic operators. All the operators within comparison operators have the same precedence order.
Example of Comparison Operators in Python
Let’s see an example of Comparison Operators in Python.
Example: The code compares the values of ‘a’ and ‘b’ using various comparison Python operators and prints the results. It checks if ‘a’ is greater than, less than, equal to, not equal to, greater than, or equal to, and less than or equal to ‘b’ .
Logical Operators in Python
Python Logical operators perform Logical AND , Logical OR , and Logical NOT operations. It is used to combine conditional statements.
Operator | Description | Syntax |
---|---|---|
and | Logical AND: True if both the operands are true | x and y |
or | Logical OR: True if either of the operands is true | x or y |
not | Logical NOT: True if the operand is false | not x |
Precedence of Logical Operators in Python
The precedence of Logical Operators in Python is as follows:
- Logical not
- logical and
Example of Logical Operators in Python
The following code shows how to implement Logical Operators in Python:
Example: The code performs logical operations with Boolean values. It checks if both ‘a’ and ‘b’ are true ( ‘and’ ), if at least one of them is true ( ‘or’ ), and negates the value of ‘a’ using ‘not’ . The results are printed accordingly.
Bitwise Operators in Python
Python Bitwise operators act on bits and perform bit-by-bit operations. These are used to operate on binary numbers.
Operator | Description | Syntax |
---|---|---|
& | Bitwise AND | x & y |
| | Bitwise OR | x | y |
~ | Bitwise NOT | ~x |
^ | Bitwise XOR | x ^ y |
>> | Bitwise right shift | x>> |
<< | Bitwise left shift | x<< |
Precedence of Bitwise Operators in Python
The precedence of Bitwise Operators in Python is as follows:
- Bitwise NOT
- Bitwise Shift
- Bitwise AND
- Bitwise XOR
Here is an example showing how Bitwise Operators in Python work:
Example: The code demonstrates various bitwise operations with the values of ‘a’ and ‘b’ . It performs bitwise AND (&) , OR (|) , NOT (~) , XOR (^) , right shift (>>) , and left shift (<<) operations and prints the results. These operations manipulate the binary representations of the numbers.
Python Assignment operators are used to assign values to the variables.
Operator | Description | Syntax |
---|---|---|
= | Assign the value of the right side of the expression to the left side operand | x = y + z |
+= | Add AND: Add right-side operand with left-side operand and then assign to left operand | a+=b a=a+b |
-= | Subtract AND: Subtract right operand from left operand and then assign to left operand | a-=b a=a-b |
*= | Multiply AND: Multiply right operand with left operand and then assign to left operand | a*=b a=a*b |
/= | Divide AND: Divide left operand with right operand and then assign to left operand | a/=b a=a/b |
%= | Modulus AND: Takes modulus using left and right operands and assign the result to left operand | a%=b a=a%b |
//= | Divide(floor) AND: Divide left operand with right operand and then assign the value(floor) to left operand | a//=b a=a//b |
**= | Exponent AND: Calculate exponent(raise power) value using operands and assign value to left operand | a**=b a=a**b |
&= | Performs Bitwise AND on operands and assign value to left operand | a&=b a=a&b |
|= | Performs Bitwise OR on operands and assign value to left operand | a|=b a=a|b |
^= | Performs Bitwise xOR on operands and assign value to left operand | a^=b a=a^b |
>>= | Performs Bitwise right shift on operands and assign value to left operand | a>>=b a=a>>b |
<<= | Performs Bitwise left shift on operands and assign value to left operand | a <<= b a= a << b |
Let’s see an example of Assignment Operators in Python.
Example: The code starts with ‘a’ and ‘b’ both having the value 10. It then performs a series of operations: addition, subtraction, multiplication, and a left shift operation on ‘b’ . The results of each operation are printed, showing the impact of these operations on the value of ‘b’ .
Identity Operators in Python
In Python, is and is not are the identity operators both are used to check if two values are located on the same part of the memory. Two variables that are equal do not imply that they are identical.
Example Identity Operators in Python
Let’s see an example of Identity Operators in Python.
Example: The code uses identity operators to compare variables in Python. It checks if ‘a’ is not the same object as ‘b’ (which is true because they have different values) and if ‘a’ is the same object as ‘c’ (which is true because ‘c’ was assigned the value of ‘a’ ).
Membership Operators in Python
In Python, in and not in are the membership operators that are used to test whether a value or variable is in a sequence.
Examples of Membership Operators in Python
The following code shows how to implement Membership Operators in Python:
Example: The code checks for the presence of values ‘x’ and ‘y’ in the list. It prints whether or not each value is present in the list. ‘x’ is not in the list, and ‘y’ is present, as indicated by the printed messages. The code uses the ‘in’ and ‘not in’ Python operators to perform these checks.
in Python, Ternary operators also known as conditional expressions are operators that evaluate something based on a condition being true or false. It was added to Python in version 2.5.
It simply allows testing a condition in a single line replacing the multiline if-else making the code compact.
Syntax : [on_true] if [expression] else [on_false]
Examples of Ternary Operator in Python
The code assigns values to variables ‘a’ and ‘b’ (10 and 20, respectively). It then uses a conditional assignment to determine the smaller of the two values and assigns it to the variable ‘min’ . Finally, it prints the value of ‘min’ , which is 10 in this case.
In Python, Operator precedence and associativity determine the priorities of the operator.
Operator Precedence in Python
This is used in an expression with more than one operator with different precedence to determine which operation to perform first.
Let’s see an example of how Operator Precedence in Python works:
Example: The code first calculates and prints the value of the expression 10 + 20 * 30 , which is 610. Then, it checks a condition based on the values of the ‘name’ and ‘age’ variables. Since the name is “ Alex” and the condition is satisfied using the or operator, it prints “Hello! Welcome.”
Operator Associativity in Python
If an expression contains two or more operators with the same precedence then Operator Associativity is used to determine. It can either be Left to Right or from Right to Left.
The following code shows how Operator Associativity in Python works:
Example: The code showcases various mathematical operations. It calculates and prints the results of division and multiplication, addition and subtraction, subtraction within parentheses, and exponentiation. The code illustrates different mathematical calculations and their outcomes.
To try your knowledge of Python Operators, you can take out the quiz on Operators in Python .
Python Operator Exercise Questions
Below are two Exercise Questions on Python Operators. We have covered arithmetic operators and comparison operators in these exercise questions. For more exercises on Python Operators visit the page mentioned below.
Q1. Code to implement basic arithmetic operations on integers
Q2. Code to implement Comparison operations on integers
Explore more Exercises: Practice Exercise on Operators in Python
Please Login to comment...
Similar reads.
- python-basics
- Python-Operators
Improve your Coding Skills with Practice
What kind of Experience do you want to share?
- Stack Overflow Public questions & answers
- Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
- Talent Build your employer brand
- Advertising Reach developers & technologists worldwide
- Labs The future of collective knowledge sharing
- About the company
Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
What is difference between addition assignment operator and plus operator with blank string?
I have a code as below:
var point = ''; point = '' + 12 + 34; console.log(point); // output = 1234
var point = ''; point += + 12 + 34; console.log(point); //output = 46
Could you explain about it?
- JavaScript has an arithmetic operator and a string operator that do entirely different things but use the same symbol ( + ). That's life. – Álvaro González May 29, 2018 at 16:16
- More about the use of assignment operator as I understand it... ecma-international.org/ecma-262/6.0/… – ficuscr May 29, 2018 at 16:17
- @ÁlvaroGonzález - JavaScript has one addition operator, which varies its action (math vs. concatenation) depending on its operands, not two separate operators with the same glyph: tc39.github.io/ecma262/#sec-addition-operator-plus – T.J. Crowder May 29, 2018 at 16:20
- Main reason is that JavaScript is pure crap :) – Regis Portalez May 29, 2018 at 17:02
4 Answers 4
The difference is grouping. This:
is equivalent to:
The indicated + is a unary + which doesn't do anything there since 12 is already a number. So we have:
which, since point starts out being "" , is "46" (a string).
In the first case, this is what happens:
- '' + 12 --> '12' (notice that now we have a string, as opposed to a number)
- '12' + 34 --> '1234' Javascript automatically coerces the number 34 to the string '34' in order to evaluate the expression
Instead, this is what happens in the second case:
- +12 --> 12 unary operator applied to the number 12 , nothing happens
- 12 + 34 --> 46 pretty standard sum
- '' + 46 --> '46' empty string summed to the number 46 , which results in the string '46'
Per Addition Assignment Operator ...
Using this operator is exactly the same as specifying: result = result + expression .
Your expression is +12 + 34 , which evaluates to the integer 46 .
You may notice that in the final step, "" combined with 46 gave us a string "46" . Again, per the aforementioned documentation...
The types of the two expressions determine the behavior of the += operator:
This would be an example of the third case. One expression is numeric ( 46 ) and the other is a string ( "" ), so these two values concatenate to "46" .
- avoid making a code sample format to the text that are not a code. Use CTRL+K to make your code to be a code sample format. – davecar21 May 31, 2018 at 3:12
Your Answer
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Sign up or log in
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
Not the answer you're looking for? Browse other questions tagged javascript or ask your own question .
- The Overflow Blog
- Introducing Staging Ground: The private space to get feedback on questions...
- How to prevent your new chatbot from giving away company secrets
- Featured on Meta
- The [tax] tag is being burninated
- The return of Staging Ground to Stack Overflow
- The 2024 Developer Survey Is Live
- Policy: Generative AI (e.g., ChatGPT) is banned
Hot Network Questions
- Is the barrier to entry for mathematics research increasing, and is it at risk of becoming less accessible in the future?
- Why didn't CPUs multiplex address pins like DRAM?
- c++ or Mathematica for large eigenvalue problem
- Why don't professors seem to use learning strategies like spaced repetition and note-taking?
- Filter by partition number when the table is partitioned by computed column
- How to justify formula for area of triangle (or parallelogram)
- Is it true that engines built in Russia are still used to launch American spacecraft?
- How to align vertically by "\shortstack" in equation in LaTeX?
- Calculating Living Area on a Concentric Shellworld
- Review not needed after review
- A story about a boy with a fever who falls in the creek and is saved by an angel and grows up to be Hitler
- Why/How is Matlab's circshift function so efficient?
- Connecting to very old Linux OS with ssh
- How do you keep the horror spooky when your players are a bunch of goofballs?
- Why does this arc die in the center?
- how do I constrain a shape key
- On a planet with 6 moons, how often would all 6 be full at the same time?
- Leaders and Rulers
- Visual Studio Code crashes with [...ERROR:process_memory_range.cc(75)] read out of range
- What is the translation of a feeler in French?
- Can campaign promises be enforced by a contract, or has it ever happened they were?
- Understanding the Amanda Knox, Guilty Verdict for Slander
- What is the U.N. list of shame and how does it affect Israel which was recently added?
- Why do airplanes sometimes turn more than 180 degrees after takeoff?
IMAGES
VIDEO
COMMENTS
In this article. The + and += operators are supported by the built-in integral and floating-point numeric types, the string type, and delegate types.. For information about the arithmetic + operator, see the Unary plus and minus operators and Addition operator + sections of the Arithmetic operators article.. String concatenation. When one or both operands are of type string, the + operator ...
BtnClickHandler2. If you want specific info about += operator, MSDN says: The += operator is also used to specify a method that will be called in response to an event; such methods are called event handlers. The use of the += operator in this context is referred to as subscribing to an event. For more info look at:
Add Two Numbers C# Examples C# Examples C# Compiler C# Exercises C# Quiz C# Server C# Certificate. C# Assignment Operators Previous Next Assignment Operators. Assignment operators are used to assign values to variables. In the example below, we use the assignment operator (=) ...
Simple assignment operator, Assigns values from right side operands to left side operand. C = A + B assigns value of A + B into C. +=. Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A.
In c#, Assignment Operators are useful to assign a new value to the operand, and these operators will work with only one operand. For example, we can declare and assign a value to the variable using the assignment operator ( =) like as shown below. int a; a = 10; If you observe the above sample, we defined a variable called " a " and ...
Assignment Operators: Assignment =, Addition assignment +=, Subtraction assignment -=, multiplication assignment *=, division assignment /= ... The NOT operator in C# is indicated by an exclamation mark (!) and it reverses the value of a. given variable or expression: y = !x.
For operands of the same delegate type, the + operator returns a new delegate instance that, when invoked, invokes the left-hand operand and then invokes the right-hand operand. If any of the operands is null, the + operator returns the value of another operand (which also might be null).The following example shows how delegates can be combined with the + operator:
Assignment operators for assigning values to variables. Comparison operators for comparing two values. Logical operators for combining Boolean values. Bitwise operators for manipulating the bits of a number. Arithmetic Operators. C# has the following arithmetic operators: Addition, +, returns the sum of two numbers.
C# compound assignment operators. The compound assignment operators consist of two operators. They are shorthand operators. a = a + 3; a += 3; The += compound operator is one of these shorthand operators. The above two expressions are equal. Value 3 is added to the a variable. Other compound operators are:
Arithmetic Assignment Comparison Logical. C# Math C# Strings. ... C# Operators Previous Next Operators. Operators are used to perform operations on variables and values. In the example below, we use the + operator to add together two values: Example int x = 100 + 50;
Operators are used to manipulate variables and values in a program. C# supports a number of operators that are classified based on the type of operations they perform. 1. Basic Assignment Operator. Basic assignment operator (=) is used to assign values to variables. For example, double x; x = 50.05; Here, 50.05 is assigned to x.
Explanation. This C# code demonstrates various assignment operators and their usage. It initializes an integer variable "x" and then performs operations like addition, subtraction, multiplication, division, modulo, left shift, right shift, bitwise AND, bitwise XOR, and bitwise OR using their corresponding assignment operators, modifying and printing the value of "x" at each step.
Assignment operators are used in programming to assign values to variables. We use an assignment operator to store and update data within a program. They enable programmers to store data in variables and manipulate that data. The most common assignment operator is the equals sign (=), which assigns the value on the right side of the operator to ...
Assignment Operators in Python. Let's see an example of Assignment Operators in Python. Example: The code starts with 'a' and 'b' both having the value 10. It then performs a series of operations: addition, subtraction, multiplication, and a left shift operation on 'b'.
1. z gets assigned value of x after x is incremented. Think of it like this: z = (x = x + 0x9e3779b97f4a7c15); The return value of an assignment is always the value of the left-hand side of the assignment after the assignment is completed. edited Jul 6, 2019 at 18:35.
1. The Graph class would have a nodes property which should only be able to have nodes added or removed but not set as a whole property. While this is great for encapsulation, there are simpler and more obvious ways to implement it than overloading the += operator. Here is one example: class Graph.
The addition assignment operator `(+=)` adds a value to a variable. `x += y` means `x = x + y` The `+=` assignment operator can also be used to add (concatenate) strings: Example: txt1 = "What a very "; txt1 += "nice day"; The result of txt1 will be: What a very nice day On the other hand adding empty String `''` will make javascript to confuse ...