C# IntelliBits: Three Tips To Improve Your C# Knowledge

Improve your C# know-how with these 3 tips.

Though C# was developed by Microsoft over a decade ago, it has proven to be one of the most productive and popular programming languages of all time. Whether you're new to C# or have been working on it for years, there’s always room for improvement.

We’ll start with advice for those new to C#. The following three tips will help novices increase their knowledge of C#, including best practice techniques and sample code:

C# IntelliBit #1: Environment.FailFast

When a program becomes unstable, it is sometimes wiser to exit the application rather than risk corruption. The FailFast method halts the program immediately, ensuring no further code can execute.

To demonstrate, create a new console application project and add the following code to the Main method. The code includes commands to output four messages to the console. The first is displayed before the call to Environment.FailFast. As FailFast exits the program straight away, the following messages within the try catch and finally blocks are not shown.

Environment.FailFast(“Exit, Exit Application”);
Console.WriteLine(“Catch Block”);
Console.WriteLine(“Finally Block”);

C# IntelliBit #2: Nullable Types

This is from Microsoft’s Developer Network: Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

For example, a Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable can be assigned the values true or false, or null.

C# IntelliBit #3: Looping Over String Arrays

The simplest and most efficient way of looping over string arrays is with the foreach: The foreach statement repeats a group of statements for each element in an array that implements the System.Collections.IEnumerable interface. The foreach statement is used to iterate through the collection to get the information that you want, and it cannot be used to add or remove items from the source array/collection. If items need to be removed from the array, you can use a for loop.

using System;
class Program
    static void Main()
string[] arrayNumbers = new string[4]; // Initialize
arrayNumbers[0] = "one";               // Element 1
arrayNumbers [1] = "two";               // Element 2
arrayNumbers [2] = "three";             // Element 3
arrayNumbers [3] = "four";              // Element 4

// Loop over strings
foreach (string s in arrayNumbers)

In this post, we’ve taken a look at some common techniques for exiting applications, nullable types and arrays. Check back soon for coverage of other C# topics, including data types, delegates, lambda expressions and error handling.

Tags: C#, Career RX