Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8293709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:55:20+00:00 2026-06-08T13:55:20+00:00

I came across this behavior today while using the Substring method: static void Main(string[]

  • 0

I came across this behavior today while using the Substring method:

static void Main(string[] args) {
    string test = "123";
    for (int i = 0; true; i++) {
        try {
            Console.WriteLine("\"{0}\".Substring({1}) is \"{2}\"", test, i, test.Substring(i));
        } catch (ArgumentOutOfRangeException e) {
            Console.WriteLine("\"{0}\".Substring({1}) threw an exception.", test, i);
                break;
        }
    }
}

Output:

"123".Substring(0) is "123"
"123".Substring(1) is "23"
"123".Substring(2) is "3"
"123".Substring(3) is ""
"123".Substring(4) threw an exception.

“123”.Substring(3) returns an empty string and “123”.Substring(4) throws an exception. However, “123”[3] and “123”[4] are both out of bounds. This is documented on MSDN, but I’m having a hard time understanding why the Substring method is written this way. I’d expect any out-of-bounds index to either always result in an exception or always result in an empty string. Any insight?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:55:21+00:00Added an answer on June 8, 2026 at 1:55 pm

    The internal implementation of String.Substring(startindex) is like this

    public string Substring(int startIndex)
    {
        return this.Substring(startIndex, this.Length - startIndex);
    }
    

    So you are asking for a string of zero characters length. (A.K.A. String.Empty)
    I concur with you that this is not clear on MS part, but without a better explanation I think that is better to give this result than throwing an exception.

    Going deeper in the implementation of String.Substring(startIndex, length) we see this code

    if (length == 0)
    {
        return Empty;
    }
    

    So, because length=0 is a valid input in the second overload, we get that result also for the first one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just came across this weird behavior today: interface IFooBar { void Foo(); void
Today i was writing some heavy reflection-using code, and i came across this behavior
While debugging a program using I came across a pecular behavior. I am running
I came across this situation while migrating our DB from Foxpro to SQL. Below
I've been working on a project and came across some interesting behavior when using
I came across this interesting behavior. I see left-join is the way to go,
Recently I came across this Pete Blois's behavior named Explode. This is the link
I came across on a strange behavior of the following code, while playing around
I came across some weird behavior today around creating an object from the return
When writing some rspec today, I came across some unexpected behavior with comparing Date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.