C# program to overload Less Than or Equal To (=) operators

You have chosen the right place. We are sure that this C# program to overload Less Than or Equal To (=) operators article will fill all your doubts. Let’s go to the article

C# program to overload Less Than or Equal To (=) operators

C# program to overload Less Than or Equal To (=) operators

using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Sample
    {
        private int X;
        
        public Sample()
        {
            X = 0;
        }

        public void Set(int v)
        {
            X = v;
        }
        public static bool operator<=(Sample S1, Sample S2)
        {
            return (S1.X <= S2.X);
        }

        public static bool operator>=(Sample S1, Sample S2)
        {
            return (S1.X >= S2.X);
        }
        
    }

    class Program
    {
        static void Main()
        {
            Sample S1 = new Sample();
            Sample S2 = new Sample();
           
            S1.Set(5);
            S2.Set(5);

            Console.WriteLine(""+(S1 <= S2));
            Console.WriteLine(""+(S1 >= S2));
        }
    }
}
True
True

Final Words

We hope the C# program to overload Less Than or Equal To (=) operators article will address some of your concerns. Also if you have any doubts please leave a comment in the comment box.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment