| | |
Class Interface
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2007
Posts: 48
Reputation:
Solved Threads: 0
Hi I am slightly confused about interfaces for an inheritance structure. Firstly can anyone explain there purpose, secondly can anyone demonstrate there use.
I have created an application that uses reflection to dynamically instantiate classes and invoke methods within those classes. During research for this app I cam accross interfaces but was lost at there purpose and the syntax of there use.
I would be greatful if anyone can help. Thanks
I have created an application that uses reflection to dynamically instantiate classes and invoke methods within those classes. During research for this app I cam accross interfaces but was lost at there purpose and the syntax of there use.
I would be greatful if anyone can help. Thanks
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 1
•
•
•
•
Hi I am slightly confused about interfaces for an inheritance structure. Firstly can anyone explain there purpose, secondly can anyone demonstrate there use.
I have created an application that uses reflection to dynamically instantiate classes and invoke methods within those classes. During research for this app I cam accross interfaces but was lost at there purpose and the syntax of there use.
I would be greatful if anyone can help. Thanks
Example: http://www.codersource.net/csharp_tu...interface.html
Just to add to this:
If you're using reflection frequently because you have several classes that have the same method, you could consider casting those objects up to a defined interface. Say you have two classes, ClassA and ClassB. Both classes have a method called Run(), which you've defined in an interface called IRunnable:
...This is a really simple example, but I hope you get a good idea. You can do similar things with Interfaces that you can do with Reflection. I think it's cleaner to use Interfaces because you then have type-safety when executing those methods when returning values and inputting parameters.
If you're using reflection frequently because you have several classes that have the same method, you could consider casting those objects up to a defined interface. Say you have two classes, ClassA and ClassB. Both classes have a method called Run(), which you've defined in an interface called IRunnable:
C# Syntax (Toggle Plain Text)
public interface IRunnable { public void Run(); } ClassA a = new ClassA(); ClassB b = new ClassB(); //casting up to IRunnable IRunnable aR = (IRunnable)a; IRunnable bR = (IRunnable)b; //you could then pass these IRunnables to any method that takes an IRunnable as a parameter, for instance. private static void MakeRun(IRunnable r) { r.Run(); } MakeRun(aR); MakeRun(bR);
...This is a really simple example, but I hope you get a good idea. You can do similar things with Interfaces that you can do with Reflection. I think it's cleaner to use Interfaces because you then have type-safety when executing those methods when returning values and inputting parameters.
Alex Cavnar, aka alc6379
![]() |
Similar Threads
- how is abstact class different from Interface?? (Java)
- interface, or enum expected errors (Java)
- class, interface, or enum expected (Java)
- Paint interface (Java)
- Java Class Class problem (Java)
- What is the difference between abstract class and interface (Java)
Other Threads in the C# Forum
- Previous Thread: Check null value in excel file
- Next Thread: Recognize and use spaces in multiline textbox
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# cast check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener mailmerge mandelbrot math mouseclick mysql operator path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox robot save saving serialization server sleep socket sockets sql sql-server statistics stream string stringformatting table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






