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

The Archive Base Latest Questions

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

Why does this happen? Please observe the following code: static class StringExtension { public

  • 0

Why does this happen? Please observe the following code:

static class StringExtension
{
    public static string Remove(this string s, char c)
    {
        return s.Replace(c.ToString(), "");
    }
    public static string Remove(this string s, char[] a)
    {
        foreach (char c in a)
        {
            s = s.Remove((char)c); // <---- ArgumentOutOfRange Exception here
        }
        return s;
    }
}
class Program
{

    static void Main(string[] args)
    {
        char[] a = new char[] { '.', ',' };

        string testString = "Clean.this,string.from,periods.and,commas.";

        Console.WriteLine(testString.Remove(a));

    }
}

When I run this code, I get an ArgumentOutOfRange exception at the indicate line. Turns out that even if I have a specific code for an extension Remove (this, char) and I explicitly (although, there should be no reason for this) specify the parameter’s type, it ignores my extension and tries to call the original Remove(int) method.

Am I doing something wrong or this is a bug in C#?

P.S. I use VS2010.

  • 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-29T15:13:21+00:00Added an answer on May 29, 2026 at 3:13 pm

    This line:

    s.Remove((char) c);
    

    is calling string.Remove(int) – the compiler will always use an applicable instance method instead of an extension method if it can. It’s applicable due to the implicit conversion from char to int. That’s the method that’s throwing the exception, because the argument you’re passing it is out of range. (In fact, you’re lucky – in a worse situation it would be in range, and returning entirely unexpected results.)

    In general I would strongly advise you not to create extension methods which have the same names as instance methods on the extended type, particularly if they’ve got the same number of parameters. Working out overloading is hard enough in general without adding extension methods to the mix. Don’t forget that whenever you can’t easily work out what your code is doing, someone reading the code in a year’s time is going to have a ten times harder job.

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

Sidebar

Related Questions

Look please at this code: public static void main(String[] args) { String[] array =
(int)(33.46639 * 1000000) returns 33466389 Why does this happen?
If I use mod_rewrite to control all my 301 redirects, does this happen before
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the
Does this code cause a memory leak: int main(){ int * a = new
Why does this javascript return 108 instead of 2008? it gets the day and
This does not always happen, but sometimes when I try to build a (large)
Does anyone have any idea of why this could happen? I have a C
Does anyone know? And a bigger question is what happens when you encounter this
Does this look like it should work? I'm wanting to generate directions from one

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.