GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — C# - Assignment Operators

Csharp Tutorial
  • C# - Home
  • C# - Overview
  • C# - Environment
  • C# - Program Structure
  • C# - Basic Syntax
  • C# - Data Types
  • C# - Type Conversion
  • C# - Variables
  • C# - Constants
  • C# - Operators
  • C# - Arithmetic Operators
  • C# - Assignment Operators
  • C# - Relational Operators
  • C# - Logical Operators
  • C# - Bitwise Operators
  • C# - Miscellaneous Operators
  • C# - Operators Precedence
  • C# Conditional Statements
  • C# - Decision Making
  • C# - If
  • C# - If Else
  • C# - Nested If
  • C# - Switch
  • C# - Nested Switch
  • C# - Switch Expressions
  • C# Control Statements
  • C# - Loops
  • C# - For Loop
  • C# - While Loop
  • C# - Do While Loop
  • C# - Nested Loops
  • C# - Break
  • C# - Continue
  • C# - Foreach Loop
  • C# - Goto Statement
  • C# OOP & Data Handling
  • C# - Encapsulation
  • C# - Methods
  • C# - Nullables
  • C# - Arrays
  • C# - Strings
  • C# - Structure
  • C# - Enums
  • C# - Classes
  • C# - Inheritance
  • C# - Polymorphism
  • C# - Operator Overloading
  • C# - Interfaces
  • C# - Namespaces
  • C# - Preprocessor Directives
  • C# - Regular Expressions
  • C# - Custom Exceptions
  • 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# - LINQ
  • C# - IEnumerable vs IEnumerator
  • C# - Anonymous Methods
  • C# - Unsafe Codes
  • C# - Tasks and Parallel Programming
  • C# - Multithreading
  • C# - Extension Methods
  • C# - Lambda Expressions
  • C# - Async and Await
  • C# Modern Features
  • C# - Tuples
  • C# - Records
  • C# - Pattern Matching Enhancements
  • C# - Top-level Statements
  • C# - Nullable Reference Types
  • C# - What's New in C# 11 / 12 / 13
  • C# - Global Usings
  • C# - File-Scoped Namespaces
  • C# Practical & Advanced Usage
  • C# - JSON & XML Handling
  • C# - Data Serialization & Deserialization
  • C# - REST API Calls with Httpclient
  • C# - Dependency Injection
  • C# - Unit Testing with NUnit, xUnit & MSTest
  • C# - Package Management with NuGet
  • C# Useful Resources
  • C# - Developer's AI Tools
  • C# - Questions and Answers
  • C# - Cheatsheet
  • C# - Quick Guide
  • C# - Useful Resources
  • C# - Discussion
  • C# - Online Compiler
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

C# - Assignment Operators



Previous
Quiz
Next

What Are Assignment Operators in C#?

C# assignment operators assign values to variables along with the assignment; in some cases, these operators perform mathematical operators.

List of C# Assignment Operators

There are following assignment operators supported by C# −

Operator Symbol Description Example
Simple Assignment = 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 Assign += 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 Assign -= 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 Assign *= 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 Assign /= 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 Assign %= 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 Assign <<= Left shift AND assignment operator C <<= 2 is same as C = C << 2
Right Shift and Assign >>= Right shift AND assignment operator C >>= 2 is same as C = C >> 2
Bitwise AND and Assign &= Bitwise AND assignment operator C &= 2 is same as C = C & 2
Bitwise XOR and Assign ^= bitwise exclusive OR and assignment operator C ^= 2 is same as C = C ^ 2
Bitwise OR and Assign |= bitwise inclusive OR and assignment operator C |= 2 is same as C = C | 2

Example of Assignment Operators

The following example demonstrates all the assignment operators available in C# −

using System;

namespace OperatorsAppl {

   class Program {
   
      static void Main(string[] args) {
         int a = 21;
         int c;
         c = a;
         Console.WriteLine("Line 1 - =  Value of c = {0}", c);
         
         c += a;
         Console.WriteLine("Line 2 - += Value of c = {0}", c);
         
         c -= a;
         Console.WriteLine("Line 3 - -=  Value of c = {0}", c);
         
         c *= a;
         Console.WriteLine("Line 4 - *=  Value of c = {0}", c);
         
         c /= a;
         Console.WriteLine("Line 5 - /=  Value of c = {0}", c);
         
         c = 200;
         c %= a;
         Console.WriteLine("Line 6 - %=  Value of c = {0}", c);
         
         c <<= 2;
         Console.WriteLine("Line 7 - <<=  Value of c = {0}", c);
         
         c >>= 2;
         Console.WriteLine("Line 8 - >>=  Value of c = {0}", c);
         
         c &= 2;
         Console.WriteLine("Line 9 - &=  Value of c = {0}", c);
         
         c ^= 2;
         Console.WriteLine("Line 10 - ^=  Value of c = {0}", c);
         
         c |= 2;
         Console.WriteLine("Line 11 - |=  Value of c = {0}", c);
         Console.ReadLine();
      }
   }
}

When the above code is compiled and executed, it produces the following result −

Line 1 - = Value of c = 21
Line 2 - += Value of c = 42
Line 3 - -= Value of c = 21
Line 4 - *= Value of c = 441
Line 5 - /= Value of c = 21
Line 6 - %= Value of c = 11
Line 7 - <<= Value of c = 44
Line 8 - >>= Value of c = 11
Line 9 - &= Value of c = 2
Line 10 - ^= Value of c = 0
Line 11 - |= Value of c = 2

More Examples on C# Assignment Operators

Example 1: Basic Assignment (=)

In this example, we assign a value to a variable and print it.

using System;

class Program
{
    static void Main()
    {
        int x = 10; // Assigning value 10 to x
        Console.WriteLine("x: " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x: 10

Example 2: Using += (Addition and Assignment)

In this example, we add and assign a value using +=.

using System;

class Program
{
    static void Main()
    {
        int x = 5;
        x += 3; // Equivalent to x = x + 3

        Console.WriteLine("x after += : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after += : 8

Example 3: Using -= (Subtraction and Assignment)

In this example, we subtract and assign a value using -=.

using System;

class Program
{
    static void Main()
    {
        int x = 10;
        x -= 4; // Equivalent to x = x - 4

        Console.WriteLine("x after -= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after -= : 6

Example 4: Using *= (Multiplication and Assignment)

In this example, we multiply and assign a value using *=.

using System;

class Program
{
    static void Main()
    {
        int x = 5;
        x *= 2; // Equivalent to x = x * 2

        Console.WriteLine("x after *= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after *= : 10

Example 5: Using /= (Division and Assignment)

In this example, we divide and assign a value using /=.

using System;

class Program
{
    static void Main()
    {
        int x = 20;
        x /= 4; // Equivalent to x = x / 4

        Console.WriteLine("x after /= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after /= : 5

Example 6: Using %= (Modulus and Assignment)

In this example, we use modulus and assign a value using %=.

using System;

class Program
{
    static void Main()
    {
        int x = 10;
        x %= 3; // Equivalent to x = x % 3

        Console.WriteLine("x after %= : " + x);
    }
}

When the above code is compiled and executed, it produces the following result −

x after %= : 1

Real-World Use Cases for Assignment Operators

Example 7: Accumulating Scores in a Game

In this example, we add points to a player's score.

using System;

class Program
{
    static void Main()
    {
        int score = 50;
        score += 10; // Add points

        Console.WriteLine("Current Score: " + score);
    }
}

When the above code is compiled and executed, it produces the following result −

Current Score: 60

Example 8: Decreasing Inventory Stock in an E-Commerce System

In this example, we decrease stock when items are sold.

using System;

class Program
{
    static void Main()
    {
        int stock = 100;
        stock -= 2; // Two items sold

        Console.WriteLine("Stock Remaining: " + stock);
    }
}

When the above code is compiled and executed, it produces the following result −

Stock Remaining: 98

Example 9: Doubling Investment Amount

In this example, we double an investment amount.

using System;

class Program
{
    static void Main()
    {
        double investment = 1000;
        investment *= 2; // Double the amount

        Console.WriteLine("Total Investment: " + investment);
    }
}

When the above code is compiled and executed, it produces the following result −

Total Investment: 2000

Best Practices

The following are the best practices while using assignment operators:

  • You should use += and -= for counters instead of x = x + 1.
  • You must check for division by zero before using /=.
  • You should use assignment operators to keep your code clean and readable.
  • You must understand operator precedence when combining them in expressions.
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.