C#
  Home arrow C# arrow Page 3 - 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 - The Unary Arithmetic Operators


    (Page 3 of 8 )

    Yes, there are two more arithmetic operators, but they are unary operators, not binary like the above operators. As we have seen, binary Operators work on two arguments, much like a method that has two parameters. The unary operators work on one argument only; it's very much like a method that has only one parameter. C# defines two unary arithmetic operators, the minus operator (-) and the plus operator (+). As usual, let's begin with an example:

    using System;
    namespace Operators
    {
    class Class1
    {
    static void Main(string[] args)
    {
    int x = -10;
    int y = +10;
    int z = +-10;
    int original = 3;
    int result;
    Console.WriteLine("x = {0}, y = {1}, z = {2}",x,y,z);

    z = -10;
    result = z * original;
    Console.WriteLine("result = {0}", result);
    result = z * +original;
    Console.WriteLine("result = {0}", result);
    Console.ReadLine();
    }
    }
    }

     
    The result will be as follows:

    The minus operator, when associated with a variable (or a value) makes it a negative value to the C# compiler. The plus operator does nothing to the variable, so the variable is displayed in its original value -- which may be negative, as in our example.

    Note the z variable, which contains the value +-10. This value actually is the same as -10, because if we say that we add nothing (+) to the value -10, it's still -10 with no modifications. The expression result = z * original; equal to 10 and the expression result = z * +original; is also equal to 10 because of that.

    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 4 hosted by Hostway