Category Archives: variables

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

Use statement lambdas in C#

The example Use lambda expressions in C# showed how to use lambda expressions to concisely create an anonymous method that takes parameters and returns a value. The following code shows one of the lambda expressions that example used. private void … Continue reading

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

Use lambda expressions in C#

Lambda expressions are basically just a shorthand notation for making an anonymous method. There are a couple of formats, the simplest of which has the following syntax. x => expression Here expression is an expression involving the variable x. For … Continue reading

Posted in algorithms, graphics, variables | Tagged , , , , , , , , , , , , , , | 1 Comment

Use anonymous methods in C#

My previous post Use the predefined Action, Func, and EventHandler delegate types in C# explains how to use the predefined generic Func, Action, and EventHandler delegate types to graph four functions F(x, y). It uses the following code to define … Continue reading

Posted in algorithms, graphics, variables | Tagged , , , , , , , , , , , , | 1 Comment

Use the predefined Action, Func, and EventHandler delegate types in C#

This and the next couple of posts discuss delegates and the somewhat related topics of anonymous methods and lambda expressions. Briefly, delegate types are programmer-defined data types that represent methods. For example, the following code declares a delegate type. // … Continue reading

Posted in algorithms, graphics, variables | Tagged , , , , , , , , , , , , | 1 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

Determine whether a year is a leap year in C#

Detecting leap years is basically trivial because the DateTime data type provides an IsLeapYear method to do just this. The example uses the following code to display the leap years between the two entered dates. // List leap years between … Continue reading

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

Compare the performance of the int, long, and byte data types in C#

The example Compare the performance of the float, double, and decimal data types in C# compares the performance of the float, double, and decimal data types. This example performs a similar test for the int, long, and byte data types. … Continue reading

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

Compare the performance of the float, double, and decimal data types in C#

A computer’s math coprocessor typically gives the best performance with a particular data type. To see which works best on my system and to learn how big the difference is, I wrote this program to compare the performance of calculations … Continue reading

Posted in performance, variables | Tagged , , , , , , , , , , | 6 Comments