ADVERTISEMENT
ADVERTISEMENT

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

Find the output of C#.Net programs | Interface | Set 1: Enhance the knowledge of C#.Net Interface concepts by solving and finding the output of some C#.Net programs.
Submitted by Nidhi, on February 08, 2021

Question 1:

using System;

namespace Demo
{
    interface IMath
    {
        public void Add(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
        }
    }
}

Output:

main.cs(7,21): error CS0106: The modifier `public' is not valid for this item

Explanation:

The above program will generate syntax error because we cannot use public or any other access modifier with the method declaration contained in the interface.

Question 2:

using System;

namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
        }
    }
}

Output:

Addition: 30
Press any key to continue . . .

Explanation:

In the above program, we created an interface IMath that contains the declaration of Add() method then we inherited the IMath interface in Math class and implemented Add() method in the Math class.

Let's look to the Main() method, here I created an object of Math class and called Add() method that will print the addition of given arguments.

Question 3:

using System;

namespace Demo
{
    interface IMath
    {
        virtual void Add(int a, int b);
    }

    class Math : IMath
    {
        public override void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
        }
    }
}

Output:

main.cs(7,22): error CS0106: The modifier `virtual' is not valid for this item

Explanation:

The above program will generate syntax error because we cannot use the virtual modifier with the method declaration contained in the interface.

Question 4:

using System;
namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
        void Sub(int a, int b);
    }

    class Math : IMath
    {
        public override void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public override void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a-b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            Math M = new Math();

            M.Add(10,20);
	     M.Sub(30,20);
        }
    }
}

Output:

main.cs(12,30): error CS0115: `Demo.Math.Add(int, int)' is marked as an override but no suitable method found to override
main.cs(16,30): error CS0115: `Demo.Math.Sub(int, int)' is marked as an override but no suitable method found to override

Explanation:

The above will generate compile-time errors because we cannot use the override keyword to implement the method of the interface. Actually, there is no need to use the override keyword.

Question 5:

using System;

namespace Demo
{
    interface IMath
    {
        void Add(int a, int b);
        void Sub(int a, int b);
    }

    class Math : IMath
    {
        public void Add(int a, int b)
        {
            Console.WriteLine("Addition: " + (a + b));
        }
        public void Sub(int a, int b)
        {
            Console.WriteLine("Subtraction: " + (a-b));
        }
    }

    class Program
    {
        //Entry point of the program
        static void Main(string[] args)
        {
            IMath M = new IMath();

            M.Add(10,20);
        }
    }
}

Output:

main.cs(28,23): error CS0144: Cannot create an instance of the abstract class or interface `Demo.IMath'
main.cs(5,15): (Location of the symbol related to previous error)

Explanation:

The above program will generate a syntax error because of the below statement,

IMath M = new IMath();

In the above statement, we tried to create the instance of an interface, but it is not possible in C#.



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.