# Tag Archives: example

## Use LINQ with an embedded lambda expression to find prime numbers in C# (Part 3 of 3)

The example Use more efficient LINQ to find prime numbers in C# (Part 2 of 3) uses a LINQ query to find prime numbers. It creates a delegate variable named IsOddPrime referring to a lambda expression and uses it to … Continue reading

Posted in algorithms, LINQ, mathematics | | 4 Comments

## Use more efficient LINQ to find prime numbers in C# (Part 2 of 3)

The previous post showed how you can use LINQ to find prime numbers. For every number between 1 and a maximum, the LINQ query called the IsPrime method to see if the number should be part of the selection. That … Continue reading

Posted in algorithms, LINQ, mathematics | | 1 Comment

## Use LINQ to find prime numbers in C# (Part 1 of 3)

This example uses LINQ to find prime numbers between 1 and a maximum value that you enter. It defines the following IsPrime method to determine whether a number is prime. // Return true if the number is prime. private bool … Continue reading

Posted in algorithms, LINQ, mathematics | | 1 Comment

## Draw a cursor and use it at run time in C#

It’s not too hard to draw a cursor and use it in a C# program. The basic approach is to create a Bitmap and then create a new Cursor object, passing its constructor the Bitmap object’s icon handle as given … Continue reading

## Let the user select the ScrollBar maximum in C#

You would think that allowing the user to select the ScrollBar maximum would be the default behavior. Unfortunately, that’s not the case. The behavior of the VScrollBar and HScrollBar controls is largely determined by their Value, Minimum, Maximum, SmallChange, and … Continue reading

## Fix rectangle SmoothingMode problems in C#

This example explains how to fix a rather obscure SmoothingMode problem. If you look closely at the PictureBox on the right in the picture, you’ll see that its top and leftmost pixels are not the same color as the rest … Continue reading

## Compress and decompress directories in C#

The ZipFile class provides remarkably simple methods to compress and decompress directories in the zip format. This class is defined in the .NET Framework version 4.5, so this example is in Visual Studio 2017 instead of an older version. To … Continue reading

## Understand region directives in C#

Region directives let you define sections of code that you can collapse and expand by clicking the – and + signs to the left in the code window. Each region directive should have a corresponding endregion directive. You can include … Continue reading

Posted in coding, syntax | | 3 Comments

## Disable Visual Studio warnings in C#

Visual Studio warnings let you know when your code contains something suspicious. For example, suppose you have XML documentation enabled. To enable XML documentation, open the Project menu and select Properties. On the Build tab, check the XML Documentation File … Continue reading

## Use XML literals in C#

Visual Basic lets you include XML literals directly in your code as in the following example. Dim employees As XElement = <employees> <employee firstname=”Terry” lastname=”Pratchett”/> <employee firstname=”Glen” lastname=”Cook”/> <employee firstname=”Tom” lastname=”Holt”/> <employee> <firstname>Rod</firstname> <lastname>Stephens</lastname> </employee> </employees> Unfortunately, C# doesn’t allow … Continue reading