C#
  Home arrow C# arrow Page 6 - Behind the Scenes Look at C#: Operators
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Behind the Scenes Look at C#: Operators
By: Michael Youssef
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 31
    2005-06-29

    Table of Contents:
  • Behind the Scenes Look at C#: Operators
  • Arithmetic Operators
  • The Unary Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Assignment Operators
  • The Ternary Operator
  • Operator Precedence

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Behind the Scenes Look at C#: Operators - Assignment Operators


    (Page 6 of 8 )

    You have seen the first assignment operator (=) and we have used it all over our code examples to assign a value to a variable. The assignment operators have two arguments, the left argument and the right argument ( for example x = 5;). Note that this statement only puts the value 5 in the x variable, but there is another way to assign values like this one (x = y;). In this case you copy the right value (of the y variable) to the left value (into the x variable). 

    This is the most basic assignment process. It does makes sense that the left value is a physical memory location, so it's a variable (or indexer) and the right value is the value that will be copied to the left value, so it may be a constant value (5), an expression (x + 4), a variable (x) or even a method call that returns a value. Thus, you can't write a statement such as 15 = 10, you must write it in the form x = 10. The assignment operation proceeds from the right to left because it gets the value on the right and copies it to the variable on the left.

    C# has many assignment operators other than the simple = operator. There's the addition assignment operator (+=), the subtraction assignment operator (-=), the multiplication assignment operator (*=), the division assignment operator (/=) and the module assignment operator (%=), plus other bitwise assignment operators, which I will discuss with the bitwise operators. For now I will discuss the above operators. Copy the following code into a .cs file, compile it, then run the application.

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    int x = 5;
    int y = 5;

    x = x + y;
    Console.WriteLine("y assigned to x and x = {0}",x + y);
    x += y;
    Console.WriteLine("Using the += operator x = {0}", x);

    Console.WriteLine("**********");

    x = x - y;
    Console.WriteLine("x = x - y and x = {0}",x - y);
    x -= y;
    Console.WriteLine("Using the -= operator x = {0}", x);

    Console.WriteLine("**********");

    x = x * y;
    Console.WriteLine("x = x * y and x = {0}",x * y);
    x *= y;
    Console.WriteLine("Using the *= operator x = {0}", x);

    Console.WriteLine("**********");

    x = x / y;
    Console.WriteLine("x = x / y and x = {0}",x / y);
    x /= y;
    Console.WriteLine("Using the /= operator x = {0}", x);

    Console.WriteLine("**********");

    x = x % y;
    Console.WriteLine("x = x % y and x = {0}",x % y);
    x %= y;
    Console.WriteLine("Using the %= operator x = {0}", x);
    Console.ReadLine();
    }
    }
    }

    The result is:

    The complex assignment operators don't just copy the right value into the left value; it's a little more complicated than that. For example, the += operator takes the value of the right side and adds it to the left side. It's the same as coding x = x + y, but the C# compiler doesn't process it that way because it knows exactly what the += operator does. To understand how these operators work, I will explain it using x = x operator y expression.

    More C# Articles
    More By Michael Youssef


     

    C# ARTICLES

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway