Sunday 3 May 2015

Implicit vs. Explicit Interface Implementation

One feature of C# that I find useful is the support of implicitly or explicitly implementing interfaces. For example, if we have a simple interface such as the following:
public interface IDoSomething
{
    void DoIt();
}
You are probably already aware that we can implicitly implement the interface with code like:
public class MyClass : IDoSomething
{
    public void DoIt()
    {
        throw new NotImplementedException();
    }
}
We can use the implementation like:
MyClass myClass = new MyClass();
myClass.DoIt();
If MyClass is to explicitly implement the interface, the MyClass implementation would look like:
public class MyClass : IDoSomething
{
    void IDoSomething.DoIt()
    {
        throw new NotImplementedException();
    }
}
Notice the two differences.
  1. The access modifier has been dropped from the DoIt method signature
  2. The DoIt method name is now qualified with the interface name
So how does this now affect how we use MyClass? Well, you can no longer call the DoIt method on a direct instance of MyClass, you are now forced to code to the interface to access the DoIt method. So our previous code which instantiates MyClass and calls DoIt will no longer compile. Instead you will need to "explicitly" work with an instance of the interface to access the DoIt method. One example would be by declaring the type of the object to be the interface, like the following:
IDoSomething myClass = new MyClass();
myClass.DoIt();
Or you could cast the MyClass instance back to its interface to call the DoIt method, as shown in the following snippet:
MyClass myClass = new MyClass();
((IDoSomething) myClass).DoIt();
So, what are the benefits of explicitly implementing an interface?
  • As the implementer of a class, you can try to force good practice on clients by making them code to an interface
  • As a consequence of the point above, you can reduce tight coupling in your code as it shouldn't be using concrete implementations directly
  • If you are implementing two interfaces which have the same method signatures, or a method signature in an interface clashes with one already visible in your class, you can separate the two methods

Saturday 2 May 2015

Microsoft Exam 70-483: Programming in C#

Earlier this week I took and passed the Microsoft 70-483 exam. In this post, I'll provide some insight into my preparation.

The 70-483 exam tests your knowledge on the C# programming language and the .NET framework. In this case, it was C# 5 and the .NET Framework version 4.5. By passing this exam, you earn the Microsoft Specialist certification.
 
The official Microsoft page for the exam can be viewed by clicking on this link. I took the exam in the UK. It should be noted that the 70-483 exam is also the first exam on the path for the MCSD Windows Store Apps certification, which is ultimately the certification that I'm aiming for. 
 
As described on the official 70-483 exam page, the exam is broken down into four main areas:
  • Managing program flow
  • Creating and using types
  • Debugging applications and implementing security
  • Implementing data access

Under each of these topics on the exam page, you get a detailed breakdown of what is covered. There is a plethora of resources on the web that you can use to prepare for this exam (blogs, MSDN, PluralSight etc.). I recommend watching the exam prep video on the exam page first - this sets the scene in more detail and gives you a better idea about the topics covered on the exam.
 
As the two guys mention on the Microsoft prep video, there is no substitute for actual practice. At the beginning of my preparation, I created a blank console application in Visual Studio and added to this as I revised various topics. I wrote a method for each particular language construct or .NET framework class that I wanted to further understand. In each method, I practiced using a particular construct/class by referring to the MSDN site for the topic. I found this approach useful in clarifying how something works - the code produced also served as a useful reminding aid when the exam date got closer (the brain recalled those "eureka" moments that I had when writing the code). LINQPad is also a useful tool for practicing.
 
I found the official Microsoft exam reference book by Wouter de Kort useful (Amazon link here). Although the book shouldn't be used as a definitive resource, it serves as a useful aid in introducing various topics which you then should build upon with your own further research. I particularly found the multithreading, cryptography and managing assemblies sections of the book a worthwhile read.
 
I also bought the practice tests from MeasureUp. The practice tests were useful to get a feel for question formats - which are a mixture of multiple choice (sometimes with more than one correct choice) and drag and drop type questions. You can also loosely simulate test conditions by running the MeasureUp tests in "certification mode" - meaning that you are timed and unable to see the answers as you progress in the test. Note that the actual exam format/time can differ compared to the MeasureUp practice certification mode tests.
 
Finally, ensure you read the website of the test centre. In the UK, Microsoft has appointed the PearsonVUE test centres. I found the videos on the PearsonVUE website useful in telling me what to expect on the day (security procedures, the software used for the exam itself and what I need to bring with me on the exam day).
 
If you're taking this exam, all the best and feel free to leave a comment about your experiences!