3

I'm not sure if I'm overthinking this but in the past, I've done something like this when declaring a class:

IMyService myService = new MyService();

Jumping into myService will take you to the IMyService interface.

However, doing the following will (obviously) take you to MyService.

var myService = new MyService();

Which is considered the 'correct' usage, or is this another example of "What's your favourite ice cream flavor?"?

I've looked at the most relevant question but it doesn't really answer my scenario.

Community
  • 1
  • 1
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
  • Your question title is misleading. You ask whether to declare it as concrete type or an interface. Latter has its benefits, but only you know it. – nawfal Jul 15 '14 at 14:43
  • @nawfal If you feel it's misleading, you're welcome to change it to something that makes sense. However, it still makes sense to me three years on... :| – Dan Atkinson Jul 20 '14 at 17:11
  • I didn't edit it since I feared OP would find it too drastic. The question title asks something about `var`, but in essence the question (in the body) is more *concrete type vs interface type*. So I was confused whether you're looking for importance of `var` or you want to know importance of interfaces. If you clarify I will edit it. Not nitpicking, just trying to make SO a better place. – nawfal Jul 20 '14 at 17:39
  • @nawfal No, that's actually not what I was asking, and not at all what I was wanting to know. :) – Dan Atkinson Jul 20 '14 at 20:49
  • which is not? I asked about two cases. – nawfal Jul 20 '14 at 20:53

5 Answers5

2

There is also this option ...

var myService = new MyService() as IMyService;

This will make var myVar = IMyService type ... you can then in other code do something like ...

if(myVar is MyService)
{
    //instance specific stuff 
}
iDevForFun
  • 978
  • 6
  • 10
1

Well, it depends. Do all the public members of your MyService class come (exclusively) from the implementation of the IMyService interface? Or there are some extra public members (perhaps from the implementation of another interface)? If so, the second "flavor" will allow you seeing these extra members, while the first one will not.

On the other hand, if you are using interfaces, I would say that the "correct" usage is obtaining the type instances from a dependency injection engine or from some kind of factory class or method, but I guess that's outside the scope of this question.

Konamiman
  • 49,681
  • 17
  • 108
  • 138
  • You're correct with the obtaining of the type instance from IOC. This is one thing that I have a concern about in my application. – Dan Atkinson Sep 27 '11 at 08:44
1

What type do you want myService to be? Do you want it to be an IMyService reference or a MyService reference? Once you make that decision, the rest follows.

In other words, only you can answer the question.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
0

It depends on what you mean by correct usage. In the example with an interface you are creating an object of MyService class and storing it into the "pointer" of IMyService class. So your object ius actually an instance of MyService, but your myService var treats it as IMyService interface. So you will not be able to call any methods/access any properties of MyService that are not in IMyService.

With var sample, you are just telling the complier to compute the type of the myService variable from the right side of the declaration. So, in this case it's absolutely identical to MyService myService, that means that you can access all public methods and properties of MyService class via myService variable.

J0HN
  • 26,063
  • 5
  • 54
  • 85
-1

I suppose the real question is why you define myService as an interface. It doesn't have much use unless you want to be able to assign other IMyService's to it. In that case you have to define it as an interface.

hcb
  • 8,147
  • 1
  • 18
  • 17
  • The use of interfaces vary. For example IOC, and mocking since they're mentioned in some of the answers. Without the interface, doing these would be extremely tricky. – Dan Atkinson Sep 27 '11 at 08:55