Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello. Now I'am studying interfaces and cann't understand some things like a interface realizing. For example I have some code
C#
IModelDoc2 swModel; //IModelDoc2 is an interface. What are we doing here? 
boolstatus = swModel.Extension.SelectByID2(.....); //..... - some parameters

I can't understand how can we realize this method by calling from interface. Class can realize interface and we can call methods from class. Right? But how we realize methods from interface?

Please, explain.
Posted
Updated 29-May-13 8:40am
v3

No.

(The right term is "to implement", not "to realize".)

There are two ways to implement an interface member: explicit and implicit:
explicit: http://msdn.microsoft.com/en-us/library/aa288461%28v=vs.71%29.aspx[^],
implicit: http://msdn.microsoft.com/en-us/library/aa664590%28v=vs.71%29.aspx[^].

The apparent benefit of explicit is: if the compile-time type (declared type) of some variable/member is the class implementing the interface, explicitly implemented members cannot be called directly, you would need an interface reference to do that. But you should not cast try to cast a class or structure variable to interface type. Instead, you should properly use patterns of the interface use. Interface is a powerful abstraction tool, it allows to abstract some code from knowing the implementation. It means that this part of code should be agnostic to implementation and never receive the class reference of structure objects implementing the interface, it should receive only the interface pointer:
C#
interface MyInterface {
   void MyMethod(/*...*/);
   int MyOtherMethod(/*...*/);
   string MyProperty { get; set; }
}

class MyImplementation : MyInterface {
    // explicit implementations:
    void MyInterface.MyMethod (/*...*/) { /* ... */ }
    string MyInterface.MyProperty { get { return /*...*/; } set { something = value; /*...*/ } }
    // or even implicit:
    public int MyOtherMethod(/*...*/) { /* ... */ return /*...*/; }
    // Now, even you may have some public (better be internal) members,
    // they should not be used in implementation-agnostic code
    internal void SomeNonInterfaceMethod(/*...*/) { /*...*/ }
}

//...

MyInterface myImplementation = new MyImplementation(/*...*/);
// NOT MyInterface myImplementation = new MyImplementation(/*...*/);

//...

//some implementation-agnostic method:
void SomeUsingMethod(MyInterface implementation) { // NOT MyImplementation implementation
                                                   // it would kill the abstraction
    implementation.MyMethod(/*...*/);
    implementation.MyProperty = "some value";
    // but you cannot call
    // implementation.SomeNonInterfaceMethod(/*...*/); // won't work
    // if somewhere you need to do this
    MyImplementation violatedEncapsulationImplementation = (MyImplementation)implementation; // AVOID IT!
    violatedEncapsulationImplementation.SomeNonInterfaceMethod(/*...*/); // AVOID IT!
    // it would work, but it would be an indication of wrong code design
}


Now, you know the basic idea.

See also my past answers:
When we use abstract and when we use interface...?[^],
Difference between abstract class and interface if they have same no of methods and var[^],
How to decide to choose Abstract class or an Interface[^],
Interfaces and Polymorphism[^].

Please pay attention for the last one: interfaces introduce one additional kind of polymorphism, where the code using the polymorphic set is abstracted even from the knowledge of the nature of element type: is it a reference or value type. This is because such value type as structure can implement same interfaces as a class, the fact which is often forgotten.

—SA
 
Share this answer
 
v5
All an interface is...in its simplest form...is the implementation for a class. One of the big benefits (to me) that interfaces provide is de-coupling your code...essentially it makes your code less dependent upon other classes.

Example situation.

Say you have 3 different currencies, US Dollar, Indian Rupee, and the Euro. Without an interface you would have this code.
C#
UsDollar currency1 = new UsDollar();

To work with the respective classes. If you wanted to swap out the UsDollar for a Rupee, you'd have to swap out the code/make any changes necessary to support the next class
C#
Rupee currency1 = new Rupee();

But by using an interface, you can de-couple the dependency upon the specific class being used and use the interface instead (similar to how your example snippet you posted).

So Say you have an interface that implements an integer of CurrencyValue and a method of Exchange Rate
C#
public interface ICurrency
{
    int CurrencyValue { get; set; }
    void ExchangeRate(double percent);
}

You would then have the following 3 classes that inherit from the Interface. Remember, if a class inherits from that interface, they must implement the same as what the interface implements.
C#
public class UsDollar : ICurrency
{
    public int CurrencyValue { get; set; }

    public void ExchangeRate(double percent)
    {
        Console.WriteLine("Your Exchange Rate for Us Dollar is {0}", CurrencyValue * percent);
    }
}

public class Euro : ICurrency
{
    public int CurrencyValue { get; set; }

    public void ExchangeRate(double percent)
    {
        Console.WriteLine("Your Exchange Rate for Euro is {0}", CurrencyValue * percent);
    }
}

public class Rupee : ICurrency
{
    public int CurrencyValue { get; set; }

    public void ExchangeRate(double percent)
    {
        Console.WriteLine("Your Exchange Rate for Rupee is {0}", CurrencyValue * percent);
    }
}

So the by changing your usage from
C#
Rupee currency1 = new Rupee();

To
C#
ICurrency currency1 = new Rupee();

Your code that is using this class no longer cares if it is a Rupee, Us Dollar, Euro...or whatever other currency classes you end up creating.

Interfaces can also provide value in various design patterns as well (factor pattern).

Example Usage:

C#
ICurrency currency1 = new UsDollar();
ICurrency currency2 = new Euro();
ICurrency currency3 = new Rupee();

currency1.CurrencyValue = 10;
currency2.CurrencyValue = 13;
currency3.CurrencyValue = 16;

currency1.ExchangeRate(1.1);
currency2.ExchangeRate(1.6);
currency3.ExchangeRate(1.5);

So to answer your question
C#
IModelDoc2 swModel; //IModelDoc2 is an interface. What are we doing here? 

This should specifically be creating a new object of some class type (in my example this would be same as ICurrency currency1 = new UsDollar();). So somewhere in your code you would be assigning the value to swModel which would be some class/type that inherits from IModelDoc2.

Hope this helps/clarifies things for you.

But to read up on interfaces i would suggest following links:
http://msdn.microsoft.com/en-us/library/87d83y5b%28v=vs.80%29.aspx[^]
Interfaces in C# (For Beginners)[^]
Abstract Class versus Interface[^]
When To Use Interfaces[^]

There is plenty of other reading, but those are just a few other links
 
Share this answer
 
v3
Somewhere between your two lines of code, you're going to have a line that instantiates an instance of a class that implements the interface IModelDoc2. At that point, the variable you defined as being of type IModelDoc2 will be represented by an object that "fills in the blanks" specified in IModelDoc2's definition.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900