title: Should explicit operators return null in c#?
body: <p>I'm writing some explicit operators to convert database model types to my domain model. Like so (simplified example):</p>
  
  <pre><code>public static explicit operator DomainModel.Role(Role roleEntity)
  {
     DomainModel.Role role = new DomainModel.Role
     {
        RoleId = roleEntity.RoleId,
        Name = roleEntity.Name
     };
  
     return role;
  }
  </code></pre>
  
  <p>However it's possible that the <code>roleEntity</code> parameter is null. Most of times in the .net framework an explicit cast of a null instance results in an exception. Like so:</p>
  
  <pre><code>user.Role = (DomainModel.Role)_userRepository.GetRole(user); // Would normally results in a NullReferenceException
  </code></pre>
  
  <p>But if the explicit operator would be adjusted, the above would function as expected:</p>
  
  <pre><code>public static explicit operator DomainModel.Role(Role roleEntity)
  {
     DomainModel.Role role = roleEntity == null ? null : new DomainModel.Role
     {
        RoleId = roleEntity.RoleId,
        Name = roleEntity.Name
     };
  
     return role;
  }
  </code></pre>
  
  <p>Question:</p>
  
  <ul>
  <li>Is it logical to create such explicit operators?</li>
  </ul>