Interface Example

Interface Example interface IPoint { // Property signatures: int x { get; set;} int y { get; set;} // method signature void print_points(); } class Point : IPoint { // Fields: private int _x; private int _y; // Constructor: public Point(int x, int y) { _x = x; _y = y; } // Property Implementation: public int x { get { return _x; } set { _x = value; } } public int y { get { return _y; } set { _y = value; } } // Method Implementation public void print_points() { Console.WriteLine("{0} {1}", _x, _y); } } class Program { /// /// Interface /// /// -An interface contains only the signatures of methods, properties, events or indexers. /// -An Interface can not be intiated /// -An Interface can be inherited /// -All the members are public /// -multiple inheritace is possible /// -An interface is like an abstract base class: any non-abstract type inheriting the interface must implement all its members. /// -An interface cannot be instantiated directly. /// -Interfaces contain no implementation of methods. /// -Classes and structs can inherit from more than one interface. /// -An interface can itself inherit from multiple interfaces. static void PrintPoint(IPoint p) { Console.WriteLine("x={0}, y={1}", p.x, p.y); } /// /// static void Main(string[] args) { Point p = new Point(2, 3); Console.Write("My Point: "); PrintPoint(p); p.print_points(); Console.ReadKey(); } }

Comments

Popular posts from this blog

How to get motherboard serial number, Processor ID, VolumeSerialNumber using C#?

Fiserv Interview Questions.

AngularJs - Simple Example