using System;
using System.Collections;namespace BioCareControls
{
/// <summary>
/// A wrapper around a Hashtable for configuration items. Configuration items are intended to be used by the property Grid
/// </summary>
public class Settings
{
private Hashtable settings;
public Settings()
{
settings = new Hashtable();
} /// <summary>
/// Get the key collection for this Settings object. Every key is a string
/// </summary>
public ICollection Keys
{
get{return settings.Keys;}
} /// <summary>
/// Get/Set the Setting object tied to the input string
/// </summary>
public SettingParameter this[string key]
{
get
            {
                return (SettingParameter)settings[key];
            }
set
            {
                settings[key]=value;
                value.Name=key;
            }
}
/// <summary>
/// Gets the Setting object tied to the string. If there is no Setting object, one will be created with the defaultValue
/// </summary>
/// <param name="key">The name of the setting object</param>
/// <param name="defaultvalue">if there is no Setting object tied to the string, a Setting will be created with this as its Value</param>
/// <returns>The Setting object tied to the string</returns>
        public SettingParameter GetSetting(string key, object defaultvalue)
{
if(settings[key]==null)
{
                settings[key] = new SettingParameter(defaultvalue, null, null);
                ((SettingParameter)settings[key]).Name = key;
}
            return (SettingParameter)settings[key];
}
}
}请问一下,红色字体标注的方法是什么写法?this[string key] 代表什么? 其中SettingParameter是一个类,定义了显示在CustomPropertyGrid上面的信息。

解决方案 »

  1.   

    this是针对对象的,当前对象的指针这是给它写了个象索引访问的方法。
      

  2.   

    是C#里面一个属性的声明方法,get和set是用来限定这个属性是只读还是读写之类的,你在msdn上搜索一下get或者set或者百度一下就知道一些相关内容了。至于this[string key]我不懂,期望高手指教
      

  3.   

    含参属性,也叫索引器比如你想遍历String类型中的每一个字符,就是用到的这个语法。
    String a = “123”;
    a[1]....上面是用一个hash来存储SettingParameter,传一个key进去就能获得相应的SettingParameter值。
      

  4.   

    SettingParameter 是类型.
    this[string key]就当它是一个属性好了。
    在其他地方可以将 this["value1"]作为一个对象使用.给它赋值,或者把它的值给别的对象。