Skip to main content

Posts

Showing posts with the label C# Interview Questions

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