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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:32:34+00:00 2026-05-10T23:32:34+00:00

Why are static indexers disallowed in C#? I see no reason why they should

  • 0

Why are static indexers disallowed in C#? I see no reason why they should not be allowed and furthermore they could be very useful.

For example:

public static class ConfigurationManager  {         public object this[string name]         {             get => ConfigurationManager.getProperty(name);             set => ConfigurationManager.editProperty(name, value);         }          /// <summary>         /// This will write the value to the property. Will overwrite if the property is already there         /// </summary>         /// <param name='name'>Name of the property</param>         /// <param name='value'>Value to be wrote (calls ToString)</param>         public static void editProperty(string name, object value)          {             var ds = new DataSet();             var configFile = new FileStream('./config.xml', FileMode.OpenOrCreate);             ds.ReadXml(configFile);              if (ds.Tables['config'] == null)                 ds.Tables.Add('config');              var config = ds.Tables['config'];              if (config.Rows[0] == null)                  config.Rows.Add(config.NewRow());              if (config.Columns[name] == null)                  config.Columns.Add(name);              config.Rows[0][name] = value.ToString();              ds.WriteXml(configFile);             configFile.Close();         }          public static void addProperty(string name, object value) =>             ConfigurationManager.editProperty(name, value);          public static object getProperty(string name)          {             var ds = new DataSet();             var configFile = new FileStream('./config.xml', FileMode.OpenOrCreate);             ds.ReadXml(configFile);             configFile.Close();              if (ds.Tables['config'] == null) return null;              var config = ds.Tables['config'];              if (config.Rows[0] == null) return null;             if (config.Columns[name] == null) return null;              return config.Rows[0][name];         }     } 

The above code would benefit greatly from a static indexer. However it won’t compile because static indexers are not allowed. Why is this so?

  • 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-10T23:32:35+00:00Added an answer on May 10, 2026 at 11:32 pm

    Indexer notation requires a reference to this. Since static methods don’t have a reference to any particular instance of the class, you can’t use this with them, and consequently you can’t use indexer notation on static methods.

    The solution to your problem is using a singleton pattern as follows:

    public class Utilities {     private static ConfigurationManager _configurationManager = new ConfigurationManager();     public static ConfigurationManager ConfigurationManager => _configurationManager; }  public class ConfigurationManager {     public object this[string value]     {         get => new object();         set => // set something     } } 

    Now you can call Utilities.ConfigurationManager['someKey'] using indexer notation.

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

Sidebar

Related Questions

public static string RatingCalculator(int input) { if (input < 10) { return string.Empty; }
I've got the following global array... public static float camObjCoord[] = new float[8000]; I've
I've got a float array camObjCoord declared as.. public static float camObjCoord[] = new
I've got a float array camObjCoord declared as.. public static float camObjCoord[] = new
Ok so I am stuck once again here is what I have public static
I have an HtmlHelper like this: (simplified for clarity) public static MvcHtmlString MyHelper<TModel>(this HtmlHelper<TModel>
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I am really struggling with this question: import java.util.regex.*; class Regex2 { public static
Very simple question of where does this code work? static void *gostruct[] = {
I have a small C# library with a static class that holds a very

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.