Category Archives: syntax

Compare performance looping over DateTime and integer variables in C#

The example Find Friday the Thirteenths in C# doesn’t really worry about performance. It uses integers to loop over the dates within a range. For every year between the start and end dates, the code considers the 13th of each … Continue reading

Posted in performance, syntax | Tagged , , , , , , , , , , , , , , | Leave a comment

Find Friday the Thirteenths in C#

The following code shows how this example lists the Friday the Thirteenths between selected start and end dates. // List Friday the 13ths between the start and end dates. private void btnGo_Click(object sender, EventArgs e) { lstResults.Items.Clear(); // Get the … Continue reading

Posted in performance, syntax | Tagged , , , , , , , , , , , | 1 Comment

Use == and != to compare structs in C#

This example shows how you can overload the == and != operators to make it easier to compare structs. By default, for reference types (classes), == returns true if two references refer to the same object (reference equality). It returns … Continue reading

Posted in operators, syntax | Tagged , , , , , , , , , , , , , , , | Leave a comment

Create a class outside of any namespace statement in C#

When you create a class in C#, Visual Studio automatically puts it inside a namespace statement. If your project’s name is howto_remove_namespace, then by default that namespace is howto_remove_namespace. But did you know that you can create classes outside of … Continue reading

Posted in classes, syntax | Tagged , , , , , , , | 2 Comments

Iterate over the items in arrays and lists in C#

You can use a foreach loop to iterate over the items in arrays, lists, collections, and other objects that have a GetEnumerator method. These loops are often more convenient than for or while loops because you don’t need to worry … Continue reading

Posted in arrays, lists, syntax | Tagged , , , , , , , , , , , , | Leave a comment

Understand the @ symbol in C#

The @ symbol can be pretty confusing for some C# developers. I’ve even known a few who have been programming for quite a while but who still have incorrect assumptions about what @ does. In your code (and only in … Continue reading

Posted in strings, syntax | Tagged , , , , , , , , , , | 1 Comment

Understand when a finally block is executed in C#

Many programmers don’t understand (and many have forgotten) how the try catch finally block works. The basic syntax is: try { // Statements that could cause an error… } catch (Exception ex) { // Statements to execute if there is … Continue reading

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

Compare the performance of incrementing statements in C#

This is another performance testing post. Assembly language usually has statements comparable to x++, x–, x +=, and other operators that add or subtract to a value and store the result in the same value, so it makes some sense … Continue reading

Posted in performance, syntax | Tagged , , , , , , , , , | Leave a comment

Give a class default indexer properties in C#

An indexer property is a property that other code can use inside square brackets to access some value provided by the class, much as you can use the index of an array. Unlike an array, a class’s indexer need not … Continue reading

Posted in arrays, classes, syntax | Tagged , , , , , , , , , | Leave a comment

Understand string equality testing in C#

To really understand this example, you need to know about string interning, equality testing, and operator overloading. Interning First, interning. To save space, .NET uses an intern pool to store literal strings at compile time. If the program contains a … Continue reading

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