Category Archives: coding

Define custom exception classes in C#

The example Throw a standard exception in C# explains how to throw exceptions to tell the program about unexpected errors. The example See a hierarchy of exception classes for use in C# lists some useful exception classes. But what if … Continue reading

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

See a hierarchy of exception classes for use in C#

The example Throw a standard exception in C# explains how to throw exceptions to indicate a problem in a program. Throwing an exception is easiest if you can find an existing exception class that correctly represents your situation. The following … Continue reading

Posted in coding | Tagged , , , , , , , , , , , | 2 Comments

Throw standard exceptions in C#

The example Use try catch blocks to protect against unexpected errors in C# explains how to catch an exception and briefly mentions that you can use the throw statement to throw your own exception. This example shows how to throw … Continue reading

Posted in coding | Tagged , , , , , , , , , , , | 2 Comments

Remove unnecessary using directives in C#

The top of a C# code file usually includes series of using directives to tell Visual Studio which namespaces are used by the code. When you first create a project, the code files include lots of using directives that might … Continue reading

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

Tip: Make parentheses matching more visible in C#

Recently someone asked me if there was a Visual Studio add-in that highlighted parentheses, brackets, and braces so it would be easier to tell which closing parenthesis matched which opening parenthesis. It turns out that Visual Studio already has a … Continue reading

Posted in coding, miscellany, tips | Tagged , , , , , , , , | 1 Comment

Use preprocessor directives in C#

Technically C# actually doesn’t have a true preprocessor, but it treats these statements as if they were preprocessor directives. These statements tell C# about how to process pieces of code. The directives are: #if Tests a Boolean value at compile … Continue reading

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

Understand the region and endregion directives in C#

The region and endregion directives define a section of code that you can collapse and expand by clicking the – and + signs to the left of the code window. You can include optional text at the end of the … Continue reading

Posted in coding, syntax | 3 Comments

Use the pragma directive to disable Visual Studio warnings in C#

Visual Studio provides warnings to let you know when your code contains something suspicious. For example, suppose you have XML documentation enabled. (See Use XML comments to provide IntelliSense support and documentation in C# for information about what XML documentation … Continue reading

Posted in coding, syntax | Leave a comment