Tag Archives: syntax

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

Compare the performance of switch and if statements in C#

This example compares the performance of switch and if-else statements. Both do roughly the same thing: they check a series of conditions until they find one that is true and then skip the rest. This example compares the speeds of … Continue reading

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

Clone lists and arrays of objects in C#

This is the last post I’ll write about clones, at least for a while. Some collection classes can already make clones. For example, an array can clone itself, although the result is always a shallow clone. This example shows how … Continue reading

Posted in algorithms, OOP, syntax | Tagged , , , , , , , , , , , , , | 1 Comment

Clone serializable objects in C#

If a class is serializable, then you can create a deep clone of an object from that class by serializing it and the deserializing it. This example uses the following code to define a generic extension method that clones objects … Continue reading

Posted in algorithms, OOP, syntax | Tagged , , , , , , , , , , , , , | 5 Comments

Use MemberwiseClone the ICloneable interface in C#

The example Clone objects in C# explains how to give a class a Clone method that makes copies of its objects. This example makes two changes: it implements the ICloneable interface and it uses MemberwiseClone to simplify the cloning code. … Continue reading

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

Clone objects in C#

A clone of an object is a copy of that object. There are two kinds of clones: shallow and deep. A shallow clone shares all of the same values as the original object, even if some of those values are … Continue reading

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