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

Find the output of C#.Net programs | Operator Overloading | Set 1: 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 count;

        public static void operator++()
        {
            count = count+1;

        }

        public void PrintCount()
        {
            Console.WriteLine(count);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

            ++S;
            ++S;
            ++S;
            S.PrintCount();
        }
    }
}

Output:

main.cs(9,23): error CS0590: User-defined operators cannot return void
main.cs(9,28): error CS1535: Overloaded unary operator `++' takes one parameter

Explanation:

The above program will generate a compile-time error, because if you want to overload a unary operator then you need to pass at least one argument of class type. The correct overloaded method is given below:

public static Sample operator++(Sample  C)
{
    C.count = C.count+1;
    return C;
} 

Question 2:

using System;

namespace Demo
{
    class Sample
    {
        int count;

        public Sample operator ++(Sample C)
        {
            C.count = C.count + 1;
            return C;
        }

        public void PrintCount()
        {
            Console.WriteLine(count);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

            ++S;
            ++S;
            ++S;
            S.PrintCount();
        }
    }
}

Output:

main.cs(9,23): error CS0558: User-defined operator 
`Demo.Sample.operator ++(Sample)' must be declared static and public

Explanation:

The above program will generate a compile-time error. In the above program, we created a method to overload increment operator '++', In the C# method for operator overloading must be declared as static and public. Here, we did not define the overloaded method as static.

Question 3:

using System;

namespace Demo
{
    class Sample
    {
        int count;

        public static Sample operator ++(Sample C)
        {
            C.count = C.count + 1;
            return C;
        }

        public void PrintCount()
        {
            Console.WriteLine(count);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

            ++S;
            ++S;
            ++S;
            S.PrintCount();
        }
    }
}

Output:

3
Press any key to continue . . .

Explanation:

The above program will print 3 on the console screen. In the program we created a Sample class that contains a data member count, here we implemented a method to overload increment operator '++', And we created PrintCount() method to print the value of data member count.

Let's look to the Main() method of Program class, the Main() method is the entry point of the program, here we created the object of Sample class and use pre-increment operator with object S and finally print the value of data member count using PrintCount() method of Sample class.

Question 4:

using System;

namespace Demo
{
    class Sample
    {
        int count;

        public static Sample operator ++(Sample C)
        {
            C.count = C.count + 1;
            return C;
        }

        public void PrintCount()
        {
            Console.WriteLine(count);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

            S++;
            S++;
            S++;
            
            S.PrintCount();
        }
    }
}

Output:

3
Press any key to continue . . .

Explanation:

The above program will print 3 on the console screen. In the program we created a Sample class that contains a data member count, here we implemented a method to overload increment operator '++', And we created PrintCount() method to print the value of data member count.

Let's look to the Main() method of Program class, the Main() method is the entry point of the program, here we created an object of Sample class and use post-increment operator with object S and finally print the value of data member count using PrintCount() method of Sample class.

Question 5:

using System;

namespace Demo
{
    class Sample
    {
        int count;

        public static Sample operator ++()
        {
            this.count = this.count + 1;
            return this;
        }

        public void PrintCount()
        {
            Console.WriteLine(count);
        }
    }
    
    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Sample S = new Sample();

            S++;
            S++;
            S++;
            
            S.PrintCount();
        }
    }
}

Output:

main.cs(9,30): error CS1535: Overloaded unary operator `++' takes one parameter

Explanation:

The above program will generate the compile-time error, because if you want to overload a unary operator then you need to pass at least one argument of class type, and this object cannot be used in a static method.






Comments and Discussions!

Load comments ↻






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