Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
4.25/5 (4 votes)
See more:
Please check it...
what is the difference between the following codes...

1.

C#
class A
   {
       public A()
       {
           Console.WriteLine("inside the  constructor A");
       }
   }
 class B:A
    {
        public B()
        {
            Console.WriteLine("inside the  constructor B");
        }
    }

main()
{
B objb=new B();
}


2.
C#
class A
   {
       public A()
       {
           Console.WriteLine("inside the  constructor A");
       }
   }
 class B:A
    {
        public B():base()    //here is the change, it is calling the constructor !!
        {
            Console.WriteLine("inside the  constructor B");
        }
    }

main()
{
B objb=new B();
}


Both are calling the constructor.And the execution of constructor also same like first A() and then B() ..
then what is the extra functionality of B():base() ??
Posted
Updated 19-Apr-12 4:17am
v3

In your example, there is no difference.
However the base keyword can be used to decide which constructor to call.
class A
   {
       public A()
       {
           Console.WriteLine("inside the  constructor A");
       }

       public A(int i)
       {
           Console.WriteLine("inside constructor(2) A");
       }

   }
 class B:A
    {
        public B():base(1)
        {
            Console.WriteLine("inside the  constructor B");
        }
    }


Here you are specifying that the second constructor of class A (the one with the parameter) be called.

You can read and find more examples about this keyword here - http://msdn.microsoft.com/en-us/library/hfw7t1ce(v=vs.100).aspx[^].
 
Share this answer
 
Comments
VJ Reddy 19-Apr-12 13:21pm    
Good example. 5!
Abhinav S 19-Apr-12 21:56pm    
Thank you VJ.
Uday P.Singh 19-Apr-12 14:00pm    
5!
Abhinav S 19-Apr-12 21:56pm    
Thanks Uday,
Espen Harlinn 19-Apr-12 16:06pm    
Right :-D
The idea of using A():base() is to give some parameter to the base class to be initialized by the constructor of the base class. In this example there is no need of calling the base class constructor cuz you don't have any parameters:)
For example you can
C#
public B(int a,int b):base(b)
{
 //some initialization here
}

and to call
C#
B obj=new B(c,d);
//where c and d are int

here your parameter d will be initialized by the constructor of the base class(A)
And btw be careful how you inherit classes . In the common case you inherit like this
C#
class B:public A

It can be private and protected too and it will change the access levels for member variables from the base class
 
Share this answer
 
Comments
VJ Reddy 19-Apr-12 13:23pm    
Good explanation. 5!
Uday P.Singh 19-Apr-12 14:00pm    
5!
Espen Harlinn 19-Apr-12 16:07pm    
Right :-D
Solution 1, 2 and 3 are really very good and explain the case very well.
I want to add that base is indispensable, when there is no parameterless constructor in the base class.
Case 1:
C#
void Main()
{
    B objb=new B(1);
}
class A
{
   public A(int id)
   {
        //Do something with id
       Console.WriteLine("inside the  constructor A");
   }
}
class B:A
{
    public B(int id)
    {
        Console.WriteLine("inside the  constructor B");
    }
}

This will not compile. And throws error that Class A does not contain a constructor that takes 0 arguments.

Case 2:
C#
void Main()
{
    B objb=new B(1);
}
class A
{
   public A(int id)
   {
        //Do something with id
       Console.WriteLine("inside the  constructor A");
   }
}
class B:A
{
    public B(int id):base(id)
    {
        Console.WriteLine("inside the  constructor B");
    }
}

This compiles
 
Share this answer
 
Comments
Espen Harlinn 19-Apr-12 16:08pm    
Right :-D
VJ Reddy 19-Apr-12 19:34pm    
Thank you, Espen :)
Abhinav S 19-Apr-12 21:57pm    
Good addition. 5.
VJ Reddy 20-Apr-12 6:43am    
Thank you, Abhinav.
Sergey Alexandrovich Kryukov 22-Apr-12 20:56pm    
I agree; and the indispensable role of "base" is a good point -- my 5.
--SA
The notation:
C#
Constructor(...)
    : base:Constructor(...)

is used to pass arguments to base class constructor. It does not make much sense to call base class constructor from within default constructor as you described in your question because it is already done by the compiler. It does not hurt either but it is redundant.
 
Share this answer
 
v2
Comments
VJ Reddy 19-Apr-12 13:29pm    
Good answer. 5!
Espen Harlinn 19-Apr-12 16:07pm    
Right :-D
Sergey Alexandrovich Kryukov 22-Apr-12 20:56pm    
Correct, a 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900