Re: interface question
From: Christopher Kimbell (a_at_b.c)
Date: 05/08/04
- Next message: SamSpade: "Re: How to use PARAFORMAT"
- Previous message: Bob Grommes: "Re: How to assign a block of text to a string in C#"
- In reply to: John Underwood: "interface question"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 8 May 2004 22:49:56 +0200
Interfaces provide another level of abstraction, you can have two objects
that implement the same interface, but are implemented completely different.
The System.Data namespace contains a number of interfaces like IDataReader
and IDataAdapter. If your code uses the interfaces instead of the actual
class, it would be easier to swap between Sql and Oracle data providers.
Interfaces can also reduce the number of assemblies that has to be rebuild
when something changes. If you change an assembly that has a strong name,
any assembly that uses the changed assembly has to be rebuilt. You can get
around this with some configuration, but it's not very nice. If you seperate
the interfaces into an assembly, the users will reference this assembly, not
the implementing one. If the interface hasn't changed, you only have to
rebuild the implementing assembly. This is very useful in large complex
systems.
Interfaces are very usefull when you want to build extensible software, like
providing a plugin.
I hope this shows you the value of interfaces.
Chris
"John Underwood" <johnwadeunderwood@yahoo.com> wrote in message
news:f004e9c2.0405081144.6fd27f6@posting.google.com...
> 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
- Next message: SamSpade: "Re: How to use PARAFORMAT"
- Previous message: Bob Grommes: "Re: How to assign a block of text to a string in C#"
- In reply to: John Underwood: "interface question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|