Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 29

Thread: Nooby To C#

  1. #11
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Part 1.

    In C# interface is a keyword and has a very precise meaning. An interface is a reference type, similar to an abstract class, that specifies behavior as a set of methods, properties, indexers, and events. An interface is a contract. When a class or struct implements an interface, it must adhere to the contract.
    Interfaces are a useful way to partition functionality. You should first specify interfaces and then design appropriate classes to implement the interfaces. While a class in C# can inherit from only one other class, it can implement multiple interfaces.
    Interfaces facilitate dynamic programs—you can query a class at runtime to see whether it supports a particular interface, and take action accordingly (The Plugin Architecture is Built on Interfaces). Interfaces in C# and .NET are conceptually very similar to interfaces in Microsoft's Component Object Model, but they are much easier to work with.

    Object-oriented programming is a useful paradigm for helping to design and implement large systems. Using classes helps us to achieve abstraction and encapsulation. Classes are a natural decomposition of a large system into manageable parts. Inheritance adds another tool for structuring our system, enabling us to factor out common parts into base classes, helping us to accomplish greater code reuse.

    The main purpose of an interface is to specify a contract independently of implementation. It is important to understand that conceptually the interfaces come first.
    Last edited by leoandru; Jun 16, 2005 at 06:15 PM.

  2. #12
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Part 2.

    Defining an Interface:

    You define an interface similar to defining a class. Like classes, interfaces are reference types. The big difference is that there is no implementation code in an interface; it is pure specification. Also note that an interface can have properties as well as methods (it could also have other members, such as indexers).

    For example this IAccount interface specifies operations to be performed on a bank account.

    Code:
    interface IAccount 
    { 
       void Deposit(decimal amount); 
       void Withdraw(decimal amount); 
       decimal Balance {get;} 
       void Show(); 
    }
    ----------------------------

    Implementing An Interface

    You specify that a class or struct implements an interface by using the colon notation that is employed for class inheritance. A class can also inherit both from a class and from an interface. In this case the base class should appear first in the derivation list after the colon.

    Code:
    public class AccountC : Account, IAccount 
    { 
       public void Show()
       { 
          Console.WriteLine("balance = {0}", Balance); 
       } 
    }
    In that example the class AccountC inherits from the class Account, and it implements the interface IAccount. The methods of the interface must all be implemented by Account (which I was too lazy to do), either directly or in one of the base classes in its inheritance hierarchy.

    More Solid Real World Examples To Come Later
    Last edited by leoandru; Jun 16, 2005 at 06:26 PM.

  3. #13
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    OK, I think I am more clear now, thanks Leo
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  4. #14
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Still send a real world example when you come across it. In a way I see it as 1) a safe proceedure of linking two classes and 2) run time facilty, so that different processes can act on a group of objects in memory
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #15
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    Interfaces are a useful way to partition functionality
    Hmmm, This means that a class with an account interface and a realestate interface could have those as functionality. What if a class itself is defined that has only the same methods as the interface and no added methods, is this legal? I figure that the interface would just be a part of the class definition so that more than one class with the same interface can act on something, but each of these classes have various other methods. Question is what something do they typically act on, each other? or a simple object with the interface and no extra methods?
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  6. #16
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by crosswire
    What if a class itself is defined that has only the same methods as the interface and no added methods, is this legal?
    If the method signature of the two methods match that defined in the interface then it is fully abideing by the contract of the interface and therfore it is legal.
    I figure that the interface would just be a part of the class definition so that more than one class with the same interface can act on something
    More importantly is for Different classes to execute the same functionality differently but they are seen as one "Type". Take for example data presentation. An application my provide ten different views of the same data. The best way to acomplish this is to use an interface that defines a method that will render teh data on screen. Class implementing the interface can render the data differently but they are seen as only one renderer. So at runtime there is no need to figure out which class renders the data as a pie chart and which gives u a bar graph.. hope u understood that illustration.

  7. #17
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    I think a coded illustration would be better.

    Code:
    public interface IRender {
        
        public void Render(Data d);
    
    }
    
    
    class BarGraphRender : IRender {
    
        public void Render(Data d) {
            DrawDataAsBarGraph(d);
        }
    }
    
    class ChartRender : IRender {
        
        public void Render(Data d) {
            DrawDataAsChart(d);
        }
    }
    
    
    class Application {
    
        static public int Main(string[] args) {
    
            Data d = new Data();
            IRender[] renders = {new BarGraphRender(), new ChartRender()};
            
            foreach(IRender r in renders) {
                r.Render(d);
            }
    
            return 0;
    }
    Hope u got it.


    Please not this code will not compile. It's just an illustration of a concept.
    Last edited by leoandru; Jun 16, 2005 at 07:29 PM.

  8. #18
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    That was well explained, got it now, thanks for the info.
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  9. #19
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    no prob.. glad I could help.

  10. #20
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    I think I even get the OLE interface. I used it so much before and read on it but I never got it. Now this makes more sense
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •