User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are the uses of using?
In other word, if you know that the initialization of a variable in using may throw a particular exception, I wrap it with try-catch. Similarly, if within using body something may happen, which is not directly related to the variable in using, then I wrap it with another try for that particular exception. I rarely use Exception in my catch es.
182 In C++11, the using keyword when used for type alias is identical to typedef. 7.1.3.2 A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name.
Updating the using keyword was specifically for templates, and (as was pointed out in the accepted answer) when you are working with non-templates using and typedef are mechanically identical, so the choice is totally up to the programmer on the grounds of readability and communication of intent.
46 Justin Lessard's answer explains the difference between using and await using, so I'll focus on which one to use. There are two cases: either the two methods Dispose / DisposeAsync are complementary, or they are doing something different.
But if you put the using declaration inside a namespace it's limited to the scope of that namespace, so is generally OK (with the usual caveats on your particular needs and style).
The using statement is used to work with an object in C# that implements the IDisposable interface. The IDisposable interface has one public method called Dispose that is used to dispose of the object.
The problem with putting using namespace in the header files of your classes is that it forces anyone who wants to use your classes (by including your header files) to also be 'using' (i.e. seeing everything in) those other namespaces. However, you may feel free to put a using statement in your (private) *.cpp files.
Extremely good point. Of all the advantages using provides, it can't be combined with other predicates: select*from t join t2 using(i) and on 1 wouldnt work.
From MSDN, using Statement (C# Reference) The using statement ensures that Dispose is called even if an exception occurs while you are calling methods on the object. You can achieve the same result by putting the object inside a try block and then calling Dispose in a finally block; in fact, this is how the using statement is translated by the compiler. The code example earlier expands to the ...