Click here to Skip to main content
15,897,273 members
Home / Discussions / C#
   

C#

 
GeneralFile to byte[ ] Pin
WDI3-Jan-05 4:43
WDI3-Jan-05 4:43 
GeneralRe: File to byte[ ] Pin
Wender Oliveira3-Jan-05 5:15
Wender Oliveira3-Jan-05 5:15 
GeneralRe: File to byte[ ] Pin
Dennis C. Dietrich3-Jan-05 7:40
Dennis C. Dietrich3-Jan-05 7:40 
QuestionHow to build patch Pin
Newbie_Toy3-Jan-05 3:34
Newbie_Toy3-Jan-05 3:34 
GeneralSaving Text Versions Pin
Darren Pruitt3-Jan-05 3:19
Darren Pruitt3-Jan-05 3:19 
GeneralRe: Saving Text Versions Pin
leppie3-Jan-05 5:55
leppie3-Jan-05 5:55 
GeneralInterfaces Pin
Bahadir Cambel3-Jan-05 0:05
Bahadir Cambel3-Jan-05 0:05 
GeneralRe: Interfaces Pin
jan larsen3-Jan-05 1:32
jan larsen3-Jan-05 1:32 
Interfaces is a method to provide polymorphism without having to implement multiple inheritance.
An interface is a description of a subset of properties and methods of an object. If an object is declared to implement an interface, then it must provide all the properties and methods of that interface.
A good example of the usefullness of interfaces is IDisposable. Let's say that you got a list of objects. The objects are all of different types, but you know that some of those objects needs to release their internal resources when you're done using them. Here is how you could do it:

foreach (IDisposable disposableObject in listOfVariousClassInstances)
{
   disposableObject.Dispose();
}


In the example above, you don't care if the object at hand is a Brush, BinaryWriter or a Cursor, you simply narrows the object to an IDisposable and invokes the one and only method available: Dispose().

When to create an interface: If you expect that your class hierarchy contains classes that share common methods and properties, you will of course use inheritance:

public class Base
{
   protected int i;

   public int I
   {
      get:
      {
         return i;
      }
   }
}

public class A: Base
{
   protected int j;

   public int J
   {
      get:
      {
         return j;
      }
   }
}

public class B: Base
{
   protected int k;

   public int K
   {
      get:
      {
         return k;
      }
   }
}


But suppose that you want class B to be a list of objects too. All that functionality could be easily provided by extending class System.Collections.ArrayList, but then you can't inherit from class Base. You know that other classes in your hierarchy will refer to B.I, so you now have the choice to either reinvent the functionality of ArrayList, or create a common interface for your classes.
In this case, it seems that your classes aren't that related, which in itself begs for an interface solution, but it's quite clear, that it should be easier to copy/paste the I property to all of your classes, than to reinvent ArrayList:

public interface IBase
{
   int I
   {
      get;
   }
}

public class A: IBase
{
   protected int j;
   protected int i;

   public int I
   {
      get:
      {
         return i;
      }
   }

   public int J
   {
      get:
      {
         return j;
      }
   }
}

public class B: ArrayList, IBase
{
   protected int k;
   protected int i;

   public int I
   {
      get:
      {
         return i;
      }
   }

   public int K
   {
      get:
      {
         return k;
      }
   }
}


"After all it's just text at the end of the day. - Colin Davies

"For example, when a VB programmer comes to my house, they may say 'does your pool need cleaning, sir ?' " - Christian Graus
GeneralRe: Interfaces Pin
Bahadir Cambel3-Jan-05 8:59
Bahadir Cambel3-Jan-05 8:59 
GeneralRe: Interfaces Pin
DavidNohejl3-Jan-05 9:53
DavidNohejl3-Jan-05 9:53 
GeneralRe: Interfaces Pin
Bahadir Cambel3-Jan-05 11:52
Bahadir Cambel3-Jan-05 11:52 
GeneralRe: Interfaces Pin
DavidNohejl3-Jan-05 12:30
DavidNohejl3-Jan-05 12:30 
GeneralRe: Interfaces Pin
Bahadir Cambel3-Jan-05 14:27
Bahadir Cambel3-Jan-05 14:27 
GeneralRe: Interfaces Pin
DavidNohejl3-Jan-05 14:50
DavidNohejl3-Jan-05 14:50 
GeneralRe: Interfaces Pin
jan larsen3-Jan-05 20:17
jan larsen3-Jan-05 20:17 
GeneralRe: Interfaces Pin
DavidNohejl3-Jan-05 23:56
DavidNohejl3-Jan-05 23:56 
GeneralRe: Interfaces Pin
jan larsen4-Jan-05 0:48
jan larsen4-Jan-05 0:48 
GeneralRe: Interfaces Pin
DavidNohejl4-Jan-05 1:35
DavidNohejl4-Jan-05 1:35 
Generalabout the xmltextreader Pin
dhol2-Jan-05 19:29
dhol2-Jan-05 19:29 
GeneralRe: about the xmltextreader Pin
DavidNohejl3-Jan-05 9:35
DavidNohejl3-Jan-05 9:35 
GeneralRe: about the xmltextreader Pin
dhol3-Jan-05 16:36
dhol3-Jan-05 16:36 
GeneralRe: about the xmltextreader Pin
DavidNohejl4-Jan-05 1:30
DavidNohejl4-Jan-05 1:30 
GeneralRe: about the xmltextreader Pin
dhol4-Jan-05 17:45
dhol4-Jan-05 17:45 
GeneralRe: about the xmltextreader Pin
dhol4-Jan-05 17:59
dhol4-Jan-05 17:59 
General'Locking' entire folder Pin
kshet262-Jan-05 18:34
kshet262-Jan-05 18:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.