Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,


I have a class A with 2 constructor
as
Default
C#
public A()
{}

and
C#
public A(int a,int b)
{}






I want to call first parametrized constructor every time i create an object then default.

How to make parametrized constructor as default.


Thanks
Posted
Updated 27-Oct-13 23:50pm
v2
Comments
BillWoodruff 28-Oct-13 5:34am    
If you are never going to use the Constructor that has no parameters, why define it ?
Thomas Daniels 28-Oct-13 7:01am    
There could be some reasons: when you want to create a Windows Forms/WPF control. When you add it in the designer or through XAML code, the compiler expects an empty constructor.
BillWoodruff 28-Oct-13 12:29pm    
Good point !

1 solution

Try this:
C#
public A()
    : this(0, 0)
{
}
public A(int a, int b)
{
    // your code here
}

Change 0 into the integers you need.
 
Share this answer
 
v2
Comments
BillWoodruff 28-Oct-13 5:35am    
That's a very interesting answer: upvoted. I didn't know you could actually force a Constructor without parameters to first call a Constructor with parameters before executing its "own" code !

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