Skip to content

TryParse for email addresses  #907

@Grauenwolf

Description

@Grauenwolf

Currently we have to write this code:

        public static bool IsEmailAddress(this string value)
        {
            try
            {
                new System.Net.Mail.MailAddress(value);
                return true;
            }
            catch ()
            {
                return false;
            }
        }

MailAddress should have a TryParse method like Int32.TryParse.

Rationale and Usage

Throwing and catching exceptions can have a significant impact on performance. So when possible there should be a non-throwing alternative for methods that commonly fail. An example would be Int32.Parse and Int32.TryParse.

There is also an impact on debugger. If you have the IDE set to "Break on All Exceptions" in order to see where an exception is being thrown, it will also catch this even though it is not interesting.

Proposed API

class MailAddress {
    public static MailAddress Parse(string address);
    public static MailAddress Parse(string address, string displayName);
    public static MailAddress Parse(string address, string displayName, Encoding displayNameEncoding);

    public static bool TryParse(string address, out MailAddress result);
    public static bool TryParse(string address, string displayName, out MailAddress result);
    public static bool TryParse(string address, string displayName, Encoding displayNameEncoding, out MailAddress result);
}

Details

Open Questions

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions