Elegant line breaks

I am looking for a more convenient way to execute the following code block, which is part of the EmailAddress class / object. In my opinion, the array of parts is clumsy, and I wonder if there is a single ruler using tuples or Linq that cleaned it up a bit.

Also, what would be a better property name than the "Alias" for the first part of the email address?

public string Alias { get; private set; } public string Domain { get; private set; } private void Split(string emailAddress) { var parts = emailAddress.Split(new[] { '@' }); Alias = parts[0]; Domain = parts[1]; } 
+4
source share
2 answers

Well, this is less complicated if you use an array of parameters:

 var parts = emailAddress.Split('@'); 

Or (to avoid creating a new array every time), you can reuse an existing array:

 private static readonly char[] EmailSplitter = new char[] {'@'}; ... var parts = emailAddress.Split(EmailSplitter); 

Also, I donโ€™t see it being particularly awkward, although if thatโ€™s all that the EmailAddress class does, I would do it in the constructor (or static method) and make the object immutable.

You might want to use "localpart" instead of "alias" - this will support RFC-822.

+5
source

I would say that everything is in order.

Another way would be to use a regular expression, but you would not get anything other than the ability to check the format at the same time as separating it.

0
source

All Articles