Timers Prior to Async/Await Pattern of C# 5.0

,
October 9, 2017 / No Comment

There are multiple ways to use Task.Delay() when a timer is required.
For scenarios prior to .NET 4.5, several timer classes are available, including System.Windows.Forms.Timer, System.Timers.Timer, and System.Threading.Timer.
The development team designed System.Windows.Forms.Timer specifically for use within a rich client user interface. Programmers can drag it onto a form as a nonvisual control and regulate the behavior from within the […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#, Toubleshooting

Generalized Async Return Types in C# 7.0

,
October 14, 2016 / No Comment

Ever since C# 5.0 when the async/await pattern was introduced, the only supported return types were Task<TResult>, Task, and void (the latter only in exceptional cases). The problem with this limitation is that on occasions when the result is known immediately, it is still necessary to proceed with the formality of instantiating a Task even though no task is actually required […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

Implementing an Implicit Conversion With C# 7.0 Deconstructors

,
October 14, 2016 / No Comment

While the deconstruct syntax is interesting for its convenience in assigning to a set of variables, what I believe has far broader implications is its potential to provide an “implicit conversion operator” and in a syntax that, quite frankly, is far easier to recall than the implicit cast operator.  For example, I can provide deconstructors that map to a string […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

Deterministic Finalization with IDisposable and the Using Statement

,
October 14, 2016 / No Comment

The problem with finalizers on their own is that they don’t support deterministic finalization (the ability to know when a finalizer will run). Rather, finalizers serve the important role of being a backup mechanism for cleaning up resources if a developer using a class neglects to call the requisite cleanup code explicitly.
For example, consider the TemporaryFileStream, which includes not […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

Guidelines for Exception Handling in C#

,
September 14, 2015 / 1 Comment

The following is a list of C# Exceptions Handling Guidelines taken from Essential C# by Mark Michaelis.
Exception handling provides much-needed structure to the error-handling mechanisms that preceded it. However, it can still lead to some unwieldy results if used haphazardly. The following guidelines offer some best practices for exception handling.

Catch only the exceptions that you can handle.
Read more

AUTHOR: CATEGORY: .Net, C#

The CIL of C# 6.0’s String Interpolation

,
January 2, 2015 / 2 Comments

One of the C# 6.0 features that will most simply and perhaps most predominantly affect the way you write C# code in the future is string interpolation.  Besides explaining composite string formatting (the old way – string.Format("{0} {1}", firstName, lastName) ) or obviously being relegated to a pre-C# 6.0 world, there is little reason to revert back from the string interpolation syntax.  Those of […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

MSCORELIB Classes that work with using static in C# 6.0

,
November 15, 2014 / No Comment

While writing another MSDN magazine article on C# 6.0 (the third because the language continues to improve) I was looking for a good example of when to use the new “using static” feature.  Towards this effort, I used PowerShell to crawl through all the MSCORELIB types and output the ones meeting the following criteria:

The type is public
The […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#, PowerShell

Exception Handling Improvements in C# 6.0

,
August 29, 2014 / No Comment

There are two new exception-handling features in C# 6.0.  The first is an improvement in the async and await syntax and the second is support for exception filtering.
When C# 5.0 introduced the async and await (contextual) keywords, developers gained a relatively easy way to code the Task-based Asynchronous Pattern (TAP) in which the compiler takes on the laborious and […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

Declaration Expressions in C# 6.0

,
August 29, 2014 / 3 Comments

It’s not uncommon that in the midst of writing a statement, you find you need to declare a variable specifically for that statement.  Consider two examples:

In the midst of coding an int.TryParse() statement, you realize you need to have a variable declared for the out argument into which the parse results will be stored.
While writing a for-statement, […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#

Static Using Statement in C# 6.0

,
August 29, 2014 / 5 Comments

Another C# 6.0 “syntactic sugar” feature is the introduction of using static.  With this feature, it’s possible to eliminate an explicit reference to the type when invoking a static method.  Furthermore, using static lets you introduce only the extension methods on a specific class, rather than all extension methods within a namespace.  The code of Listing 1 below provides a “Hello […]
Read more

AUTHOR: CATEGORY: .Net, Blog, C#