Category Archives: variables

Loop over an array of unknown dimension in C#

The previous post Loop over entries in an array of unknown length in C# explains how to loop over the entries in a one- or two-dimensional array, but what do you do if you don’t know how many dimensions an … Continue reading

Posted in algorithms, arrays, recursion, variables | Tagged , , , , , , , , , , , , , , , , | 1 Comment

Loop over entries in an array of unknown length in C#

One way to loop through the items in an array is to hard code its length into the program. Alternatively you can use the array’s Length property to determine how many items it contains at runtime. Then if you later … Continue reading

Posted in arrays, variables | Tagged , , , , , , , , , , , , | Leave a comment

Take actions depending on the day of the week in C#

When the program starts, it uses the following code to display a different message in two different ways for each day of the week. // Display a message that depends on the day of the week. private void Form1_Load(object sender, … Continue reading

Posted in variables | Tagged , , , , , , , | Leave a comment

Find the Friday following a given date in C#

This example shows how you can find the Friday following a particular date. You can use a similar method to find other days of the week following a specific date. (Note that I have updated this example from its original … Continue reading

Posted in variables | Tagged , , , , , , , , , , , | 2 Comments

Define and use bit masks in C#

The example Understand bit masks in C# explains how to use bit masks. To define a bit mask, simply create an enum and give it the Flags attribute as in the following code. [Flags] private enum BitmaskEnum { Value1 = … Continue reading

Posted in algorithms, calculations, mathematics, variables | Tagged , , , , , , , , , , , , , | Leave a comment

Understand bit masks in C#

Some values, including some properties defined by the .NET Framework, are bit masks. That means each bit in a value means something. For example, the AnchorStyles enumeration that determines how controls are anchored in their parents defines four values: Top, … Continue reading

Posted in algorithms, calculations, mathematics, variables | Tagged , , , , , , , , , , , | Leave a comment

Use bit operations in C#

C# defines several operators that perform bit operations. As you may be able to guess from the names, these operators manipulate the bits in an integer value. They operate on the value’s bits separately so they are sometimes called “bitwise … Continue reading

Posted in calculations, mathematics, variables | Tagged , , , , , , , , , , , , , | Leave a comment

Get variable values by name in C#

The process for displaying variable values is somewhat convoluted, but it’s not too long. The example starts by using the following code to define some private and public fields. // Some form-level values. private string private_value1 = “This is private … Continue reading

Posted in arrays, reflection, variables | Tagged , , , , , , , , , , , , , , , | 1 Comment

Check text data type in C#

This example shows how to use the TryParse method to check text data type. Each data type (int, float, bool, etc.) has a TryParse method attempts to parse a string and returns true if it is successful. You can use … Continue reading

Posted in strings, variables | Tagged , , , , , , , , , , , , , , , , , , , , | Leave a comment

Initialize arrays with ranges or repeated values in C#

This example uses the Enumerable class’s shared methods to initialize arrays to ranges or repeated values. The methods return IEnumerable results. You can then use the result’s ToArray and ToList methods to convert the IEnumerable into an array or list. … Continue reading

Posted in arrays, variables | Leave a comment