Abstract Classes vs Interfaces in C#

When should you use an abstract class, when should you use an interface?  Here’s the lowdown on some of the reasons to choose one over another.  First, take a look at a very simple example of an abstract class and two classes that inherit from it.

[snippet id=”8″]

Setting aside technical reasons, one of the strongest benefits to using an abstract class is the ability to provide an invariant implementation of some functionality across all sub classes, while leaving other implementations up to the subclass.  In our example above, all types of toast must first be toasted in a toaster.  Then, depending on the type of toast, the subclass can add whatever type of spread it wants.

On to the technical side.  An abstract class cannot be instantiated.  It is meant to be inherited.  If a method is not marked as abstract in an abstract class, the implementation belongs to the abstract class.  Abstract methods are implicitly virtual, and are meant to be overridden by the subclass.  They have no implementation in the base class, and therefore no method body, just a signature.  An class can inherit from multiple interfaces, but only one abstract class.  Here’s a more real world example.  Imagine a website back end where we want to collect and process credit cards for online purchases.  We might implement an abstract class such as the following:

[snippet id=”9″]

The abstract PaymentBase class dictates that no matter what way we collect card information in the front end website, we must always process the payment the same way.

Interfaces are like contracts that dictate all the functionality a class must have that implements it.  When you implement an interface, you must define a method body for all the method signatures in the definition.  Interfaces are great for dependency injection, which states that we should always program to an interface, not an implementation.  This is beyond the scope of this article, but warrants further reading if you’re not familiar with it.

Interfaces enable you do decorate additional classes of the same concept, adding functionality.  Consider the example below.  We have a Customer and a LoyalCustomer class.  They both implement the ICustomer interface.  While we can place an order for each type of customer using the same interface, we can apply a discount to a loyal customer.  Meanwhile, all throughout our application, we can use the ICustomer interface anywhere we refer to a customer, and pass either Customer or LoyalCustomer to the method.  This also allows us to extend Customer to additional custom types down the road.

[snippet id=”10″]

That’s all for now.  I hope you enjoyed this post!

 

 

 

 

 

 

 

 

Why is a manhole cover round?

If anyone ever asks you this in an interview, as I’ve had asked of me, they’re probably testing how you think.  I was a bit shocked at first, what does this have to do with code?  Much, as it turns out.  How do you answer this question?  Many of you know the answer, but if you were like me, sadly I did not.  I gave it my best guess.  Reason out loud.  That’s the most important strategy here.  Turns out manhole covers are round so they don’t fall in the hole, and possibly on someone working in the hole.

Edit in peek definition Window!

This is the coolest thing since Spreadable Bacon.  In Visual Studio 2017, you can view a method definition, and edit the code right in a pop up window with the Peek Definition command.  In the screenshot below, I’m actually editing the code right in the pop up window itself.  This will save you a bunch of time by not having to dig down into other folders where your classes or methods are defined, just to make a minor adjustment.

peekDefinition

 

 

 

Organize and visualize your using statements better in Visual Studio 2017

In Visual Studio 2017, you can clearly see what using statements you are not… using.    The IDE grays them out for you.  This is a nice touch, as it can get confusing as to what namespaces are actually in use.  Especially after you’ve added some code, refactored, taken some things out.  Those little orphaned using statements that are no longer needed become easy to see.

HighlightedUsings

Want to get rid of them?  No problem.  Just go to Edit > IntelliSense > Organize Usings.  From there you can choose to Remove Unnecessary Usings, Sort Usings, or Remove and Sort usings.

Here’s what it looks like after I’ve removed my unnecessary usings.

RemovedUsings

Pretty cool huh?  I thought so.  If you choose Sort Usings, it will sort them alphabetically for you.  Here’s the before and after of that

UsingBeforeOrganize

UsingAfterOrganize

Visual Studio Live Redmond 2015

Visual Studio Live  is a multi-city tech conference focused on primarily the Microsoft stack.  I had the privilege of  attending the conference in Redmond.  If you’re familiar with tech conferences, there can be great things and not so great things.   Fortunately, VS Live has been full of great things!

If like me, you’ve been to Microsoft Build and the now formerly Tech Ed, you’ll know some of the ups and downs that go with big conferences like these.  Here’s my take on how VS Live compares.

Location.  Location.  You Get the Idea  VS Live Redmond is at the Microsoft campus.  This is cool in and of itself.  Its the center of it all.  Its cool to be here and see where it all goes down.  They let us have lunch at the main Microsoft commissary they call the Mixer, using a pre-loaded credit card, and tour the Visitor Center.

Big Vs Small.  Build and the new Ignite are big.  There are benefits to this.  More swag, more networking opportunities.  Typically bigger recreational events to keep the geeks busy at night, and more session offerings.  More experts floating around to pick their brain. Comparatively, with a smaller conference like VS Live, you get more intimacy.  This means a lot of things, and most of them, in my opinion, are good.  Lets explore this.

Speaker Access.  Ever want to ask a speaker a question after a session?  At the bigger conferences its possible, but you’ll have to fight your way through a mosh pit of developers all clamoring around him or her immediately after they utter “so in conclusion”.  At VS Live, you simply walk up and ask them a question.  Sure there might be a few other developers asking questions, but you’re basically guaranteed to get some time.  At Build, the speakers hang out in the cordoned off speaker zone.  At VS Live, the speakers hang out among us mere mortals.  You can always see them walking around the hallways during meals or breaks, and they’re more than happy to answer your questions.  In fact, they actually seem to enjoy it!  There’s really no emphasizing this aspect enough.  Being able to talk to and hang out with experts that are approachable, not egocentric, and actually really cool and interesting people, makes all the difference.  I learned a lot just through side conversations.  They also provided round table sessions with folks from the Microsoft team and speakers.  Again, you get plenty of chances to talk to knowledgeable people and ask them questions.  Its expected and encouraged.  You’re not competing with a mob of people.

Bathrooms  If you’re female, this doesn’t mean anything to you and you can skip to the next section 🙂  There’s no better representation of the (unfortunate and unnecessary) gender imbalance in tech than the length of lines for the men’s vs women’s bathroom.  In smaller conferences like VS Live,  if you’re a guy, the lines aren’t 10 miles long.

Food  The food at VS Live is better than Build, hands down in my opinion.

Content The sessions at VS Live are relevant to what you can use TODAY.  The content is more rich, in my opinion.  Its a good mix of bleeding edge and practical.  In depth and informational.  The bigger conferences tend to be a bit infomercially.  That’s all well and good, they kind of have to be.  At VS Live, they seem to be striking a good balance between addressing the challenges we face in the trenches daily, as well as getting us up to speed with the things we need to keep current.

Fun Stuff to Do  VS live did a good job of providing fun stuff to do at night.

In short, I’d consider VS Live the next time you’re convincing your boss to pay for something and send you out of town.  I’ll post more on this topic later.  What are your thoughts?  Happy conferencing!