C# Program to Overload Unary Increment (++) and Decrement (-) Operators - GeeksforGeeks

16 Nov, 2021
In C#, overloading is the common way of implementing polymorphism. It is the ability to redefine a function in more than one form. A user can implement method overloading by defining two or more methods in a class sharing the same name but with different method signatures. So in this article, we will learn how to overload unary increment and decrement operators.
In C#, the decrement operator(–) is used to decrement an integer value by one. It is of two types pre-decrement operator and post decrement operator. When this operator is placed before any variable name then such type of operator is known as pre-decrement operator, e.g., –y whereas when the operator is placed after any variable name then such type of operator is known as post-decrement operator, e.g., y–. We can also overload the decrement operator using the following syntax. Here we will pass the object as the parameter and then set the decrement value to object value and this method will return the decremented value.
Syntax:
public static GFG operator --(GFG obj)
{
    obj.value = --obj.value;
    return obj;
}
Example:
Input  : 50
Output : 49

Input  : 79
Output : 78
Example:
C#
// C# program to demonstrate overloading decrement operatorusing System;  class GFG{  // Declare integer variableprivate int value;  // Initialize data memberspublic GFG(int value){this.value = value;}  // Overload unary decrement operatorpublic static GFG operator--(GFG obj){    obj.value = --obj.value;    return obj;}  // Display method to display the valuepublic void Display(){    Console.WriteLine("Values : " + value);    Console.WriteLine();}}  class Geeks{      // Driver codestatic void Main(string[] args){          // Declare the object and assign    // the value to 50    GFG obj = new GFG(50);          // Call the unary decrement overload method    obj--;      // Call the display method    obj.Display();}}
Output:
Values : 49
In C#, the increment operator(++) is used to increment an integer value by one. It is of two types pre-increment operator and post-increment operator. When this operator is placed before any variable name then such type of operator is known as pre-increment operator, e.g., ++y whereas when the operator is placed after any variable name then such type of operator is known as post-increment operator, e.g., y++. We can also overload the increment operator using the following syntax. Here we will pass the object as the parameter and then set the increment value to object value and this method will return the incremented value.
Syntax:
public static GFG operator ++(GFG obj)
{
    obj.value = ++obj.value;
    return obj;
}
Example:
Input  : 50
Output : 51

Input  : 79
Output : 80
Example:
C#
// C# program to demonstrate overloading// increment operatorusing System;  class GFG{      // Declare integer variableprivate int value;  // Initialize data memberspublic GFG(int value){    this.value = value;}  // Overload unary increment operatorpublic static GFG operator ++(GFG obj){    obj.value = ++obj.value;    return obj;}  // Display method to display the valuepublic void Display(){    Console.WriteLine("Values : " + value);    Console.WriteLine();}}  class Geeks{  // Driver codestatic void Main(string[] args){          // Declare the object and assign the value to 50    GFG obj = new GFG(50);          // Call the unary increment overload method    obj++;          // Call the display method    obj.Display();}}
Output:
Values : 51
CSharp Operators
CSharp-programs
Picked
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Extension Method in C#
HashSet in C# with Examples
Partial Classes in C#
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
C# | Inheritance
C# | List Class
Lambda Expressions in C#
C# | .NET Framework (Basic Architecture and Component Stack)
Linked List Implementation in C#