Category Archives: Technical

New in C# 7.0: Part 2 – Ref Locals

After about a year, I’m finally trying to start writing some more blog posts, so I’ll pick up right where I left off. Today, I’m going to give a brief overview of Ref Locals.

Ref Locals allow you to create an alias for a variable so that you are not creating another copy of the data or reference (in the case of an object), in memory. In previous versions of C#, if you wanted to refer to a local variable with a different name, you’d have to do this:

This unfortunately creates another copy of the variable, so changes to the original are not reflected in the copy. With Ref Locals, you can ensure changes are reflected in both places:

This is particularly helpful when dealing with arrays of items.

The changes will be reflected in the array itself, instead of just in the copy version of the variable.

That’s it for today. Next week is Ref Returns.

New in C# 7.0: Part 1 – Out Variables

This is the first post in a series on the new features available in Visual Studio 2017 and C# 7.0. Today, I’m going to be talking about Out Variables.

One of the great new features is probably something you will use frequently, which is defining an out variable, and using that variable in the same statement. Currently, to use an out variable, one must do something like this:

With simple syntactic sugar in the new version, you can instead do this:

That’s pretty much it. This is definitely a small, but useful little addition to the C# language.

Next time, I’ll be going over Ref Returns and Ref Locals.

Hello, Ruby!

As part of my professional development plans for the year, I’m learning the Ruby language and the Ruby on Rails framework. Now, I’m just getting started so I’m a complete newbie, but here’s the first program that we all write when we’re learning a language: Hello, world!

I’m using the “irb” REPL command instead of actually saving source file(s) at this point. Here’s how I started:

Output: Hello, world!

Basic, right? Now, let’s take some user input, and say hello to an entered name.

Input: Russell
Output: Hello, Russell!

If you just use gets, you will also receive the newline character on the input. The chomp function removes this character.

That’s all for now. I’ll be creating more posts as I continue learning Ruby.

SimpleLookups 2.0 Released

Today, I released SimpleLookups v2.0. This version includes the following changes:

New Features:
– Lookup Caching (Enabled by Default)
– Core improvements/optimizations.
– Support for .NET 4.5.1, 4.5.2, and 4.6.

Removed Features:
– Support .NET 2.0 and 3.0.

Version 2.0 is now available on NuGet and on this site. Please install using Visual Studio, if possible. This way, your download counts!

A few links for the lazy:
Getting Started with SimpleLookups

Download SimpleLookups 2.0 | NuGet
View Complete SimpleLookups API Documentation

WCF: Logging All SOAP Messages

Do you want to know what is actually in that SOAP message that your Windows Communication Foundation service is sending? Well, look no further. It’s actually quite simple to output the XML that is being created.

First, there are a couple classes that need to be added to your service project (or a separate project). The first of these is perhaps the most important, which is the message inspector that is used to do the actual logging, etc.

Next, we’ll create a behavior that uses this inspector.

Now we’ll create a Behavior Extension that uses the behavior we created.

Finally, we have to set up the behavior extension in the web.config so that the service uses it.

1. Assuming that there is already an element in the <system.serviceModel><services>, change the appropriate element to have a behaviorConfiguration=”LoggingBehavior”.

2. Still under <system.serviceModel>, add an element under <behaviorExtensions> with the name “loggingBehavior” and the type pointing to the full namespace of our LoggingBehaviorExtensionElement.

3. Again, still under <system.serviceModel>, add a element under <behaviors>/<endpointBehaviors> with the name “LoggingBehavior” and an element within it called “loggingBehavior”.

Here’s the full XML that we’re adding to the web.config:

That’s all there is to it. You can add code to log using your favorite logger within the LoggingMessageInspector methods. Feel free to comment if you have any questions or improvements.

Supporting iPhone 6/6 Plus Native Resolutions: The Basics

Ever since I got the new iPhone 6, I’ve been silently cursing the applications that haven’t yet properly updated their apps for the new screen sizes. The zoomed iPhone 5s display looks really terrible, folks! So, I’m going to give you a five minute tutorial to update your applications. Assuming, of course, that you updated your app last year for iOS 7. I’m also assuming that you have a reasonable understanding of how to navigate Xcode, so this post is a bit light on screenshots.

Step 0: Upgrade to Xcode 6.
I’ll wait. This doesn’t count as part of the five minutes.

Step 1: Add a Launch Screen File in Project Settings

This one is actually pretty easy. Click on your project on the left of Xcode, and select the proper project under “TARGETS”. It’ll probably be auto-selected. Go to the General tab (again, probably already selected) and scroll down to the “App Icons and Launch Images” section. For the Launch Screen File, select your storyboard file. Easy.

Set up Launch Screen File.

Set up Launch Screen File.

Step 2: Update all of your constraints

Alright, so this will take a bit longer. Basically, add constraints to your layouts so that it can stretch and still keep your controls where you want them. Set up constraints for the items on the left based on the left layout bound, and do the same for the items on the right based on the right layout bound. Auto-layout is your friend here. Pay attention to any red or yellow warning indicators that appear in your Document Outline, and fix them/add other constraints where necessary. One of the things that you’ll notice here is that in Xcode 6, the default left/right bounds are to “Margin”. This is something that was introduced in iOS 8, so if you’re still supporting iOS 7, as you should be, this will mess up your layout on those devices. The easiest way to avoid using margins in your constraints is to press the Option key, which changes “Leading Edge to Container Margin” to “Leading Edge to Container”, which was the default behavior in previous versions.

Constraint setup with Container Margins.

Constraint setup with Container Margins.

Constraint setup after pressing Option key.

Constraints setup after pressing Option key.

Step 2.1: A “Gotcha” on Table Views

Sometimes, you just can’t get the table view to start at the top of it’s container. Assuming that you have created your layout correctly, and set the TableView’s top to the Top Layout Guide, it’s because there’s an “Inset” set up. Click on the View Controller in the Document Outline, and uncheck “Adjust Scroll View Insets”.

TableView insets.

TableView insets.

Step 3: Add some new image assets

The iPhone 6 Plus’s display requires @3x image assets. If you want to truly support that device, you need to add @3x versions of your images and icons. @2x images will still work, but they’re obviously scaled up to the @3x size, which can look bad depending on the image.

Application icons.

Application icons.

That’s all. You’re kind of ready for the new iPhones!

iOS 8: Notifications Changes

We’re updating our iOS application at work, and I came across some fun with iOS 8 and notifications. So, I thought I’d write a short post about it.

In iOS 7, we used the following method on the shared UIApplication instance to set up notifications:

In iOS 8, this doesn’t work. So now, it’s a multi-step process:

First, we get an instance of the shared UIApplication (this is obviously optional, but I think it makes the code easier to read). Then, we create a UIUserNotificationSettings instance that specifies which notifications we want. After that, we register for user notifications, and finally, register for remote notifications.

The new method, registerForRemoteNotifications:, sends the same messages as the registerForRemoteNotificationTypes: method. These are application:didRegisterForRemoteNotificationsWithDeviceToken: for a successful registration, and application:didFailToRegisterForRemoteNotificationsWithError: for an error.

Well, what if you want to support both iOS 7 and iOS 8? It’s simple. See if the UIApplication instance responds to the new selector. Here’s the complete code:

Let me know if you have any questions or if there’s a better way to do this in the comments.

EDIT: I originally messed up the if statement in the complete code above. I used registerUserNotificationSettings instead of registerUserNotificationSettings: which caused the statement to be false, even on iOS 8.

SimpleLookups 1.6 Released

Today, I released SimpleLookups v1.6.0. This version includes the following changes:

New Features:
– Specify column suffixes for Name/Description/Code/Id columns.
– Specify column name of Active column.
– Specify whether or not to prepend the table name to the column names.
– Core improvements/optimizations.

Version 1.6 is now available on NuGet and on this site. I recommend NuGet, but to each their own.

As typical, a few links for the lazy:
Getting Started with SimpleLookups

Download SimpleLookups 1.6 | NuGet
View Complete SimpleLookups API Documentation

GIT, Powershell, and Windows 8.1

This weekend I opted to upgrade(depending on your opinion) my laptop to Windows 8.1. Now that I have lost all credibility, I can move on to talk about my experience with using GIT on Windows 8.1, and the issue I had with my PowerShell profile script.

Like many users, I have a profile script set up in PowerShell so that when I open it, my main code directory is the working directory, and I also have a custom prompt set up to tell me the current folder’s GIT status. I wish I could give credit where it is due on this script, but unfortunately I don’t know who wrote the original, original version. It was passed to me by my coworkers and I’ve made some changes/fixes to it to fit my environment.


As you can see in the image on the left, my PowerShell prompt shows the current branch, master, the current directory, and the status of my local copy (c = created, u = updated, d = deleted). When I moved to Windows 8.1 (with PowerShell 4.0), the script naturally stopped working properly. So, as a help to anyone out there that also uses this type of script and is making the jump, here’s a working version for Windows 8.1, which also works on Windows 7 (PowerShell 2.0+). You have to place it in the following location and it should be named this way for it to work properly:


You can verify this location on your machine by typing “$profile” into PowerShell.

Here’s the script. Make sure you replace my working path with your own:

In order to run this, you must first run the following command in PowerShell as an administrator, which will allow you to run unsigned scripts:

There’s more information about signed/unsigned PowerShell scripts here.

Any suggestions for improvement? Hit me up in the comments.

Pattern of the Week: Builder

This post is part of a series of “Pattern of the Week” posts. I’m primarily sticking with the Gang of Four patterns, but I may branch off occasionally. Thanks for reading.

Type: Creational

This week’s pattern is called the Builder pattern. The builder pattern is a common way to create an object, using methods and properties to set up exactly the kind of object you want, then calling a Build() method to actually create the object. Let’s look at an interface for a builder object:

This interface has a couple properties, ShirtColor and ShirtSize, and a method called Build. Let’s look at ShirtBuilder, the concrete implementation of this interface:

This class creates a Shirt object, which implements the IShirt interface. This interface and class are incredibly simple, so we don’t need to go over them here. Please look at the source code if you’re curious.

Now that we have the builder ready to create shirts, what do we need to do now? Let’s use it! A builder is also really easy to use.

The shirt variable above now has a fully initialized Shirt object with all of the parameters that you wanted. It’s ready to use.

The question is: when should I use this? You should use this pattern when you need to specify a lot of details about the object you’re creating. If you just need an object, with maybe one or two properties that need initialized, you should consider using the Abstract Factory pattern or a simple copy constructor instead. If there are a ton of things that you need to set on the object, Builder is the way to go.

This is pretty much all there is to this pattern. If you need additional help, please take a look at the full source code below, or feel free to comment, and I’ll respond directly.

Visit next week for an explanation of another pattern.

View Sample Code