C Sharp/Assignment Operators

From Meshplex

Jump to: navigation, search
Image:Csharp_programming.gif

Main Home

Basics
C# Tutorial Home
C# - Introduction to Visual Studio IDE
Introduction to C#
C# - Overview
C# - Statements
C# - Data Types
C# - Variables
C# - Operators

C# - Flow Control
C# - Variables II
C# - Functions and Methods
C# - Classes and Objects I
C# - Enumerations
C# - Dates and Times
C# - Random Numbers

Advanced
C# - Inheritance
C# - Polymorphism
C# - Garbage Collection
C# - Operator Overloading
C# - Encapsulation
C# - Properties
C# - Indexers
C# - Exceptions
C# - GUI
C# - Delegates
C# - Events
C# - Components
C# - Multithreading
C# - Regular Expressions
C# - Graphics and Multimedia
C# - Files and Streams
C# - XML
C# - Database, SQL and ADO.NET
C# - ASP.NET Web Forms and Web Controls
C# - Web Services
C# - Network Programming
C# - Datastructures and Collections
C# - Enumerations and Iterators
C# - .NET Assemblies
C# - CLR
C# - Visual Studio Debugger
C# - Namespaces
C# - Generics
C# - MS Intermediate Language
C# - Deploying Windows Application

[edit] C# assignment Operator


  • In this section we will cover C# assignment operators
  • Refer to the list below for assignment operator usage


C# Assignment Operators
X (oerator) (expression)    Equivalent to X (operator) 2

Operator Description
= Basic assignment operator Equivalent to: X = 2
*= Compound assignment operator Equivalent to: X = X * 2
/= Compound assignment operator Equivalent to: X = X / 2
%= Compound assignment operator Equivalent to: X = X % 2
+= Compound assignment operator Equivalent to: X = X + 2
-= Compound assignment operator Equivalent to: X = X - 2
<<= Compound assignment operator Equivalent to: X = X << 2
>>= Compound assignment operator Equivalent to: X = X >> 2
&= Compound assignment operator Equivalent to: X = X & 2
^= Compound assignment operator Equivalent to: X = X ^ 2
|= Compound assignment operator Equivalent to: X = X | 2



Lets try some real C# code to get an idea of how to use the assignment operator

  1. using System;
  2.  
  3. class Program
  4. {
  5. static void Main(string[] args)
  6. {
  7. int x = 0;
  8. x += 1; //Equivelent to x = x + 1
  9. Console.WriteLine(x);
  10.  
  11. int y = 9;
  12. y += 2; //Equivelent to y = y + 2
  13. Console.WriteLine(y);
  14. Console.Read();
  15. }
  16. }

Output:

1

11


  • Line 12: y is equal to 9
  • Line 13: right side of expression: (y + 2) is (9 + 2) --- y = 11