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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:07:17+00:00 2026-06-11T11:07:17+00:00

If this question is too simple I apologize in advance. Why does the method

  • 0

If this question is too simple I apologize in advance.

Why does the method NthIndexOf found in the link below require a static class and also static class member?

C# – indexOf the nth occurrence of a string?

  • 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-11T11:07:19+00:00Added an answer on June 11, 2026 at 11:07 am

    Because it’s an EXTENSION METHOD (EM).

    Extension methods enable you to “add” methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type. For client code written in C# and Visual Basic, there is no apparent difference between calling an extension method and the methods that are actually defined in a type.

    For example:

    String is a .Net type. Prior to EM, without deriving, you cannot add your own method to string type. Even if you did so by deriving, that method would be counted as method of derived type and not of string type.

    But now with EM’s you can do so without deriving from it.

    Requirements of EM (in C#)

    1. They have to be public and static
    2. The class in which they are defined is also public and static.
    3. Most Important: First parameter should be that type for which you are creating your EM and should have this keyword in front of it. Without it, your method would not be an EM and would be a static method only.

    In your earlier scenaro your EM NthIndexOf is defined in static class StringExtender

    public static class StringExtender
    {
        public static int NthIndexOf(this string target, string value, int n)
        {
            ....
        }
    }
    

    Now since first parameter contains this in front of string so you can call it as

    int result = "My string".NthIndexOf("M", 0);
    

    If it was not EM and a plain static method like this

    public static class StringExtender
    {
        public static int NthIndexOf(string target, string value, int n)
        {
            ....
        }
    }
    

    then it had to be called like

    int result = NthIndexOf("My string", "M", 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to SSRS, so I apologize if this question is too simple: I
this is probably too simple of a question, but here I go. I have
may be too simple groovy question but....please help i have a list like this:
Sorry if this question is too simple or easy. I just started studying Delegates
This is a very simple, probably too simple question but I'm afraid my limited
I'm pretty new to WPF, so please understand if this question is too simple.
I hope this question isn't too simple, I tried to go through the postgresql
I am getting to know SSIS, I apologize if the question is too simple.
Sorry if this is too simple a question, but is there a sumif function
Maybe this is a too simple question but I'm kind of stuck here. I've

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.