New in C# 7.0: Part 4 – Expression-Bodied Members

This week, we’re discussing Expression-Bodied Members in C# 7.0. Now, this was an existing feature in C# 6.0, but it was limited to only methods, like so:

With the new additions in C# 7.0, we can now do Constructors, Deconstructors, and Properties. A Constructor definition looks like this:

Similarly, a Deconstructor looks like this:

Finally, although auto-properties are shorter, you can write properties with specific implementations with this new shorthand:

That’s it for this week. Next week, we’ll go over Pattern Matching.

New in C# 7.0: Part 3 – Ref Returns

Picking up where I left off last week, there’s another interesting new feature called “Ref Returns”. Similar to Ref Locals, this allows you to return a reference from a method, and then optionally use it as a Ref Local in the calling code.

Let’s look at the following method definition:

The method takes an integer, and returns an entry from an array with that index. The important differences from a normal method is the ref keyword in the method signature, and the ref keyword after the return keyword. This tells the compiler that you want the method to return a reference to the variable/object, instead of passing it back by value.

When calling the method, we do this, defining a Ref Local in the process:

Or, if we don’t want to use it as a reference, and just want to use the value, we can omit the ref keyword in both places.

That’s it for this week. Next week, we’ll be talking about Expression-Bodied Members.

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.

Alarm Updates

I was going to shut down the Alarm Update service today (after originally saying it would be taken offline back in April), and I realized that it hasn’t actually been working properly since I migrated my websites to Azure. So, I’ll give all the 3.5.18 and earlier users until August 18, 2016 to update, and then I’m definitely shutting it off. So, update to 3.5.19!

Alarm 3.5.19 Released

Today, I officially released Alarm 3.5.19. You can get it on the “Things I Work On” page, or just Check for Updates in the application. This is the first official release of Alarm since July 2011, so there is a huge amount of changes (even though it is a minor revision).

This version contains several performance enhancements and large rewrites of core areas of the application. It also updates the Update Checker to the new RWC API. It is targeted against .NET 3.5 and also removes support for Windows XP.

Please Note: All previous versions will no longer be supported as of April 10, 2016, so it’ll be a good idea for you to upgrade as soon as you can. After that date passes, the update check in the software will no longer function, and you will have to upgrade manually.

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

SimpleLookups 2.0 Beta 1

Today, I’m releasing the first beta of SimpleLookups v2.0. This version includes the following changes:

New Features:
– Lookup Caching
– Core improvements/optimizations.
– Support for .NET 4.5.1, 4.5.2, and 4.6.

Removed Features:
– The final version will not support .NET 2.0 and 3.0.

Caching is enabled by default. You can turn it off by adding the following attribute to the node in your web.config: enableCaching=”false”. Or, if you use the SimpleLookups.Initialize() method, there’s now a set of overloads that deal with caching.

Version 2.0b1 can be downloaded here.

If you run into any issues at all, please email youdidthatwrong@russellwritescode.com and I’ll take a look at the issue.

I have a few cleanup tasks to do over the next week or two, so expect the final version to be ready around August 16th. Source will also be released at that time.

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.