As long as your code looks as simple like this
void ExampleFunc()
{
ICustomer oCustomer = new Customer();
oCustomer.Method1OfICustomer();
oCustomer.Method2OfICustomer();
// ...
}
there is no semantic difference - you can exchange "ICustomer" by "Customer", and the behaviour will stay identical. In this example, however, it could fulfill already some documentary purposes. The first line may express the design decision of the programmer to avoid adding calls like
oCustomer.MethodNotPartOfICustomer();
in the future, but in fact, when this kind of call is needed later on, one could also change the "new" line, or add the missing method to the ICustomer
later.
In fact, the use of interfaces, as the term implies, will start to make more sense when there is some real "interfacing" involved - interfacing between functions, classes, components. For example, the function
void ExampleFunc(ICustomer oCustomer)
{
oCustomer.Method1OfICustomer();
oCustomer.Method2OfICustomer();
// ...
}
is probably more generic than
void ExampleFunc(Customer oCustomer)
{
oCustomer.Method1OfICustomer();
oCustomer.Method2OfICustomer();
// ...
}
since it will work with any class implementing the ICustomer
interface, not just Customer
. For example, for testing purposes one could think of passing a "CustomerMock" into the function, something you cannot do with the second variant.
oCustomer
is way, way out of fashion, a throwback to "Hungarian" notation (Microsoft, Charles Simonyi). The original purpose of Hungarian notation was to indicate the type of a variable, in the C language, since C has such a weakly enforced type system (virtually non-existent, really) and the tools were so much more primitive. The original C# language specification at Microsoft actually recommended explicitly against using Hungarian notation. I used to be a huge advocate, now it seems kind of obnoxious when I encounter it. Just an observation. ;-) – Craig Tullis Apr 02 '15 at 07:37Customer
is an object (show of hands?). None. There's my point. If the prefix actually told you something more useful, like the oldlpsz
prefix in C which told you that you were (ostensibly) dealing with a long pointer to a null terminated plain old C string (as opposed to something like a BSTR), then the prefix would serve a purpose. :-) – Craig Tullis Apr 02 '15 at 18:08var
instead of specific type names in variable declarations is an abomination. – Craig Tullis Apr 02 '15 at 18:09