Splitting a string of random length I am currently using this code, but I am pretty sure there is a better way of doing it:

private string ReturnEmployeeName(string userName)
{
    try
    {
        // Username coming is     RandomDomain\RandomLengthUsername.whatever
        string[] sAMAccountName = userName.Split('\\');
        return sAMAccountName[1];


How can I make this faster?  I am not sure if my try block will catch any exceptions that may arise because of this line.
Personally, I think you would be better off moving this code into a separate class as it's probably logic you are going to want in multiple places in your application and is most likely violating SRP (Single Responsibility Principle) wherever it is at the moment.

If we create a username class, we can encapsulate the logic in there and then re-use it from wherever we want in the application:

public class UserName
{
    private UserName() {}
    public string AccountName { get; private set; }
    public string DomainName { get; private set; }

    public static UserName Parse(string userName)
    {
        if (userName == null)
        {
            throw new ArgumentNullException("userName");
        }

        if (userName.Contains(@"\"))
        {
            var parts = userName.Split('\\');

            return new UserName { DomainName = parts[0], AccountName = parts[1] };
        }

        if (userName.Contains("@"))
        {
            var parts = userName.Split('@');

            return new UserName { DomainName = parts[1], AccountName = parts[0] };
        }

        return new UserName { AccountName = userName };
    }
}


You can then test the logic for this class in isolation:

public class UserNameTests
{
    public class WhenCallingParseWithANullUserName
    {
        [Fact]
        public void AnArgumentNullExceptionShouldBeThrown()
        {
            Assert.Throws&lt;ArgumentNullException&gt;(() =&gt; UserName.Parse(null));
        }
    }

    public class WhenCallingParseWithAUserNameWithNoDomainName
    {
        private UserName userName;

        public WhenCallingParseWithAUserNameWithNoDomainName()
        {
            userName = UserName.Parse("JohnSmith");
        }

        [Fact]
        public void TheDomainNameShouldBeNull()
        {
            Assert.Null(userName.DomainName);
        }

        [Fact]
        public void TheUserNameShouldEqualTheWholeUserName()
        {
            Assert.Equal("JohnSmith", userName.AccountName);
        }
    }

    public class WhenCallingParseWithDomainSlashUserName
    {
        private UserName userName;

        public WhenCallingParseWithDomainSlashUserName()
        {
            userName = UserName.Parse(@"Domain\JohnSmith");
        }

        [Fact]
        public void TheDomainNameShouldBeSetToTheDomainName()
        {
            Assert.Equal("Domain", userName.DomainName);
        }

        [Fact]
        public void TheUserNameShouldBeSetToTheUserName()
        {
            Assert.Equal("JohnSmith", userName.AccountName);
        }
    }

    public class WhenCallingParseWithUserNameAtDomain
    {
        private UserName userName;

        public WhenCallingParseWithUserNameAtDomain()
        {
            userName = UserName.Parse(@"JohnSmith@Domain");
        }

        [Fact]
        public void TheDomainNameShouldBeSetToTheDomainName()
        {
            Assert.Equal("Domain", userName.DomainName);
        }

        [Fact]
        public void TheUserNameShouldBeSetToTheUserName()
        {
            Assert.Equal("JohnSmith", userName.AccountName);
        }
    }
}