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.
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
}
Answer 1:
Output:
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.
Answer 2:
Output:
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.
Answer 3:
Output:
Explanation:
The above program will generate syntax error because we cannot use the virtual modifier with the method declaration contained in the interface.
Answer 4:
Output:
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.
Answer 5:
Output:
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#.
need an explanation for this answer? contact us directly to get an explanation for this answer