Does Dispose still get called when exception is thrown inside of a using statement?

Yes, using wraps your code in a try/finally block where the finally portion will call Dispose() if it exists. It won’t, however, call Close() directly as it only checks for the IDisposable interface being implemented and hence the Dispose() method. See also: Intercepting an exception inside IDisposable.Dispose What is the proper way to ensure a … Read more

What happens if i return before the end of using statement? Will the dispose be called?

Yes, Dispose will be called. It’s called as soon as the execution leaves the scope of the using block, regardless of what means it took to leave the block, be it the end of execution of the block, a return statement, or an exception. As @Noldorin correctly points out, using a using block in code … Read more

How to use an iterator?

That your code compiles at all is probably because you have a using namespace std somewhere. (Otherwise vector would have to be std::vector.) That’s something I would advise against and you have just provided a good case why: By accident, your call picks up std::distance(), which takes two iterators and calculates the distance between them. … Read more

The type or namespace name could not be found [duplicate]

See this question. Turns out this was a client profiling issue. PrjForm was set to “.Net Framework 4 Client Profile” I changed it to “.Net Framework 4”, and now I have a successful build. Thanks everyone! I guess it figures that after all that time spent searching online, I find the solution minutes after posting, … Read more

What is the C# Using block and why should I use it? [duplicate]

If the type implements IDisposable, it automatically disposes that type. Given: public class SomeDisposableType : IDisposable { …implmentation details… } These are equivalent: SomeDisposableType t = new SomeDisposableType(); try { OperateOnType(t); } finally { if (t != null) { ((IDisposable)t).Dispose(); } } using (SomeDisposableType u = new SomeDisposableType()) { OperateOnType(u); } The second is easier … Read more

What are the uses of “using” in C#?

The reason for the using statement is to ensure that the object is disposed as soon as it goes out of scope, and it doesn’t require explicit code to ensure that this happens. As in Understanding the ‘using’ statement in C# (codeproject) and Using objects that implement IDisposable (microsoft), the C# compiler converts using (MyResource … Read more