Is there any better way to do this with fewer lines of code? Sometimes FirstName
or SecondName
or ThirdName
values could be null
- this code works but I want to improve the quality.
if (FirstName != null)
{
txtEngageeName.Text = FirstName.ToString() ;
}
if (FirstName != null && SecondName != null)
{
txtEngageeName.Text = FirstName.ToString() + SecondName.ToString();
}
if (FirstName != null && SecondName != null && ThirdName != null)
{
txtEngageeName.Text = FirstName.ToString() + SecondName.ToString() + ThirdName.ToString();
}
You can do simple (if you really don't want spaces between entities):
txtEngageeName.Text = string.Format("{0}{1}{2}", FirstName, SecondName, ThirdName);
Nulls will be formatted as empty string.
If you need spaces.
txtEngageeName.Text = string.Join(" ", new[] { FirstName, SecondName, ThirdName}.Where(s => s != null));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With