C#.Net find output programs (Operator Overloading) | set 3

Find the output of C#.Net programs | Operator Overloading | Set 3: Enhance the knowledge of C#.Net Operator Overloading concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 10, 2021

Question 1:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static bool operator==(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val == S2.val);
            return ret;
        }

    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2 = new Sample(20);
            Sample S3 = new Sample(10);

            if (S1 == S2)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");

            if (S1 == S3)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");
        }
    }
}

Output:

main.cs(14,28): error CS0216: The operator `Demo.Sample.operator ==(Demo.Sample, Demo.Sample)' requires a matching operator `!=' to also be defined
main.cs(5,11): warning CS0660: `Demo.Sample' defines operator == or operator != but does not override Object.Equals(object o)
main.cs(5,11): warning CS0661: `Demo.Sample' defines operator == or operator != but does not override Object.GetHashCode()

Explanation:

The above program will generate a compile-time error. If we overload "==" operator, then we need to implement "!=" also.

Question 2:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static bool operator==(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val == S2.val);
            return ret;
        }
	 
	 public static bool operator !=(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val != S2.val);
            return ret;
        }


    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2 = new Sample(20);
            Sample S3 = new Sample(10);

            if (S1 == S2)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");

            if (S1 == S3)
                Console.WriteLine("Matched");
            else
                Console.WriteLine("Not Matched");
        }
    }
}

Output:

Not Matched
Matched
Press any key to continue . . .

Explanation:

In the above program, we created a Sample class that contains a data member val, constructor, operator overloaded methods for "==" and "!=".

In the Main() method we created three objects S1, S2, and S3 initialized with 10, 20, and 10 respectively. Then we compared S1 and S2 then "Not Matched" printed on the console screen, then we compared S1 and S3 then "Matched" printed on the console screen.

Question 3:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static int operator=(Sample S)
        {
            return S.val;
        }

        public void Print()
        {
            Console.WriteLine(val);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2;
            
            S2 = S1;

            S2.Print();
        }
    }
}

Output:

main.cs(14,35): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
main.cs(14,36): error CS8124: Tuple must contain at least two elements
main.cs(15,9): error CS1519: Unexpected symbol `{' in class, struct, or interface member declaration
main.cs(15,9): error CS9010: Primary constructor body is not allowed

Explanation:

The above program will generate a compile-time error because we cannot overload assignment operator "=" in C#.

Question 4:

using System;

namespace Demo
{
    class Sample
    {
        bool val;

        public Sample(bool v)
        {
            val = v;
        }

        public static Sample operator ||(Sample S1, Sample S2)
        {
            Sample S3 = new Sample(false);

            S3.val = S1.val || S2.val;
            return S3;
        }

        public void PrintVal()
        {
            Console.WriteLine(val);
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(true);
            Sample S2 = new Sample(false);
            Sample S3;

            S3 = S1 || S2;
            
            S3.PrintVal();
        }
    }
}

Output:

main.cs(14,40): error CS1519: Unexpected symbol `||' in class, struct, or interface member declaration
main.cs(15,9): error CS1519: Unexpected symbol `{' in class, struct, or interface member declaration
main.cs(15,9): error CS9010: Primary constructor body is not allowed

Explanation:

The above program will generate a compile-time error because we cannot overload "||" operator in C#.

Question 5:

using System;

namespace Demo
{
    class Sample
    {
        int val;

        public Sample(int v)
        {
            val = v;
        }

        public static bool operator<(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val < S2.val);
            return ret;
        }

        public static bool operator >(Sample S1, Sample S2)
        {
            bool ret = false;

            ret = (S1.val > S2.val);
            return ret;
        }


    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S1 = new Sample(10);
            Sample S2 = new Sample(20);
            Sample S3 = new Sample(10);

            if (S1 < S2)
                Console.WriteLine("S1 is less than S2");
            else
                Console.WriteLine("S1 is not less than S2");

            if (S1 > S3)
                Console.WriteLine("S1 is greater than S2");
            else
                Console.WriteLine("S1 is not greater than S3");
        }
    }
}

Output:

S1 is less than S2
S1 is not greater than S3
Press any key to continue . . .

Explanation:

In the above program, we created a Sample class that contains a data member val, constructor, operator overloaded methods for "<" and ">".

In the Main() method we created three objects  S1, S2, and S3 initialized with 10, 20, and 10 respectively. Then we compared S1 and S2 using "<" operator then "S1 is less than S2" printed on the console screen, then we compared S1 and S3 using ">" operator then "S1 is not greater than S3" printed on the console screen.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.