0

Where I work we develop in C# .NET and I was looking a code that they have made (my job is to optimize it) but I saw something in particular:

Assuming that InterfaceDHO is an interface and ClassDHO is a class that implements InterfaceDHO.

Code:

InterfaceDHO in1;
ClassDHO cl1 = new ClassDHO();

in1 = cl1;

in1.METHODHERE(ARG HERE, ARG2 HERE);

Why are they doing this? Why assign the ClassDHO instance to an interface?

  • 3
    How is ClassDHO implemented? That is are there interface methods there that are implemented as InterfaceDHO.<some method> (aka Explicit Interface Implementation)? – Oded Dec 11 '15 at 15:15
  • Are you sure you don't mean ClassDHO implements InterfaceDHO? – paparazzo Dec 11 '15 at 15:20
  • @Frisbee well it sounds it can be ambiguous what I wrote, I'll edit it in a second, but yes that's what I meant – Horacio Garza Dec 11 '15 at 15:26
  • 1
    is what you are showing us the exact code (except for changed names) or are these lines picked from different places in the code? (Do the blank lines signify discontinuities in the code listing?) – Mike Nakis Dec 11 '15 at 15:44
  • 2
    I'm not sure why anyone would answer why someone wrote code a particular way. You should ask whoever wrote it. Everyone else is guessing. – Andy Wiesendanger Dec 11 '15 at 16:45
8

If ClassDHO implements InterfaceDHO as follows:

class ClassDHO : InterfaceDHO
{
    void InterfaceDHO.METHODHERE(arg1, arg2) ...
}

then METHODHERE is what's termed an explicit interface implementation, and it can only be accessed via a variable of type InterfaceDHO. The code you show is a little long-winded, but handles that situation. It could be simplied to the following though:

InterfaceDHO in1 = new ClassDHO();

in1.METHODHERE(ARG HERE, ARG2 HERE);

However, if this is not the case, then the following code will, at least for the small snippet you provide, have the same affect:

var in1 = new ClassDHO();  // in1 will be of type ClassDHO

in1.METHODHERE(ARG HERE, ARG2 HERE);

In other words, the interface variable isn't required.

You provide very little code though and in1 may be used in other ways later in the code (or you may have missed bits out), so it's not possible to say for sure.

  • Hehe, this is probably what is happening. – Mike Nakis Dec 11 '15 at 16:00
  • 1
    What I do in these cases is in my ClassDHO I would have public InterfaceDHO interfaceDHO { get { return this; } } – Mike Nakis Dec 11 '15 at 16:14
  • There's no need i can spot for that property, Mike. If ClassDHO implements InterfaceDHO, then you can just pass an instance of ClassDHO into any methods that require an InterfaceDHO without the need of the prop in your comment above. – Graham Dec 11 '15 at 20:33
  • This answer needs to mention the term "explicit interface implementation" so those sufficiently curious can learn more. – OldFart Dec 11 '15 at 21:19
  • @OldFart, good point. I've updated the answer accordingly. – David Arno Dec 14 '15 at 8:42
6

There is no such thing as an instantiation of an interface. Interfaces are implemented by classes. Classes are instantiated. References are held, either to classes, or to interfaces.

What you are witnessing is (probably) a programmer trying to adhere to the (very good) principle called Program To The Interface, Not To An Implementation. See:

So, if the lines that you have shown are the actual code, then this programmer may be a bit confused. But if the lines that you are showing us have been picked up from different places in the code, (as the blank lines seem to suggest discontinuities in the code listing,) then what is happening is that the code that makes the method call does not want to know anything about the implementation (ClassDHO), it only wants to work with the interface (InterfaceDHO).

  • 1
    Ahhh, I was wondering why my up-vote did nothing – Alternatex Dec 11 '15 at 15:49
  • 1
    I did not vote you down. But I got one also. And the answer that states bad at programming has an up. – paparazzo Dec 11 '15 at 15:50
  • 1
    I downvoted you because I felt that your first two paragraphs (at the time, your entire answer) didn't answer the actual question at hand and instead focused on what are likely side effects of English not being the asker's primary language. – Telastyn Dec 11 '15 at 16:25
  • 1
    @Telastyn the first version of my answer already contained the mention of "Program to The Interface, Not To An Implementation", together with references. That was the point of my answer, and it still is. Also, English language problems manifest in grammar and in spelling, not in the use of wrong words for technical concepts. When the wrong word is being used for a technical concept, then what you are almost always looking at is confusion about the concepts. So, I think you were just trying overly hard to find a reason to down-vote. – Mike Nakis Dec 11 '15 at 16:32
  • 1
    @MikeNakis - Well, I disagree with the point of your answer for this particular question strongly enough to downvote it. I downvote almost nothing (seriously, take a look at my profile). I assure you, I am not looking for reasons to downvote - let alone overly hard. – Telastyn Dec 11 '15 at 16:34
3

It is possible that ClassDHO doesn't actually implement InterfaceDHO, and the code is doing an implicit conversion (though I don't think that extra step is necessary even then).

It is possible that there used to be different implementations of the interface in that code, so it made sense to split up the variables, but when the others were removed this code wasn't cleaned up.

More likely though, the code was simply written by someone who was bad at programming.

  • Yep that's what I thought XD the people here are quite bad in programming, not my supervisor but the code that is already made it has a lot of dead code lol – Horacio Garza Dec 11 '15 at 15:30
  • If the ClassDHO (or it`s ancestors) does not implement the interface would not the assignment fail altogether ? Can .NET really do implicit conversions like this ? – Newtopian Dec 11 '15 at 17:49
  • 1
    @Newtopian - msdn.microsoft.com/en-us/library/85w54y0a.aspx – Telastyn Dec 11 '15 at 18:20
  • ha, indeed, easier to abuse than than operator overloading could create code with hidden surprises. With great power comes great responsibilities as said a famous philosopher !. thanks a million for the enlightenment. Will definitively go in my C# toolbox albeit under the use sparingly drawer (for now). – Newtopian Dec 11 '15 at 18:51
  • C# has been cleverly designed so that the kind of implicit conversion you are describing is difficult or impossible. If an object is convertible implicitly to an interface, it implements that interface. The principle here is that reference conversions preserve referential identity. – Eric Lippert Dec 15 '15 at 15:55
3

You are assigning the interface not instantiating

There is nothing to optimize

You just have some line(s) of code that are dealing with the interface.
It is clear that section of code only cares about methods and properties of the interface.

You could have something like this (assuming ClassDHO2 implements InterfaceDHO)

InterfaceDHO in1;
if(x > y)
{
   ClassDHO  = new ClassDHO();
   in1 = cl1; 
}
else 
{
   ClassDHO2 cl2 = new ClassDHO2();
   in1 = cl2;
}

in1.METHODHERE(ARG HERE, ARG2 HERE);

Consider a method call.
You could pass it the a class that implements InterfaceDHO (cl1) or your variable in1.

public void(InterfaceDHO interfaceDHO) 
{
}
  • I will tell you what is most likely the problem. Read this: meta.stackexchange.com/questions/17204/… And then this: meta.stackexchange.com/questions/17538/… --I am not sure that this is the problem, but it may be the problem. – Mike Nakis Dec 11 '15 at 15:57
  • No idea why you were downvoted, but your code could be simplified to InterfaceDHO in1 = x > y : new ClassDHO() : new ClassDHO2();. Your current example has a lot of duplication. – David Arno Dec 11 '15 at 16:00
  • 1
    @DavidArno It is just mimicking the OPs code to keep things understandable. – paparazzo Dec 11 '15 at 16:04
  • 1
    @Frisbee, that is a fair point. :) – David Arno Dec 11 '15 at 16:05
  • I didn't downvote this one, and have 16 ever. – Telastyn Dec 11 '15 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.