The Black Belt C# series aims to uncover powerful but lesser-known features of the C# language. Each article introduces a few of these features and shows you how to use them to take your programming to the next level.
The “??” Operator
This granule of syntactical sugar, known as the the “Null Coalescing Operator”, provides a quick way to check and react to null values. As an example, lets say we have a widget name which came from an external source. Programming defensively, it’s always good idea to check reference types for null before working with them. Here’s an example of the code that may be written without using the operator:
This, vastly superior version, produces identical output to the first. If WidgetName is not null, then it’s simply written to the output window. However, if WidgetName does happen to be null, “WidgetName” is null” is safely written instead. This is a great way to unobtrusively improve quality in your program without sacrificing readability.
Black Belt Tip: Lazy Instantiation Made Easy
Leverage the null coalescing operator to achieve lazy instantiation with manually implemented properties:
Auto-Implemented properties are a nice shorthand (C# 3+) for expressing a public property and a private (although inaccessible) field… without having to type all of that out. Have a look:
This is a perfectly valid property, backed by a private field that is created automatically but is inaccessible. Here is the disassembly:
You can designate a different scope for the getter and for the setter, without leaving automatic properties. For Example:
Taking advantage of generics, introduced in .NET 2.0, is a great way to improve your productivity while layering on type-safety and added performance benefits (up to 200% when working with value types such as integers). To review, here is a sample generic method and usage:
Sample Generic Method
The usage outputs something like “Read 7 books from John Locke”. The important thing to notice is the use of the same generic “SelectRandom” method for both integers and strings.
It’s possible for the c# compiler to infer the type to be used. For example, this is perfectly legal:
Where T : new()
This special constraint limits acceptable types to those which feature a public, parameterless constructor. This allows for some really special syntax inside a generic class. Have a look:
Using the “new()” constraint unlocks the ability to easily instantiate a new version of the specified class type. This is just the tip of the iceberg, for a complete list of constraints, visit:
Likely the most advanced and confusing topic surrounding generics in c#, currying is the process of distributing an argument list over several functions, instead of one. For example, instead of having one function that takes three arguments, you can have three functions taking one argument, each building on the next. Let me show you a few examples:
Example Without Currying
Example With Currying
If you haven’t yet delved into functional programming then this example may be confusing and the benefit may not be immediately obvious. What’s happening here is each function calls the next, while contributing its argument. This process continues until it gets to the last function (X + Y + Z), which in this case defines what should happen with the arguments that have been assembled.
There are a few interesting benefits to this massive overhead of complexity. First, it’s possible to slowly assemble the function in bits and pieces over many lines. Example:
Example of Calling One Argument at a Time
This may be useful for reducing the scope and span of variables that contribute to your curried function. More importantly, by encapsulating combinations of arguments inside a variable, it opens up new possibilities for reuse:
Example Reusing Curried Functions
References [ + ]
|1.||↑||int)(DateTime.Now.Ticks % 0x7FFFFFFF|