C# 在C中公开数组元素的属性#

C# 在C中公开数组元素的属性#,c#,arrays,properties,C#,Arrays,Properties,我想在C#中创建一个属性,用于设置或返回数组的单个成员。目前,我有: private string[] myProperty; public string MyProperty[int idx] { get { if (myProperty == null) myProperty = new String[2]; return myProperty[idx]; } set { myP

我想在C#中创建一个属性,用于设置或返回数组的单个成员。目前,我有:

private string[] myProperty;
public string MyProperty[int idx]
{
    get
    {
        if (myProperty == null)
            myProperty = new String[2];

        return myProperty[idx];
    }
    set
    {
        myProperty[idx] = value;
    }
}
但是,我得到以下编译错误:

坏数组声明符:要声明托管数组,秩说明符位于变量标识符之前。要声明固定大小的缓冲区字段,请在字段类型之前使用fixed关键字


您必须使用
作为索引器的属性名称。

您需要使用索引器。它的工作原理有点不同。见示例:

public class Node
{
    public Node this[int offset]
    {
        get { return localList[offset]; }
    }
}
注意:每个类只允许一个索引器。原因是编译器对它的意思太混乱了,所以只允许您使用一个

您也可以这样做:

private static int[] _widget = new int[Counter];
public static int [] Widget
{
    get { return _widget; }
    set { _widget = value; }
}

for(int i=0;i
C#每个类只允许一个索引属性,因此您必须使用此属性

一个选项是按如下方式重新编码:

private string[] myProperty = new string[2]; 
public string[] MyProperty
{ 
    get 
    { 
        return myProperty;
    } 
    set 
    { 
        myProperty = value; 
    } 
} 

它会编译,但它确实有自己的一系列问题(fxCop会大喊大叫,但它会引导您选择其他选项)。

如何:编写一个只做一件事的类:提供对某个底层索引集合元素的随机访问。给这个类一个
这个
索引器

对于要提供随机访问的属性,只需返回此索引器类的实例

琐碎的实现:

public class Indexer<T>
{
    private IList<T> _source;

    public Indexer(IList<T> source)
    {
        _source = source;
    }

    public T this[int index]
    {
        get { return _source[index]; }
        set { _source[index] = value; }
    }
}

public static class IndexHelper
{
    public static Indexer<T> GetIndexer<T>(this IList<T> indexedCollection)
    {
        // could cache this result for a performance improvement,
        // if appropriate
        return new Indexer<T>(indexedCollection);
    }
}
公共类索引器
{
私人IList_来源;
公共索引器(IList源)
{
_来源=来源;
}
公共T此[int索引]
{
获取{return _source[index];}
设置{u source[index]=value;}
}
}
公共静态类索引帮助器
{
公共静态索引器GetIndexer(此IList indexedCollection)
{
//可以缓存此结果以提高性能,
//如果合适的话
返回新的索引器(indexedCollection);
}
}
重构到代码中:

private string[] myProperty;
public Indexer<string> MyProperty
{
    get
    {
        return myProperty.GetIndexer();
    }
}
私有字符串[]myProperty;
公共索引器MyProperty
{
得到
{
返回myProperty.GetIndexer();
}
}

这将允许您拥有所需数量的索引属性,而无需使用
IList
界面公开这些属性。

您可以执行以下操作:

class Indexers
{
    private string[] _strings = new [] {"A","B"};
    private int[] _ints = new[] { 1, 2 };

    public string[] Strings
    {
        get{ return _strings;}
    }

    public int[] Ints
    {
        get{ return _ints;}
    }
}

class Program
{
    static void Main(string[] args)
    {
        Indexers indexers = new Indexers();

        int a1 = indexers.Ints[0];
        string a2 = indexers.Strings[0];
    }
}

您可以这样使用它:

    private string[] myProp;
    public string[] MyProp
    {
        get
        {
            if (myProp == null)
            {
                myProp = new String[2];
            }
            return myProp;
        }

        set
        {
            myProp = value;
        }
    }

而且可以将myProp[1]作为myProp[1]访问,例如

通过只读属性公开阵列可能满足您的需要。由于您不希望允许其他代码这样分配数组,因此不需要公共setter:

private string[] myProperty;
public string[] MyProperty
{
    get
    {
        if (myProperty == null)
        {
            myProperty = new String[2];
        }

        return myProperty;
    }  
}
然后您可以编写如下代码:

theObject.MyProperty[1] = "some string";
…但不能替换阵列本身:

theObject.MyProperty = new string[2]; // will not compile

首先,字段内声明避免了过度检查:

private string[] myProperty = new string[2];
您可以通过按输入类型重载来实现多个索引器:

public string this[int index]
{
    get
    {
        return myProperty[index];
    }
    set
    {
        myProperty[index] = value;
    }
}

public object this[object a, object b] // different input type(s) (and different return type)
{
    get
    {
        // do other stuff
    }
}
C#不提供创建索引属性的内置机制。您可以使用类级索引器(使用
this[int index]
notation),但在属性级上不能使用类似的方法


一个选项是使用索引器创建帮助器类,并将该类用作属性类型。请参阅MSDN上的。如果我有多个,该怎么办?您必须让MyProperty返回某种可索引列表。如果您需要严格的控制,您可能希望自己实现它。这比最初的接口更强大,它只允许修改元素。这实际上允许通过set替换底层数组,但事实并非如此。只要签名不同,即用作索引值的类型不同,就可以有多个索引器。然后情况就发生了变化。最初这种限制确实存在。你介意发布新的语法并告诉我们它是什么时候被添加到语言中的吗?也许我们在讨论的目的不同:你可以在
public int this[string index]
的同时声明
public int this[int index]
,它们可以做两件完全不同的事情。我会说这不仅仅是一个索引属性,明白了。从类设计器的角度来看,您仍然有一个属性,因为它只有一个名称。想象一下StringList类,它在其他语言中可能有属性字符串[int]、对象[string key]、名称[int]、值[string name]等。在C#中,您无法实现它们,即使可以,它也不明显,几乎不可用。
public string this[int index]
{
    get
    {
        return myProperty[index];
    }
    set
    {
        myProperty[index] = value;
    }
}

public object this[object a, object b] // different input type(s) (and different return type)
{
    get
    {
        // do other stuff
    }
}