स्थिर सूचकांक सी # में क्यों अस्वीकृत हैं? मुझे कोई कारण नहीं दिखता कि उन्हें अनुमति क्यों नहीं दी जानी चाहिए और इसके अलावा वे बहुत उपयोगी हो सकते हैं।स्टेटिक इंडेक्सर्स?
उदाहरण के लिए:
static class ConfigurationManager {
public object this[string name]{
get{
return 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) {
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];
}
}
ऊपर कोड बहुत एक स्थिर इंडेक्सर से लाभ होगा। हालांकि यह संकलित नहीं होगा क्योंकि स्थिर सूचकांक की अनुमति नहीं है। ऐसा क्यों है?
अगला मैं स्थैतिक वर्ग पर प्रत्यक्ष आईनेमेरेबल कार्यान्वयन चाहता हूं, इसलिए मैं 'foreach (enum में var enum) कर सकता हूं :) :) – nawfal