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

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# Interface

Abstract Classes & Methods

Sealed Classes, Methods

C# Properties

C# Indexer

C# Delegates

C# Generics

C# Collection

System.Object

Operator Overloading

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.

Virtual Method or Override Method

Inheritance is a mechanism of inheriting, but sometimes some properties should be modified according to the need like a son inherits legs from his parents but his walking style is different. This is known as Overriding. Using this concept derived class can modify the features of base class. It is a kind of polymorphism and very important in terms of dynamic decisions.

The primary difference between method overloading and overriding is that overloading takes place within the same class whereas when the method in the sub class performs different functionality, it is referred to as overriding. The method that is to be overridden has to be given a modifier virtual and the derived class that overrides this method should specify the modifier override for the method.
For example, the method display() is overridden.

class A
{
	public virtual void display()
	{
		Console.WriteLine("Class A");
	}
}
class B : A
{
	public override void display()
	{
		Console.WriteLine("Class B");
	}
}
class Program
{
	static void Main(string[] args)
	{
		B ob = new B();
		ob.display();
		Console.Read();
	}
}

//Calling Base Class Method
class A
{
	public virtual void display()
	{
		Console.WriteLine("Class A");
	}
}
class B : A
{
	public override void display()
	{
		base.display();
		Console.WriteLine("Class B");
	}
}
class Program
{
	static void Main(string[] args)
	{
		B ob = new B();
		ob.display();
		Console.Read();
	}
}

Exercise: Operator Overloading

  1. Overload + operator and find Sum of 2 numbers.
  2. Overload > operator and find Greater number from 2 input numbers.
Updated: 07-Feb-19