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 3351318
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:53:39+00:00 2026-05-18T01:53:39+00:00

Say I have a method like this*: public T GetItem(int index) { if (index

  • 0

Say I have a method like this*:

public T GetItem(int index)
{
    if (index < 0 || index >= _privateList.Count)
    {
        throw new ArgumentOutOfRangeException("index");
    }

    return _privateList[index];
}

Would you include that throw or leave it out? On the one hand I feel like it’s good to handle invalid input right away, since this allows you to feel more confident with what you’re dealing with when writing the actual implementation code. But in this case, if the bounds check were omitted, the end result would be much the same (the list would throw the ArgumentOutOfRangeException instead of the enclosing type).

Furthermore, since the above code is checking for something that I, the developer, know will be checked for anyway (in the call to _privateList[index]), it seems I’m making the code do more work than it needs to above, essentially performing the exact same set of comparisons twice.

Any guidance on this subject would be appreciated.

*Specifically I’m talking about .NET (C#), but I would imagine the same or a similar question could be asked of many different languages/frameworks—hence the “language-agnostic” tag.

  • 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-05-18T01:53:40+00:00Added an answer on May 18, 2026 at 1:53 am

    This is the kind of code you’ll find back in the .NET framework. It is rather important there, source code is (was) not readily available. If the argument check were not included, you’d get an exception that is hard to diagnose. It bombs on invisible code, concluding that the argument was wrong is not exactly easy.

    This is less of a concern with code that you write. But not absent, it depends on who will be the end-user of the code. If that is you, and you maintain the code, then you’ll have little trouble diagnosing the cause. If it is somebody else then it becomes important that they have ready access to an accurate copy of the source code as well. If that is muddled at all then don’t hesitate to include the check.

    Another consideration is what happens when you deploy the Release build of your code. Without the throw statement, it is quite likely that this particular code will be inlined. In other words, you won’t see the GetItem() method on the top of the stack. This can make finding the real source of the exception more difficult, especially since release stack traces don’t have line numbers.

    Then the cost is relevant. It is quite low in this case, maybe a nanosecond or two if the Count property is cheap enough. But the actual work done by the method is very cheap as well. Your test makes it easily 20% slower. It is the kind of method that could very well live deeply nested on the critical path of the program. The more the method does, the less relevant the extra test becomes.

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

Sidebar

Related Questions

Let's say I have a situation like this: public String method(String s) { return
Say I have a method like this: public void SaveData() { try { foreach
Let's say that I have a method that looks like this: def raise_if_client_status_error(xml_resp) #
Let's say I have a method: bool myMethod(int a) { //return a bool }
Say I have this simple method: public IEnumerable<uint> GetNumbers() { uint n = 0;
I have a method in a controller say like this. In fact, I'm in
I have a method like this: public FbUser FindUserByGraphOrInsert(dynamic json, bool commit = false)
Lets say I have a class like this in Java: public class Function {
Say i have a object like this public class Student{ public IList<Coursework> Courseworks{get;set;} public
Let's say I have an object MyObject that looks like this: public class MyObject

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.