Have you ever tried using the "Using" statement with a method and not a class?
We all have done the following:
1: using(StreamReader s = new StreamReader(@"c:\Test.txt"))
2: {
3: // Do stuff
4: }
Whether you know it or not, the compiler translates that code to the following:
This one could be easily become one of the Job Interview Questions we publish here at Dev102.com, but I decided to write a “regular” post about this issue because it is an important concept and not a just a puzzle or a brain teaser. Take a look at the following code, can you tell what will the output be?
public class BaseType { public BaseType() { Console.WriteLine("Call base ctor."); DoSomething(); } public virtual void DoSomething() { Console.WriteLine("Base DoSomething"); } } public class DerivedType : BaseType { public DerivedType() { Console.WriteLine("Call derived ctor."); } public override void DoSomething() { Console.WriteLine("Derived DoSomething"); } } public class MainClass { public static void Main() { DerivedType derived = new DerivedType(); Console.ReadLine(); } }
The output of this program is: