C# Arrays

Single-Dimension Array

An array is a set of data items of the same type. Each data item can be accessed using a numerical index. You declare an array as follows:

// Create an array named aBunchOfInts containing 5 elements indexed 0,1,2,3,4
int[] aBunchOfInts = new int[5];

Once defined, you can fill it using the index. (always remember that arrays start with an index of 0).

aBunchOfInts[0] = 5;
aBunchOfInts[1] = 10;
aBunchOfInts[2] = 15;
aBunchOfInts[3] = 20;
aBunchOfInts[4] = 25;

Be aware that if you declare an array but don’t explicitly fill each index, each item will be set to the default value of the data type. (in this example, because its an array containing ints, each int in the array would be a default of 0).

An array can be initialized at the same time it’s being declared like so:

int[] aBunchOfInts = new int[] {5,10,15,20,25};

The var keyword can be used to define an implicitly typed array. this way you can allocate a new array variable without specifying the type contained within the array itself.

// a is an array of ints
var a = new[] {5, 10, 15, 20, 25};

// b is an array of doubles
var b = new[] {2.2, 3.3, 4.4, 5.5, 6.6};

// c is an array of strings
var c  new[] {"hi", "how", "are", "you?"};

Multidimensional Arrays

Rectangular Multidimensional Array

There are two types of multidimensional arrays. The first is a rectangular array, which is an array of multiple dimensions where each row is the same length. The following code initializes a rectangular multidimensional array with 3 rows and 4 columns and then uses a for loop and a nested for loop to fill it with values.

// a rectangular multidimensional array
int[,] myRectangularMDArray;
myRectangularMDArray = new iny[3,4];

// Fill the (3 by 4) array
for(int i = 0; i < 3; i++)
     for(int j = 0; j < 4; j++)
          myRectangularMDArray[i, j] = i * j;

// Print the (3 by 4) array
for(int i = 0; i < 3; i++)
     for(int j = 0; j < 4; j++)
          Console.Write(myRectangularMDArray[i, j] + "\t");

this example would print out the following to the console window:

0     0     0     0

0     1     2     3

0     2     4     6

Jagged Multidimensional Array

A jagged array contains some number of inner arrays, each of which may have a different upper index limit. The following code initializes a jagged multidimensional array and then fills it with varying length arrays.

// A jagged multidimensional array (i.e., an array of arrays).
// Here we have an array of 5 different arrays.
int[][] myJagArray = new int[5][];

// Create the jagged array.
for (int i = 0; i < myJagArray.Length; i++)
     myJagArray[i] = new int[i + 7];

// Print each row.
// Because we didn't fill each element with a specific int value, 
// each element will have its default int value which is zero(0)
for (int i = 0; i < 5; i++)
     for (int j = 0; j < myJagArray[i].Length; j++)
          Console.Write(myJagArray[i][j] + " ");

this example would print out the following to the console window:






Multidimensional arrays take a little getting used to, the nested for loops will take a bit of effort to visualize in your head but once you wrap your head around them they become pretty straight forward.

New in C# 7 – Digit Separators and Binary Literals

C# 7 has added digit separators which makes it easier for the eye to keep track of the really long numbers in code. The underscore ( _ ) can now be used as a separator for integers, long, float, double and decimal data types.

static void ShowDigitSeparators()
     Console.WriteLine("***** Digit Separators *****");

digit separator output

C# 7 has also added Binary Literals which makes it easier to see your bit masks, and guess what?… the new digit separator works with these also!

static void ShowBinaryLiterals()
     Console.WriteLine("***** Binary Literals With Digit Separators: *****");
     Console.WriteLine("Sixteen: {0}",0b0001_0000);
     Console.WriteLine("Thirty Two: {0}",0b0010_0000);
     Console.WriteLine("Sixty Four: {0}",0b0100_0000);

binary literals

These are two small features that make life as a C# programmer just that much easier.

Format Numerical Data

C# offers some handy numerical format characters.

  • C or c – used to format currency.
  • D or d – used to format decimal numbers. It can also specify the minimum number of digits used to pad the value.
  • E or e – used for exponential notation. if you use upper case the exponential constant will be uppercase, if you use lowercase it will be lowercase.
  • F or f – used for fixed point formatting. It can also specify the minimum number of digits used to pad the value.
  • G or g – stands for general. Can be used to format a number to fixed or exponential format.
  • N or n – used for basic formatting with commas.
  • X or x – used for hexadecimal formatting. If you use uppercase the hex values will be in uppercase and vice versa.

here are some examples.

class Program
    static void Main(string[] args)
        Console.WriteLine("***** Formatting Numerical Data *****");
        Console.WriteLine("The value 99999 in various formats:");
        Console.WriteLine("c format: {0:c}", 99999);
        Console.WriteLine("d9 format: {0:d9}", 99999);
        Console.WriteLine("f3 format: {0:f3}", 99999);
        Console.WriteLine("n format: {0:n}", 99999);
        Console.WriteLine("E format: {0:E}", 99999);
        Console.WriteLine("e format: {0:e}", 99999);
        Console.WriteLine("X format: {0:X}", 99999);
        Console.WriteLine("x format: {0:x}", 99999);


the previous code produces the following output.


Setting Up a Virtual Network Lab with VMware Workstation and pfSense – Part 5, DHCP server

Now that we have our server set up for DNS and Active Directory we need to add one final role. We need a DHCP server on our network to handle giving out IP addresses. Since we turned DHCP off on our pfSense router in part 1 of this tutorial we need to add it to the server. So here we go… On the Server Manager dashboard click add roles which will start the add roles and features wizard. on the before you begin screen click next.


For installation type choose role based and click next.


select your server and click next.


Select the DHCP Server role.


Setting Up a Virtual Network Lab with VMware Workstation and pfSense – Part 4 DNS and Active Directory

Now that we have a clean server install we can start adding roles for DNS and the Domain Controller. The first thing we must do is give the server a static IP address and a preferred DNS server. For the static IP I’ll use and I’ll use a local DNS address of since this server will act as the DNS server. Then reboot the server.


Once we have our IP set up we can start by adding the DNS role to the server. From the Server Manager dashboard choose “Add roles and Features” to start the Add Roles and Features Wizard.


Click next and under select installation type choose “Role based or feature based installation and click next.


in the Select destination server screen you should see the static IP that you set earlier. Highlight the server and click next.


Setting Up a Virtual Network Lab with VMware Workstation and pfSense – Part 3 the server

In part 3 of this tutorial I’ll set up a virtual Windows 2012 R2 server that will act as our DNS server, Domain Controller and a DHCP Server for our virtual private network. The server install is pretty straight forward and I won’t go into much detail. So here we go. First we’ll create another typical virtual machine.


Once again choose to install the operating system later.


Select Microsoft Windows version Windows Server 2012 for the operating system.


Name the virtual machine and choose a location for it.


Setting Up a Virtual Network Lab with VMware Workstation and pfSense – Part 2 the client

In part 2 of this tutorial we’ll add a Windows 7 client to our virtual network. The client will be added to the Active Directory once that is set up in a future part of this tutorial. So lets create another virtual machine. Here we can accept the default “Typical (recommended)” radio button.


We’ll install the operating system later.


Select Microsoft Windows for operating system and Windows 7 x64 for the version.


Name the client whatever you like, I named mine Windows 7 Client 01 and choose a location where you want to save this virtual machine.


Setting Up a Virtual Network Lab with VMware Workstation and pfSense – Part 1 the router

In this part I’ll create a new pfSense virtual machine that will act as our router. It will have two NIC’s, one for the WAN and the other for the LAN that our virtual network will use to keep itself on its own private network. The first thing we have to do is create a new custom virtual network in VMware Workstation by going to Edit > Virtual Network Editor… and then click on Add Network… select a network to add from the dropdown list (I selected VMnet11) and click OK, under VMnet Information check the Host-only radio button and make sure that everything else is unchecked. You can leave the Subnet IP and Subnet mask to whatever VMware has given you.

Virtual Network Editor

Now we can create the pfSense virtual machine. Click “Create a New Virtual Machine” on the VMware Workstation home screen.

VMware Workstation Home Screen

Select “Typical (recommended) from the Welcome to the New Virtual Machine Wizard screen.

Welcome to the New Virtual Machine Wizard

Select “I will install the operation system later.” radio button and choose Next.

Guest Operating System Installation