Re: interface question
From: Bob Grommes (bob_at_bobgrommes.com)
Date: 05/08/04
- Next message: Bob Grommes: "Re: How to assign a block of text to a string in C#"
- Previous message: Opa: "Re: Hash Table problems"
- In reply to: John Underwood: "interface question"
- Next in thread: Christopher Kimbell: "Re: interface question"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 8 May 2004 13:31:38 -0700
John,
In my view the IPoint example is a trivial example that demonstrates the
syntax without providing any insight into its usefulness.
An interface is useful IMO mainly when you have a bit of functionality that
needs to be defined in more than one class, and not all of those classes
share the same inheritance tree. If you look at where MSFT used interfaces
within the CLR, you'll see many examples of this. For example, the
IDisposable interface defines standard functionality for releasing unmanaged
resources such as database connections or file handles. That capability
might be needed across a wide variety of classes, and an interface
definition allows it to be implemented in a uniform fashion even though the
main purpose of any two classes that inherit from IDisposable might be
completely different and unrelated.
Interfaces work best when the actual implementation is likely to be very
specific to the class that inherits the interface (again, IDisposable is a
great example of this; the implementation must know a great deal about the
internals and specifics of the class it's in, but a client of IDisposable is
completely spared these details).
Another useful aspect of an interface is that you may sometimes need to
manipulate only the portion of a class that implements some interface. In
that situation you can cast to the interface rather than to the class type,
and then you have access only to the interface members. In other words you
have visibility only to the members that make sense in the current context,
plus you can access that functionality from a known, single type and you
needn't know anything else about the rest of the class and what it does.
In practice I don't create interfaces in application-level code that often,
but when I do, they are quite useful.
--Bob
"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: Bob Grommes: "Re: How to assign a block of text to a string in C#"
- Previous message: Opa: "Re: Hash Table problems"
- In reply to: John Underwood: "interface question"
- Next in thread: Christopher Kimbell: "Re: interface question"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|