Click here to Skip to main content
15,879,348 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
What will be the access modifier of the constructor defined in the following c# class?

C#
Class SampClass
{
   int var1;
   int var2;
   
   SampClass(int var1,int var2)
   {
      this.var1=var1;
      this.var2=var2;
   }
}
Posted
Updated 28-Nov-13 0:14am
v2
Comments
CPallini 26-Sep-12 7:00am    
What do you meant with 'type of the constructor'?
Constructor have the implicit return type of the class itself.
Tinoy Jameson Malayil 26-Sep-12 7:10am    
I mean the access specifier.(Public,Private or protected).What will be the access specifier of the above constructor?

The access specifier of constructor is private(by default).

Note: if no constructor is explicitly defined, a public default constructor will be automatically defined.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 13:51pm    
This is correct, but does not answer the question (I did not vote).
OP's confusion is deeper.
Please see my answer.
—SA
If we don't specify access modifier for any method or constructor then it is Private by default.

and for any class if we don't specify access modifier then it is Internal by default.
 
Share this answer
 
 
Share this answer
 
Comments
S.P.Tiwari 1-Oct-12 5:50am    
i didn't get you. what you mean.? can you explain plz.
Sergey Alexandrovich Kryukov 18-Feb-13 13:52pm    
This might be useful, but does not directly answer the question (I voted 4).
OP's confusion is deeper.
Please see my answer.
—SA
The declaration of the empty constructor prevents the automatic generation of a default constructor. Note that if you don't use an access modifier with the constructor it will still be private by default. However, the private modifier is usually used explicitly to make it clear that the class cannot be instantiated.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 13:52pm    
This is correct, but does not answer the question (I did not vote).
OP's confusion is deeper.
Please see my answer.
—SA
see more about default constrctor here


http://www.dotnetperls.com/default-constructor[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 13:52pm    
This is might be useful, but does not answer the question (I voted 4).
OP's confusion is deeper.
Please see my answer.
—SA
The notion of "type" is not applicable to constructors. None of the constructors ever returns anything. Or, if you wish, consider a constructor as something returning the instance of the declaring type, always. As you cannot change this type, return type and return statement are not used. You modify the instance of the type, not return it.

—SA
 
Share this answer
 
Comments
Tinoy Jameson Malayil 9-Apr-13 6:59am    
Here type represents the access specifier not the return type..
Sergey Alexandrovich Kryukov 9-Apr-13 10:38am    
Not only this is not true, this is complete gibberish. Amazing.
—SA
hi dear,

It is void type of constructor if you have not defined it.

C#
SampClass()
  {    
  }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 13:47pm    
As if any constructors could return any type.
This is not a void type constructor. It's apparent that constructors never return anything at all. The notion of type is not applicable.
Is it clear now?
—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