## #989 – Formatting Numbers as Hexadecimal

You can represent an integer as a series of hexadecimal characters by using the hexadecimal format specifier in a format string.

In the examples below, we represent several integers using their hexadecimal characters.  We use the X notation in the format string to indicate that the number should be displayed as hex.

```            Console.WriteLine("{0} = {0:X}", 10);    // A
Console.WriteLine("{0} = {0:X}", 157);   // 9D = (9 * 16) + 13
Console.WriteLine("{0} = {0:X}", 1024);  // 400 = (4 * 256)
Console.WriteLine("{0} = {0:X}", 6975);  // 1B3F = (1 * 4096) + (11 * 256) + (3 * 16) + 15
```

You can include a digit after the X to indicate the number of minimum digits to be displayed. The hex number will be padded with zeroes to reach the desired width.

```            Console.WriteLine("{0} = {0:X4}", 157);   // 009D
```

## #988 – Using global to Explicitly Refer to a Namespace

A fully qualified namespace is not always sufficient to refer to a type.  Below, we’ve (unwisely) declared two different classes named Utility–a subclass of Program in the ConsoleApplication1 namespace and a class in the Program namespace.

If we refer to Program.Utility from within the Main method, the subclass is used.  To use the other version of Utility, we need to use the global keyword to describe the exact path to the type.  global::Program refers to the top-level namespace Program.

```namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Class Program.Utility in namespace ConsoleApplication1
Program.Utility util = new Program.Utility();

// Class Utility in namespace Program
global::Program.Utility util2 = new global::Program.Utility();
}

class Utility
{
public Utility()
{
Trace.WriteLine("Class Program.Utility in namespace ConsoleApplication1");
}
}
}

}

namespace Program
{
public class Utility
{
public Utility()
{
Trace.WriteLine("Class Utility in namespace Program");
}
}
}
```

## #987 – The using Directive Can Create an Alias for a Type

You can use the using directive to create an alias for a namespace, which you can then use to access the types in that namespace.  For example:

```using U1 = DogLibrary.Utility.StandardLogging;
using U2 = DogLibrary.Utility.AlternateLogging;
```

We might do the above if we had an identically named type in each of the two namespaces so that we could then prefix the type with the appropriate namespace.

```U1.DogLogger log1 = new U1.DogLogger(@"C:\log1.txt");
U2.DogLogger log2 = new U2.DogLogger(@"C:\log2.txt");
```

We can also use the using directive to create an alias for a specific type.  For the example above, we could do the following:

```using Logger1 = DogLibrary.Utility.StandardLogging.DogLogger;
using Logger2 = DogLibrary.Utility.AlternateLogging.DogLogger;
```

We could then use the alias directly as a type name:

```            // Short for DogLibrary.Utility.StandardLogging.DogLogger
Logger1 log1 = new Logger1(@"C:\log1.txt");

// Short for DogLibrary.Utility.AlternateLogging.DogLogger
Logger2 log2 = new Logger2(@"C:\log2.txt");
```

You can use the goto statement within a block of code to explicitly jump to another location in the code using a label.

```            Dog d = new Dog("Bob", 5);

DoTraining:
// Train my dog
d.Train();
if (d.NumMinutesCanSit < 5)
goto DoTraining;

Console.WriteLine("My dog is trained!");
```

While you can use a goto statement to jump to a label, it’s almost always a bad idea to use goto in this way.  You can always use structured programming techniques, like the while statement, rather than goto.  Code containing goto statements is typically harder to understand than functionally equivalent code written using structured programming constructs.

Here’s the above block of code, re-written to use a while loop.

```            do
d.Train();
while (d.NumMinutesCanSit < 5);
```

## #985 – Why It’s Useful for Conditional Operators to Short-Circuit Evaluation

C# won’t necessarily evaluate every sub-expression in a larger expression.  If knowing the value of the first operand is sufficient for knowing the value of the entire expression, the larger expression is said to be short-circuited.

For example, when evaluating a logical AND (&&), the second operand will not be evaluated if the first operand is false.

Short-circuiting is useful in cases when evaluating later operands would throw an exception.

For example, in the code below, if the variable d is null, the first operand is false, which means that the entire expression is evaluated as false.  d.CanBark, which would throw an exception, is not evaluated.

```            // d is of type Dog
if ((d != null) && (d.CanBark))
d.Bark();
```

Without the ability to short-circuit the expression, we’d have to do the check in two different if statements:

```            if (d != null)
if (d.CanBark)
d.Bark();
```

## #984 – The Birthday Problem

To celebrate today’s Thanksgiving holiday in the United States, the post today is just a fun little code fragment for exploring the birthday problem.  The idea of the birthday problem is to look at the probability of having at least one shared birthday in a group of n people.

Instead of calculating the actual probability of a collision, the code below empirically creates a group of people, assigns them birthdays, and then looks for shared birthdays.  It does this 100,000 times and then reports what percentage of time there actually was a match.

```        static void Main(string[] args)
{
while (true)
{
Console.Write("Enter # people: ");

const double NumTrials = 100000.0;

int numBirthdayMatches = 0;
double numUniqueBirthdaySum = 0;

List<int> personBirthdays;
Random rnd = new Random();

for (int trialNum = 1; trialNum <= NumTrials; trialNum++)
{
// Generate birthdays
personBirthdays = new List<int>(numPeople);
for (int personNum = 1; personNum <= numPeople; personNum++)

if (personBirthdays.Count != personBirthdays.Distinct().Count())
numBirthdayMatches++;

numUniqueBirthdaySum += personBirthdays.Distinct().Count();
}

double percentMatched =
numBirthdayMatches / NumTrials * 100.0;
double numUniquePerTrial = numUniqueBirthdaySum / NumTrials;
Console.WriteLine(string.Format("For {0} people, there was at least one matching birthday {1}% of the time.  Avg # unique birthdays={2}",
numPeople,
percentMatched,
numUniquePerTrial));
}

}
```

## #983 – Using a BitArray to Store a Large Collection of Boolean Values

If you need to store a set of boolean values, you can store them in array of type bool.  Each element of the array would require 8 bits, despite storing a single boolean value.

You can more efficiently store a set of boolean values in an enum type, marking the type with the Flags attribute.  This allows storing a maximum of 32 distinct boolean values.

To store a larger collection of boolean values, you can use the BitArray type.  A BitArray allows you to index into an array of boolean values, but stores the values in a more compact form than would be used by a normal array.

Below, we use an enum to index into a 50-element BitArray.

```        public enum States
{
Alabama,
// ...
Wyoming
}

BitArray baStatesIveVisited = new BitArray(50);

baStatesIveVisited[(int)States.Minnesota] = true;
baStatesIveVisited[(int)States.Wisconsin] = true;
baStatesIveVisited[(int)States.California] = true;

States nextState = States.Alabama;
foreach (bool b in baStatesIveVisited)
Console.WriteLine("{0}:{1}", nextState++, b);
```