Tag Archives: syntax

Cast arrays from one reference type to another in C#

Every C# programmer knows that you can use a cast operator to convert one data type into another. For example, if the Employee class inherits from the Person class, then the following code creates an Employee object and then makes … Continue reading

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

Validate optional parameters in C#

The example Use named and optional parameters in C# explains how to use named and optional parameters to let the calling code omit any combination of parameters. In many applications, you may not want to allow the user to omit … Continue reading

Posted in methods, syntax, variables | Tagged , , , , , , , , , , , , , | 1 Comment

Use named and optional parameters in C#

Optional parameters is one of the few additions in recent versions of Visual Studio that are really useful. I usually try to post examples in an older version of C# because it’s easier for someone to move an older example … Continue reading

Posted in methods, syntax, variables | Tagged , , , , , , , , , , , , , | 1 Comment

Iterate over items in an array with unknown dimensions in C#

This example shows how you can iterate over the items in an array that has an unknown number of dimensions. When it starts, the program executes the following code. private void Form1_Load(object sender, EventArgs e) { string[, ,] values = … Continue reading

Posted in algorithms, arrays, syntax | Tagged , , , , , , , , , , , , , | 2 Comments

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