Constructor Chaining in C#
[AdSense-A]
C# Constructor chaining is something pretty simple to accomplish in most classes. It is pretty common to have multiple constructors, but chaining them makes it a little easier to manage.
Here is a simple way to accomplish constructor chaining:
1 2 3 4 5 6 7 8 9 10 11 | public ProcessService(string id):this(new Account(id)){ } public ProcessService(Account a){ account = a; //some other code that may need to happen here. } |
Now when you call the constructor with the string parameter, it will automatically call the base constructor passing in a new Account object that is created from the id.
This is pretty straight forward and easy to use. It makes your code easier to maintain as well.