I run a code review and Resharper keep prompting me that “Expression is always true”.

“loSqlCon” is a SQLConnection object and i initiate this in a Using statement. I was started my programming life with vb.net and i was trained to always dispose the object if the object is not null. But this can be done more easily using the Using statement in C#. The using statement simplifies the code that you have to write to create and then finally clean up the object.

So in my case within the Using block, the object will always true and no need to write extra code and check if the object is null and dispose it because in C# the Using statement will help you to handle all these! One thing you need to remember is that the objects you instantiate must implement the System.IDisposable interface.

In IL code, the Using statement actually compiled to try-finally block.   Worth to spend a minute or two to continue your reading in here, very interesting article !

Advertisements