Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Interface :

C#
public interface Interface2
   {
       void get2();
       bool insert2(User U);
   }




Implementation:

C#
public class HomeController : Controller
   {
       Interfaces _iInterfaces;
       Interface2 _iInterface2;

       public HomeController(Interfaces iInterface,Interface2 iInterface2)
       {
           _iInterfaces = iInterface;
           _iInterface2 = iInterface2;
       }



       [HttpPost]
       public ActionResult Index(User objUser)
       {

           _iInterfaces.insert(objUser);
           _iInterface2.insert2(objUser);

           return View();
       }



What i want to know is my Interface2 has 2 methods declared. I am implementing the interface in a class but only calling 1 method(insert2) out of 2 methods declared in interface.

Does this satisfy the usage of Interface or i should compulsory give a definition to all the methods inside a interface when that interface is implemented.

What I have tried:

public interface Interface2
{
void get2();
bool insert2(User U);
}


I have implemented this interface and only used one of the method. Does it suffice ?
Posted
Updated 18-Aug-16 14:23pm
v2

For as long you've implemented those methods in a concrete class then you should be fine. However, if you don't use the other method you defined in your interface somewhere in your app then you could just simply remove it.
 
Share this answer
 
You don't have to call all members of an interface, just call the ones you need. Reading between the lines, though, if your interfaces are effectively the same;

C#
public interface Interfaces
   {
       void get();
       bool insert(User U);
   }

   public interface Interface2
   {
       void get2();
       bool insert2(User U);
   }


then you don't need Interface2 at all, simply have the class that implements Interface2 implement Interfaces instead.
 
Share this answer
 
Any Class (or, in C#, Struct) that implements an Interface must implement all Properties and Methods (and Indexers, and Events) specified in the Interface (C# interfaces do not allow Fields).

Keep in mind that a C# Interface is not a Class, or, a value, or reference, Type. While you can call some methods of 'Object on an Interface, like 'ToString(), the "nature" of an Interface is rather unique in .NET: see these comments by Jon Skeet: [^].

An Interface can inherit from another Interface, and I suggest you look at this as being an example of "extension;" and, you can use generic Type declaration(s) with an Interface:
C#
public interface IController
{
    // property
    string Name { set; get; }
}

public interface IControllerItems<T> : IController
{
    // property
    List<T> Items { set; get; }
    
    // method
    int ModelNumber { set; get; }
    
    // method
    void AddItem(T item);
}
So, an Interface is a contract, a promise that you will implement, a demand that you must implement; I suggest you think of that as different from "inheritance" of Classes.

Once you have implemented an Interface in a Class, to what extent you make use of every Property and Method you've implemented is ... up to you.

Problem here. To be continued ...
 
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