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

Related Questions

Lutz Roeder's Reflector, that is. Its obfuscated. I still don't understand this. Can somebody
This is what reflector gives: public int Int1 { get; set; } public string
Haven't fired up reflector to look at the difference but would one expect to
I downloaded Reflector.Net and it's an .Exe outside VS. I remember (last year) when
I was using .Net Reflector on an Internal App to try and understand what
Visual Studio does it; Reflector does it; and now I want to as well
I'm sure many readers on SO have used Lutz Roeder 's .NET reflector to
I am trying to figure out Messagebox( ownerWindow, ... ). Using reflector I see
Using reflection, how can I get all types that implement an interface with C#
Using reflection, I need to investigate a user DLL and create an object of

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.