J
John Underwood
Hi.. I was looking at interface, and I have a example in the docs i'll
paste below.. I'm not grasping what you would gain by using a
interface, does any one have a brief description of their benefit?
Thanks,
John Underwood
Visual Studio .Net example below:
Example
The following example demonstrates interface implementation. In this
example, the interface IPoint contains the property declaration, which
is responsible for setting and getting the values of the fields. The
class MyPoint contains the property implementation.
// keyword_interface.cs
// Interface implementation
using System;
interface IPoint
{
// Property signatures:
int x
{
get;
set;
}
int y
{
get;
set;
}
}
class MyPoint : IPoint
{
// Fields:
private int myX;
private int myY;
// Constructor:
public MyPoint(int x, int y)
{
myX = x;
myY = y;
}
// Property implementation:
public int x
{
get
{
return myX;
}
set
{
myX = value;
}
}
public int y
{
get
{
return myY;
}
set
{
myY = value;
}
}
}
class MainClass
{
private static void PrintPoint(IPoint p)
{
Console.WriteLine("x={0}, y={1}", p.x, p.y);
}
public static void Main()
{
MyPoint p = new MyPoint(2,3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
Output
My Point: x=2, y=3
paste below.. I'm not grasping what you would gain by using a
interface, does any one have a brief description of their benefit?
Thanks,
John Underwood
Visual Studio .Net example below:
Example
The following example demonstrates interface implementation. In this
example, the interface IPoint contains the property declaration, which
is responsible for setting and getting the values of the fields. The
class MyPoint contains the property implementation.
// keyword_interface.cs
// Interface implementation
using System;
interface IPoint
{
// Property signatures:
int x
{
get;
set;
}
int y
{
get;
set;
}
}
class MyPoint : IPoint
{
// Fields:
private int myX;
private int myY;
// Constructor:
public MyPoint(int x, int y)
{
myX = x;
myY = y;
}
// Property implementation:
public int x
{
get
{
return myX;
}
set
{
myX = value;
}
}
public int y
{
get
{
return myY;
}
set
{
myY = value;
}
}
}
class MainClass
{
private static void PrintPoint(IPoint p)
{
Console.WriteLine("x={0}, y={1}", p.x, p.y);
}
public static void Main()
{
MyPoint p = new MyPoint(2,3);
Console.Write("My Point: ");
PrintPoint(p);
}
}
Output
My Point: x=2, y=3