When there was no default parameter in the past, many constructor will be written to make it easier for others to call our code. For example, the following code is pasted on a query method:
Public DataTable Search (string search)
{
Return Search (string. Empty, search, null, null, string. Empty );
}
Public DataTable Search (string search, string [] roleIds)
{
Return Search (string. Empty, search, roleIds, null, string. Empty );
}
Public DataTable Search (string search, string [] roleIds, bool? Enabled)
{
Return Search (string. Empty, search, roleIds, null, string. Empty );
}
Public DataTable Search (string search, string [] roleIds, string auditStates)
{
Return Search (string. Empty, search, roleIds, null, auditStates );
}
Public DataTable Search (string search, string [] roleIds, bool? Enabled, string auditStates)
{
Return Search (string. Empty, search, roleIds, enabled, auditStates );
}
After using the default parameters, how can this part of code be simplified?
Public DataTable Search (string search = null, string [] roleIds = null, bool? Enabled = true, string auditStates = null)
{
Return Search (string. Empty, search, roleIds, enabled, auditStates );
}
Wow, a line of code is enough, so many constructors are not needed. The code is much more concise and refreshing.
New technologies often increase production efficiency.
With permission management and workflow management, one person can do only a few things well.
From: permission management system component source code, professional permission design [Business Edition]