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

C++ use if else

Please refrain from using namespace std; { int a,b,c,d; int w=0.0,x=0.0,y=0.0,z=0.0; std::cout<<“enter four numbers” << std::endl; std::cin>>a>>b>>c>>d; int max = a; if (b > a) max = b; if ( c > max) max = c; if (d > max) max = d; std::cout << max << std::endl; } I voluntarily kept trivial code that … Read more