Tag Archives: C#

Make It Easier for the DBA: Give SQL Connections the Application’s Name!

Imagine you’re a database administrator tracing query activity on a server. You’d want all the clues you can get to help you figure out what’s going on. One clue is the name of the client program associated with each database session. This name, passed from client to Microsoft SQL Server when a connection is established, can be used by the DBA to differentiate between multiple applications running on a single client and to focus on (or filter out) activity from the same application across multiple clients.

Does your application pass a meaningful program name to the database server? Continue reading

NetMsmqIntegrationBinding — No Data Being Passed

Recently, I encountered a strange problem with a simple WCF NetMsmqIntegrationBinding service. The data objects passed into my service contained no data! When a message was received, the service would create an instance of the appropriate DataContract class but then would not populate the just-constructed object with the data contained in the WCF message (i.e. the data object’s fields were all left at their default values—null, 0, empty string, etc.). Continue reading

Monitor in Three Flavors – C# Thread Synchronization

C#’s lock statement provides built-in language support for synchronizing multi-thread access to blocks of code. Under the hood, lock is syntactical sugar simplifying the use of .Net’s Monitor exclusive lock. Let’s examine three ways Monitor can be used: via lock, directly and via a method attribute. Continue reading

C# Boolean Expressions With and Without Short-Circuiting

For years I’ve used the && and || operators in C# Boolean expressions (e.g. if (age > 60 && gender == male) { /* do something */ }). Today, I was surprised to learn that & and | can also be used to compare bool values (this is in addition to their traditional use as bitwise AND and OR operators).

What’s the difference between the two pairs of operators? && and || use short-circuit evaluation while & and | do not. Continue reading

INotifyPropertyChanged – propertyName – Past, Present and Hopes for the Future

Raising an INotifyPropertyChanged event requires a string containing the affected property’s name. Developers want a simple, clean and performance-efficient way to populate this string. New versions of .Net have brought developers closer and closer to achieving this goal.

Let’s review how INotifyPropertyChanged implementation options have improved over the years. Then, let’s consider an idea on how these options might be made even better. Continue reading

async/await Doesn’t Make Synchronous Code Asynchronous

With the recent release of .Net 4.5/C# 5, I’ve spent time experimenting with the new async/await functionality. To my surprise, these new keywords didn’t have the effect I anticipated. Based on my skimming of pre-release information, I was under the impression that the appropriate use of these keywords would cause a normally-synchronous method to execute asynchronously.

Wrong! Continue reading

Master-Detail Forms & Object Scope (Context)

Imagine a master-detail form. The top portion of the window contains a grid listing people’s names. Below is a form where the currently-selected person’s details may be edited. Should data-binding be used to connect the edit form’s fields directly to the grid’s currently selected record (row)? To put it another way, should the grid’s selected item and the edit form both be referencing the same in-memory object instance? Continue reading

Emulating C/C++’s switch Fall Through with C#’s goto

Today, while doing some research in the MSDN Library, I came across an interesting use of C#’s goto statement. Instead of causing the application’s execution to jump to a named label—a practice frowned upon in the programming world—this example used goto to emulate C/C++’s switch statement’s fall through behavior. Continue reading