Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将方法索引器更改为索引器的属性?_C# - Fatal编程技术网

C# 如何将方法索引器更改为索引器的属性?

C# 如何将方法索引器更改为索引器的属性?,c#,C#,当我使用: public ArrayList choiceArray = new ArrayList(); public UC_RadioX Item(int index) { return (UC_RadioX)choiceArray[index]; } 如何将方法更改为属性?以下是一个示例: 根据您对另一个答案的回答,我认为您可以更轻松地使用属性公共列表项{get;set;}执行您试图执行的操作,然后,您将直接访问UC_RadioX

当我使用:

public ArrayList choiceArray = new ArrayList();
public UC_RadioX Item(int index)
        {
            return (UC_RadioX)choiceArray[index];
        }
如何将方法更改为属性?

以下是一个示例:

根据您对另一个答案的回答,我认为您可以更轻松地使用属性
公共列表项{get;set;}
执行您试图执行的操作,然后,您将直接访问UC_RadioX对象,并且可以通过索引器访问它。这将适用于C#2.0及更高版本。自动实现的属性
{get;set;}
是C#3.0及更高版本

在实例化为
cc
的对象中访问这些项可以像这样完成:

    cc.Item(0).Checked = true;
    cc.Item(1).Checked = true;
下面是一个例子:

根据您对另一个答案的回答,我认为您可以更轻松地使用属性
公共列表项{get;set;}
执行您试图执行的操作,然后,您将直接访问UC_RadioX对象,并且可以通过索引器访问它。这将适用于C#2.0及更高版本。自动实现的属性
{get;set;}
是C#3.0及更高版本

在实例化为
cc
的对象中访问这些项可以像这样完成:

    cc.Item(0).Checked = true;
    cc.Item(1).Checked = true;
您可以按以下方式定义:

UC_RadioX rx = cc.Item[0];
bool good = cc.Item[1].checked;
这将允许您在类实例本身上使用
[]
符号:

// ...
public ArrayList choiceArray = new ArrayList();
public UC_RadioX this[int index]
{
    get
    {
        return (UC_RadioX)choiceArray[index];
    }
}
// ...
您可以按以下方式定义:

UC_RadioX rx = cc.Item[0];
bool good = cc.Item[1].checked;
这将允许您在类实例本身上使用
[]
符号:

// ...
public ArrayList choiceArray = new ArrayList();
public UC_RadioX this[int index]
{
    get
    {
        return (UC_RadioX)choiceArray[index];
    }
}
// ...

无法更改为此抄送项[0]。选中=true;无法更改为此抄送项[0]。选中=true;此外,通常的做法是将集合名称多元化。不过,我假设您正在尝试尽可能少地更改当前代码。此外,通常的做法是将集合名称多元化。不过,我假设您正在尝试尽可能少地更改当前代码。