Interfaces in C# Assignment Help
INTERFACES
- Interfaces define variables, methods, and events; these are the elements of an interface.
- Interfaces contain only the declaration part.
- It can be defined in the deriving className.
- It is used to implement multiple inheritance.
Declaring Interfaces
- Interfaces are declared using the keyword interface.
- It is related to classNamedeclaration.
- By default Interface statements are public
Interfaces in C# Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com
Interface Declaration Example
public interface interface_name
{
{'// interface members'}
void showTrans ();
double getAmount();
}
Example
The following example demonstrates the usage of interface in implementing multiple inheritance
using System;
namespace Inheritance
{
classNameShape
{
public void setW (int w)
{
width = w;
}
public void setH (int h)
{
height = h;
}
protected int width;
protected int height;
}
public interface Paint // Base classNamePaintCost
{
int getCost(int area);
}
classNameRectangle : Shape, Paint // Derived className
{
public int getArea()
{
return (width * height);
}
public int getCost(int area)
{
return area * 70;
}
}
classNameRectangleTester
{
static void Main(string[] args)
{
Rectangle Rect = new Rectangle();
int area;
Rect.setW(5);
Rect.setH (10);
area = Rect.getArea();
{'// Print the area of the object.'}
Console.WriteLine("Total area: {0}", Rect.getArea());
Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
Console.ReadKey();
}
}
}
Output: Total area: 50
Total paint cost: $3500