skip to Main Content

is there a way to manipulate the properties of a Model into another property?

I need to generate the FullName based on the supplied LastName and FirstName properties of my Model.

public class User
{
    public string Username { get; set; } = null!;
    public string Role { get; set; } = null!;
    public int EmployeeID { get; set; }

    public string FullName { get; set; } = null!; //CONCAT FROM LastName & FirstName

    public string LastName { get; set; } = null!;
    public string FirstName { get; set; } = null!;
    public Division Division { get; set; } = null!;
}

3

Answers


  1. Simply use public string FullName => LastName +" "+ FirstName;
    instead of public string FullName {get;set;}.

    Also, there really is no need to specify = null as a default value for reference type properties – the default value of reference types is already null.

    Login or Signup to reply.
  2. Try:

      public string FullName
            {
                get { return string.Format("{0} {1}", FirstName, LastName); }
            }
    
    Login or Signup to reply.
  3. You can concatenate FirstName and LastName by making the FullName property getter-only:

    public string LastName { get; set; };
    public string FirstName { get; set; };   
    public string FullName 
    {
        get { return LastName + " " + FirstName; }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search