Alan Alan - 1 month ago 5
C# Question

When I type objects is there any difference between me using an Interface or a class?

I created this interface:

public interface IPhrase
{
int CategoryId { get; set; }
string PhraseId { get; set; }
string English { get; set; }
string Romaji { get; set; }
string Kana { get; set; }
string Kanji { get; set; }
}


and this class:

public class Phrase : IPhrase
{
public Phrase()
{
}

public int CategoryId { get; set; }
public string PhraseId { get; set; }
public string English { get; set; }
public string Romaji { get; set; }
public string Kana { get; set; }
public string Kanji { get; set; }
}


Here this code returns data and typecasts it to Phrase:

var phrases = db2.Query<Phrase>("SELECT * FROM Phrase", ans);
var phrases = db2.Query<IPhrase>("SELECT * FROM Phrase", ans);


What I would like to know is if there is any difference / advantage in my using the IPhrase here or Phrase? Also what advantages are there (if any) in my creating a IPhrase interface in this example. Does that lead to more readable code?

Answer

With objects you can inherit from to things:

  1. Other Objects
  2. Interfaces

Object

If you have an Animal object, a Dog object can inherit from it as it is an animal.

With Object inheritance think of the phrase: I am a ...._yourObject_...

Interface

With an interface you can think of it as a describer for that object. Such as IWalkOnTwoLegs or ICanSwim.

So think of the phrase : I can do ...._yourInterface_..

Now to answer your question, would it make a difference if you use an interface or not?

Well, it wouldn't in this case but if you extend your phrases and create a Question object and a Statement object, for example, and they inherit from Phrase, you have a choice where you can return all phrases (questions and statements) or only phrases that are Questions or only Statements.

You can also apply an interface saying IAmInFrench and IAmInSpanish to your phrase so you can have extended SpanishPhrase and FrenchPhrase objects. Now you can return either all phrases whether they are questions, statements, in a different language, or you can be specific and return only french phases.

Where I find interfaces are most useful are for registration of different types in unity.

Where it will make a difference:

Where it will definitely make a difference is if there is a property on the object that isn't on the interface, then if you return the interface you will not be able to access that property very easily unless you type cast.

eg:

public class Phrase : IPhrase
{
    public Phrase()
    {
    }

    public int CategoryId { get; set; }
    public string PhraseId { get; set; }
    public string English { get; set; }
}

And interface

public interface IPhrase
    {
      int CategoryId { get; set; }
    }

You will not be able to access the property English if you return the interface:

var phrases = db2.Query<IPhrase>("SELECT * FROM Phrase", ans);
var eng = phrases[0].English; //**THIS WONT WORK**
Comments