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

  • Home
  • SEARCH
  • 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 184733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:20:40+00:00 2026-05-11T15:20:40+00:00

Reflector tells me that SortedList uses a ThrowHelper class to throw exceptions instead of

  • 0

Reflector tells me that SortedList uses a ThrowHelper class to throw exceptions instead of throwing them directly, for example:

public TValue this[TKey key] {     get     {         int index = this.IndexOfKey(key);         if (index >= 0)             return this.values[index];         ThrowHelper.ThrowKeyNotFoundException();         return default(TValue);     } 

where ThrowKeyNotFoundException does nothing more than just:

throw new KeyNotFoundException(); 

Note how this requires a duff statement ‘return default(TValue)’ which is unreachable. I must conclude that this is a pattern with benefits large enough to justify this.

What are these benefits?

  • 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. 2026-05-11T15:20:41+00:00Added an answer on May 11, 2026 at 3:20 pm

    According to ThrowHelper.cs source code the main purpose is to reduce the JITted code size. Below is a direct copy paste from the link:

    // This file defines an internal class used to throw exceptions in BCL code. // The main purpose is to reduce code size.  //  // The old way to throw an exception generates quite a lot IL code and assembly code. // Following is an example: //     C# source //          throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key")); //     IL code: //          IL_0003:  ldstr      "key" //          IL_0008:  ldstr      "ArgumentNull_Key" //          IL_000d:  call       string System.Environment::GetResourceString(string) //          IL_0012:  newobj     instance void System.ArgumentNullException::.ctor(string,string) //          IL_0017:  throw //    which is 21bytes in IL. //  // So we want to get rid of the ldstr and call to Environment.GetResource in IL. // In order to do that, I created two enums: ExceptionResource, ExceptionArgument to represent the // argument name and resource name in a small integer. The source code will be changed to  //    ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key); // // The IL code will be 7 bytes. //    IL_0008:  ldc.i4.4 //    IL_0009:  ldc.i4.4 //    IL_000a:  call       void System.ThrowHelper::ThrowArgumentNullException(valuetype System.ExceptionArgument) //    IL_000f:  ldarg.0 // // This will also reduce the Jitted code size a lot.  
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 220k
  • Answers 220k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This looks like the non-singleton version of the module pattern,… May 13, 2026 at 12:00 am
  • Editorial Team
    Editorial Team added an answer just make a php page like this on your server… May 12, 2026 at 11:59 pm
  • Editorial Team
    Editorial Team added an answer If you are using TCP: You are right, there is… May 12, 2026 at 11:59 pm

Related Questions

In building a content management system that incorporates jquery ajax into it's GUI I
I always assumed that if I was using Select(x=> ...) in the context of
Is there a good tool that will look at a .NET assembly and tell
In .Net, given a type name, is there a method that tells me in

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.