Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic Indexers?
    text
    copied!<p>Why are static indexers disallowed in C#? I see no reason why they should not be allowed and furthermore they could be very useful.</p> <p>For example:</p> <pre><code>static class ConfigurationManager { public object this[string name]{ get{ return ConfigurationManager.getProperty(name); } set { ConfigurationManager.editProperty(name, value); } } /// &lt;summary&gt; /// This will write the value to the property. Will overwrite if the property is already there /// &lt;/summary&gt; /// &lt;param name="name"&gt;Name of the property&lt;/param&gt; /// &lt;param name="value"&gt;Value to be wrote (calls ToString)&lt;/param&gt; public static void editProperty(string name, object value) { DataSet ds = new DataSet(); FileStream configFile = new FileStream("./config.xml", FileMode.OpenOrCreate); ds.ReadXml(configFile); if (ds.Tables["config"] == null) { ds.Tables.Add("config"); } DataTable 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) { DataSet ds = new DataSet(); FileStream configFile = new FileStream("./config.xml", FileMode.OpenOrCreate); ds.ReadXml(configFile); configFile.Close(); if (ds.Tables["config"] == null) { return null; } DataTable config = ds.Tables["config"]; if (config.Rows[0] == null) { return null; } if (config.Columns[name] == null) { return null; } return config.Rows[0][name]; } } </code></pre> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload