VisualBuilder
  Home > Csharp > Tutorials > Operator Overloading in C sharp -1 - C# Tutorial
Tell a friend
Link to us
Total Members
      Members: 84606
     
Sitemap Forum Chat
Home
C# Tutorial Home
1 . Introduction to C sharp
2 . Control Statement: Selection Statement in C sharp
3 . Control Statements – Loops Statement
4 . Methods
5 . Namespaces
6 . Introduction to Classes:
7 . Inheritance:
8 . Polymorphism
9 . Properties
10 . Indexers
11 . Structs
12 . Interfaces
13 . Delegates:
14 . Exception Handling:
15 . Attributes
16 . Enums
17 . Encapsulation
18 . Parameter Passing in C sharp
19 . Method Overloading
20 . Database Interaction Using C sharp
21 . Operator Overloading in C sharp -1
22 . Operator Overloading in C sharp -2
23 . Operator Overloading in C sharp -2
24 . Sockets
25 . DNS [Domain Name System]
26 . Working with Files
27 . Generating Help File in C sharp
28 . Code Access Security
29 . Multi-Threading
30 . Globalization and Localization -1
31 . Working with Registry in C sharp
32 . Globalization and Localization -2
33 . Windows Service
34 . Web Service
35 . Consuming Web Services
36 . Creating Proxy Object of Web Service
37 . Creating an XML document
38 . Reading XML document in C sharp
39 . Using XMLWriter class to Write XML document in C sharp
40 . Assembly Information : Getting Permission set of the assembly
41 . Creating your own Permission Set
42 . Using C sharp Socket
 
Csharp Group Home
Csharp Discussion (7)
Csharp Members (2250)
Csharp Resources
Csharp Source Code (35)
Csharp Articles (0)
Csharp Blogs
Csharp Jobs
Csharp Components (5)
Csharp Books
Csharp Websites (25)
Csharp News (17)
Csharp Q & A (4)
- Csharp Ask Question
- Csharp Questions
- Csharp Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (432 )
Source Code (3217 )
Articles (11 )
Components (1589 )
News (880 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Csharp Tutorial
 Operator Overloading in C sharp -1
  << Prev: Database Interaction Using C sharp Next: Operator Overloading in C sharp -2 >>

There are varieties of operators available for use in C#. The C# language defines the behavior of these operators when used in an expression with the C# built-in data types. C# gives user the flexibility to change the default behaviour of the operators that means, user can define the behavior of many of the standard operators with his own structures and classes.


 


Suppose, for example, that you're writing a class that manages a set of records read from a database. If another piece of code has two objects of your class, it may want to write an expression that joins the records together into a third object. This sounds like an addition operation, and it seems natural for other pieces of code to write code like the following:


Records Records1;


Records Records2;


Records Records3;


Records3 = Records1 + Records2;


 


Your Records class would include a method that specifies "how objects of the class will behave when used in an expression with the addition operator. These methods are called user-defined operator implementations , and the object-oriented terminology for defining operator behavior in a class is called operator overloading .


Note: All operator overloading methods must be declared with both the static and public keywords.


 


Overloading Unary Plus /Unary Minus:


If you need to overload the unary plus operator, the following characteristics must be enjoyed by the method that is used to overload the unary plus.



  • A return type of your choice

  • The keyword operator

  • The operator being overloaded

  • A parameter list specifying a single parameter of the type of class or structure containing the overloaded operator method


Overloading Prefix Increment and Decrement Operator:


If you need to overload the prefix increment or prefix decrement operators in your class or structure, define a method with the following characteristics:



  • A return type specifying the type of class or operator method

  • The keyword operator

  • The operator being overloaded

  • A parameter list specifying a single parameter containing the overloaded operator method


Example: Demonstrate Overloading of Unary Plus, Unary Minus, Prefix Increment, Prefix Decrement.


 


Overloading Unary Plus


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


  class ClsOverLoadUnaryPlus


    {


    public int X;


    public int Y;


 


    public static ClsOverLoadUnaryPlus operator +( ClsOverLoadUnaryPlus RValue)


        {


            ClsOverLoadUnaryPlus NewObj = new ClsOverLoadUnaryPlus ();


      if (RValue.X < 0)


         NewObj.X = -(RValue.X);


      else


        NewObj.X = RValue.X;


      if (RValue.Y < 0)


        NewObj.Y = -(RValue.Y);


      else


        NewObj.Y = RValue.Y;


      return NewObj;


        }


    }


}


 


Overloading Unary Minus


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


class ClsOverLoadingUnaryMinus


{


    public int X;


    public int Y;


    public static ClsOverLoadingUnaryMinus operator – ( ClsOverLoadingUnaryMinus RValue)


        {


        ClsOverLoadingUnaryMinus objNew = new ClsOverLoadingUnaryMinus ();


    if (RValue.X > 0)


        objNew.X = -(RValue.X);


    else


        objNew.X = RValue.X;


    if (RValue.Y > 0)


        objNew.Y = -(RValue.Y);


    else


        objNew.Y = RValue.Y;


    return objNew;


        }


    }


}


 


Overloading Prefix Increment


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


class ClsOverloadingIncrement


{


    public int X;


    public int Y;


 


    public static ClsOverloadingIncrement operator ++( ClsOverloadingIncrement RValue)


        {


        ClsOverloadingIncrement objIncrement = new ClsOverloadingIncrement ();


    objIncrement.X = RValue.X + 1;


    objIncrement.Y = RValue.Y + 1;


    return objIncrement;


        }


    }


}


 


Overloading Prefix Decrement


 


using System;


using System.Collections.Generic;


using System.Text;


 


namespace _CSharpApplication


{


class ClsOverLoadingDecrement


{


    public int X;


    public int Y;


    public static ClsOverLoadingDecrement operator – ( ClsOverLoadingDecrement RValue)


        {


        ClsOverLoadingDecrement objDecrement = new ClsOverLoadingDecrement ();


    objDecrement.X = RValue.X - 1;


    objDecrement.Y = RValue.Y - 1;


    return objDecrement;


        }


    }


}


 


 


Form2.cs


 


using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Data;


using System.Drawing;


using System.Text;


using System.Windows.Forms;


 


namespace _CSharpApplication


{


public partial class Form2 : Form


{


    public Form2()


    {


    InitializeComponent();


    }


 


    private void button1_Click( object sender, EventArgs e)


        { //Overloading Unary Plus


        ClsOverLoadUnaryPlus objUnaryPlus = new ClsOverLoadUnaryPlus ();


    objUnaryPlus.X = -100;


    objUnaryPlus.Y = 200;


    MessageBox.Show((objUnaryPlus.X).ToString());


    MessageBox.Show((objUnaryPlus.Y).ToString());


    objUnaryPlus = +objUnaryPlus;


    MessageBox.Show((objUnaryPlus.X).ToString());


    MessageBox.Show((objUnaryPlus.Y).ToString());


        }


 


    private void button2_Click( object sender, EventArgs e)


        { //Overloading Unary Minus


        ClsOverLoadingUnaryMinus objUnaryMinus = new ClsOverLoadingUnaryMinus ();


    objUnaryMinus.X = -10;


    objUnaryMinus.Y = 20;


    MessageBox.Show((objUnaryMinus.X).ToString());


    MessageBox.Show((objUnaryMinus.Y).ToString());


    objUnaryMinus = -objUnaryMinus;


    MessageBox.Show((objUnaryMinus.X).ToString());


    MessageBox.Show((objUnaryMinus.Y).ToString());


        }


 


    private void button3_Click( object sender, EventArgs e)


        { //Overlading Prefix Increment


        ClsOverloadingIncrement objIncrement = new ClsOverloadingIncrement ();


    objIncrement.X = 100;


    objIncrement.Y = 200;


    MessageBox.Show((objIncrement.X).ToString());


    MessageBox.Show((objIncrement.Y).ToString());


    objIncrement = ++objIncrement;


    MessageBox.Show((objIncrement.X).ToString());


    MessageBox.Show((objIncrement.Y).ToString());


        }


 


    private void button4_Click( object sender, EventArgs e)


        { //Overloading Prefix Decrement


        ClsOverLoadingDecrement objDecrement = new ClsOverLoadingDecrement ();


    objDecrement.X = 100;


    objDecrement.Y = 200;


    MessageBox.Show((objDecrement.X).ToString());


    MessageBox.Show((objDecrement.Y).ToString());


    objDecrement = --objDecrement;


    MessageBox.Show((objDecrement.X).ToString());


    MessageBox.Show((objDecrement.Y).ToString());


        }


    }


}


 


 


Output:


 


 




 


Click on Overloading Unary Plus:


   


Click on Overloading Unary Minus:


   


Click on Prefix Increment:


  


Click on Prefix Decrement:


  


 


  << Prev: Database Interaction Using C sharp Next: Operator Overloading in C sharp -2 >>
Csharp Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
MSN
Video Surveillance
VoIP Internettelefonie
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising