Assigning a value to a class variable

I have tried the following code:

SystemUser user = (SystemUser)Session["CurrentUser"];
Data.Configuration configuration = db.Configurations.Single(c => c.ID == user.ID);
{
}


Here I declare the from variable of the MailAddress class with a default value, and if configuration.EmailFromAddress exists, I am assigning that value to the from.

Is this following best practice?

Here, the MailAddress class is calling twice to assign the value. Is it good performance and memory wise? Another implementation is using a string variable:

SystemUser user = (SystemUser)Session["CurrentUser"];
Data.Configuration configuration = db.Configurations.Single(c => c.ID == user.ID);
{
}


Which one follows best practice?

• "Is it good performance and memory wise?" It's very likely the difference won't be significant. Don't micro-optimize prematurely. – svick Oct 10 '14 at 13:54

If you are sure that configuration.EmailFromAddress will always be valid or null, I'd suggest using the null-coalescing operator (??)

SystemUser user = (SystemUser)Session["CurrentUser"];
Data.Configuration configuration = db.Configurations.Single(c => c.ID == user.ID);

In the case that configuration.EmailFromAddress can also be empty, I'd use the ternary operator
SystemUser user = (SystemUser)Session["CurrentUser"];

Assuming that EmailFromAddress is just a plain field or property then the second version is better because you are not creating an unnecessary object. However when you are comparing strings it is often better to use string.IsNullOrEmpty() or string.IsNullOrWhitespace() (available since .NET 4.0) depending of what the possible values might be. Saul's answer already contains the code showing how to do this.