Skip to main content

Posts

Showing posts from July 19, 2018

C# Try Catch & Finally Demystified

One can wonder what can be there to demystify the try catch and finally block, There are some tricky scenarios in this area Lets see what are all that: Ideally Try is used along with Catch, so if any exception arises then it will be get catch in the catch block and based on the  business's preference he / she may want to throw using throw keyword or write in log file or do any other action. The Try Catch Finally works in the following mannaer Step 1: Control Comes inside the try block. Step 2: If the code inside the try executes normal then the control goes to finally block. Step 3: If the code inside the try is throwing error then it will execute the contents of the catch and then go to finally block. Step 4: In finally block thw CLR will free up the resources used in that try block. Step 5: If the function returns the control inside the try block, then also the control will finally comes to finally. Step 6: You cannot return the control from

C#: How to call base class constructor in the derived class?

Suppose you have a class which is derived from a base class you want to do some action on when the base class object is initialized but you don’t want to alter the base class constructor or you don’t have the rights to modify the base class constructor then C# comes in handy to provide a mechanism to do the logic in your derived class itself. You need to use the base keyword to do this task,    class BaseClass1     {         public BaseClass1()         {             Console .WriteLine( "BaseClass1() Called! " );         }         public BaseClass1( int Count)         {             Console .WriteLine( "BaseClass1(int Count) Called! " );         }     }     class DerivedClass1 : BaseClass1     {         public DerivedClass1()             : base ()         {             Console .WriteLine( "DerivedClass1() Called! " );         }     }     class Program     {         static void Main( string