ankitweblogic logo
W e b t u t o r i a l s

Console C#.Net Tutorial

Visual Studio.NET IDE

Define C#.NET

C# Comment

C# Variables

C# Data Types

C# Escape Sequence

C# Operators

Exercise-1

Format String

Operator Precedence

C# Keywords

Constant Variable

Type Conversion

Flow Control

Exercise Loops & Nested Loop

C# Arrays

C# Strings

User-Define Methods

Variable Scope

C# Enumerations

C# Structure

C# Exception Handling

Object Oriented Programming

C# Classes

Constructor & Destructor

C# Inheritance

C# Polymorphism

C# Operator Overloading

C# Method Overriding

C# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

System.Object

C#.Net Operator Overloading

In C#.Net, Operator overloading is declared in a same way as a method declaration, but with the operator keyword followed by the operator symbol which we are overloading. C#.Net requires that all operator overloading be declared as public and static.

Example 1 : Overload + operator and find Sum of 2 numbers.

namespace operator_sum
{
    class overload
    {
        int a;
        public void input()
        {
            Console.WriteLine("Enter number: ");
            a = Convert.ToInt32(Console.ReadLine());
        }

        public void output()
        {
            Console.WriteLine("Value = " + a);
        }

        public static overload operator+(overload obj1 , overload obj2)
        {
            overload obj = new overload();
            obj.a = obj1.a + obj2.a;
            return obj;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            overload obj1 = new overload();
            overload obj2 = new overload();
            overload obj3 = new overload();

            obj1.input();
            obj2.input();
            obj3 = obj1 + obj2; //operator overload call
            obj3.output();

            Console.ReadLine();
        }
    }
}

Example 2 : Overload > operator and find Greater number from 2 input numbers.

namespace operator_greater
{
    class greater
    {
        int a;
        public void input()
        {
            Console.WriteLine("Enter number");
            a = Convert.ToInt32(Console.ReadLine());
        }
        public static greater operator >(greater g1, greater g2)
        {
            if (g1.a > g2.a)
                return g1;
            else
                return g2;
        }

        public static greater operator <(greater g1, greater g2)
        {
            if (g1.a < g2.a)
                return g1;
            else
                return g2;
        }

        public void output()
        {
            Console.WriteLine("Value = " + a);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            greater g1 = new greater();
            greater g2 = new greater();
            greater g3 = new greater();
            g1.input();
            g2.input();
            g3 = g1 > g2; //operator '>' overload call
            g3.output();
            g3 = g1 < g2; //operator '>' overload call
            g3.output();
            Console.ReadLine();
        }
    }
}
  1. Input 2 numbers and find smallest number.
  2. Input 2 names and find it is equal or not, overload == operator.
  3. Input 2 names and find greater, overload > operator.