Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
What will be the access modifier of the constructor defined in the following c# class?
 
Class SampClass
{
   int var1;
   int var2;
   
   SampClass(int var1,int var2)
   {
      this.var1=var1;
      this.var2=var2;
   }
}
Posted 26-Sep-12 0:53am
Edited 28-Nov-13 0:14am
v2
Comments
CPallini at 26-Sep-12 7:00am
   
What do you meant with 'type of the constructor'?
Constructor have the implicit return type of the class itself.
Jameson M Tinoy at 26-Sep-12 7:10am
   
I mean the access specifier.(Public,Private or protected).What will be the access specifier of the above constructor?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

The access specifier of constructor is private(by default).
 
Note: if no constructor is explicitly defined, a public default constructor will be automatically defined.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
S.P. Tiwari at 1-Oct-12 5:50am
   
i didn't get you. what you mean.? can you explain plz.
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

see more about default constrctor here
 

http://www.dotnetperls.com/default-constructor[^]
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

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
  Permalink  
Comments
Jameson M Tinoy at 9-Apr-13 6:59am
   
Here type represents the access specifier not the return type..
Sergey Alexandrovich Kryukov at 9-Apr-13 10:38am
   
Not only this is not true, this is complete gibberish. Amazing.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

hi dear,
 
It is void type of constructor if you have not defined it.
 
SampClass()
  {    
  }
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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)



Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 28 Nov 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100