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:

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.

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.

When you store a boolean value in a bool type, each boolean value uses 1 byte, or 8 bits–the size of a bool instance.

You can be more efficient in storing boolean values by using each bit within a chunk of memory to represent a single boolean value. You can do this quite easily by using an enum type to store a series of flags. In the example below, a single value of type Talents can represent unique values for up to 8 different boolean values.

Confidential data stored in an instance of the SecureString type is stored in memory on the unmanaged heap, in an encrypted form.

If you need to work with the data in an unencrypted form, you can read the data out of the SecureString into an unmanaged string (BSTR). Once you are finished working with the confidential string data, you should zero out the memory where it was stored.

Below is an example of using the Marshal.SecureStringToBSTR method to work with string data stored in a SecureString.

If you must work with confidential data in memory within your application, the proper procedure is to extract and decrypt the string data, but to store it in an unmanaged data structure (e.g. a BSTR). The data will be vulnerable while in memory within the unmanaged object, but you can then explicitly delete the data when done working with it, limiting the amount of time during which the data is vulnerable.