C#:公共方法{get{}set{}}问题

发布于 2024-08-05 14:47:11 字数 264 浏览 8 评论 0原文

我不完全确定我的所有术语是否正确,所以如果我错了,请原谅我。我想知道是否可以向该方法发送参数。以下面为例。

public item (int index)  
{  
    get { return list[index]; }  
    set { list[index] = value; }  
}

我知道如果这样的话,它会出错。我要问的是是否有某种方法可以让它发挥作用。有什么建议或者我应该想办法解决它吗?

提前致谢。

I'm not entirely sure if I have all the terminology correct so forgive me if I'm wrong. I was wondering if it would be possible to send an argument(s) to the method. Take the following for example.

public item (int index)  
{  
    get { return list[index]; }  
    set { list[index] = value; }  
}

I know that as it is, it will error. What I'm asking is if there is some way to get it working. Any suggestions or should I figure out some way around it?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

墨离汐 2024-08-12 14:47:11

试试这个:

// This returns an instance of type "Foo",
// since I didn't know the type of "list".
// Obviously the return type would need to
// match the type of whatever "list" contains.
public Foo this[int index]
{
    get { return list[index]; }
    set { list[index] = value; }
}

这是 C# 的 索引器 语法,它有一些限制 (它不像 VB.NET 的参数化属性那么灵活),但它确实适用于您的特定示例。

Try this:

// This returns an instance of type "Foo",
// since I didn't know the type of "list".
// Obviously the return type would need to
// match the type of whatever "list" contains.
public Foo this[int index]
{
    get { return list[index]; }
    set { list[index] = value; }
}

This is C#'s indexer syntax and it has some limitations (it's not as flexible as VB.NET's parameterful properties) but it does work for your specific example.

川水往事 2024-08-12 14:47:11

正如其他人所示,您可以将其变成索引器 - 顺便说一句,它可以有多个参数。

不能做的是在C#中命名索引器...尽管您可以在VB中做到。因此,您不能有两个索引器,一个称为 Foo,另一个称为 Bar...您需要编写返回值的属性,这些值本身是可索引的。说实话,有点痛苦:(

As others have shown, you can turn it into an indexer - which can have multiple parameters, by the way.

What you can't do is name an indexer in C#... although you can in VB. So you can't have two indexers, one called Foo and the other called Bar... you'd need to write properties which returned values which were themselves indexable. It's a bit of a pain, to be honest :(

深空失忆 2024-08-12 14:47:11

这称为索引器属性

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

This is called indexer property

public int this [int index]  
{      
     get { return list[index]; }      
     set { list[index] = value; }  
}
东京女 2024-08-12 14:47:11

我想您可能正在寻找的是:

public Something this[int index]
{
    get
    {
         return list[index];
    }
    set
    {
         list[index] = value;
    }
}

I think what you might be looking for is:

public Something this[int index]
{
    get
    {
         return list[index];
    }
    set
    {
         list[index] = value;
    }
}
我的奇迹 2024-08-12 14:47:11

作为记录,虽然其他答案是有效的,但您可能还需要考虑使用以下方法:

public IList<Something> items { get; set; }

然后可以按如下方式使用:

Something item = myFoo.items[1];

其他答案将按以下稍有不同的方式使用:

Something item = myFoo[1];

您想要的答案取决于您到底想要实现什么目标,如果不查看其余代码就很难确定。

For the record, Whilst the other answers are valid, you might also want to consider using the following approach:

public IList<Something> items { get; set; }

This could then be used as follows:

Something item = myFoo.items[1];

The other answers would be used in the following, slightly different, way:

Something item = myFoo[1];

The one you want depends on what exactly you are trying to achieve, which is difficult to determine without seeing the rest of the code.

苏佲洛 2024-08-12 14:47:11

除了现在已经多次提到的索引器之外,另一种可能性是使用索引器创建一个自定义类并将其实例作为属性返回。

例子:

public class IntList
{
    public IntList(IEnumerable<int> source)
    {
        items = source.ToArray();
        Squares = new SquareList(this);
    }

    private int[] items;

    // The indexer everyone else mentioned
    public int this[int index]
    {
        get { return items[index]; }
        set { items[index] = value; }
    }

    // Other properties might be useful:
    public SquareList Squares { get; private set; }

    public class SquareList
    {
        public SquareList(IntList list)
        {
            this.list = list;
        }

        private IntList list;

        public int this[int index]
        {
            get { return list.items[index] * list.items[index]; }
        }
    }
}

Besides the indexer that has been mentioned several times now, another possibility is to make a custom class with an indexer and return an instance of it as a property.

Example:

public class IntList
{
    public IntList(IEnumerable<int> source)
    {
        items = source.ToArray();
        Squares = new SquareList(this);
    }

    private int[] items;

    // The indexer everyone else mentioned
    public int this[int index]
    {
        get { return items[index]; }
        set { items[index] = value; }
    }

    // Other properties might be useful:
    public SquareList Squares { get; private set; }

    public class SquareList
    {
        public SquareList(IntList list)
        {
            this.list = list;
        }

        private IntList list;

        public int this[int index]
        {
            get { return list.items[index] * list.items[index]; }
        }
    }
}
御弟哥哥 2024-08-12 14:47:11

您可以使用索引器来解决这个问题

public object this[string name]
        {
            get
            {
                int idx = FindParam(name);
                if (idx != -1)
                    return _params[idx].Value;

                throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
            }
            set 
            { 
                int idx = FindParam(name);
                if (idx != -1)
                    _params[idx].Value = value;
                else
                    throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
            }
        }

You can use indexator for solving this problem

public object this[string name]
        {
            get
            {
                int idx = FindParam(name);
                if (idx != -1)
                    return _params[idx].Value;

                throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
            }
            set 
            { 
                int idx = FindParam(name);
                if (idx != -1)
                    _params[idx].Value = value;
                else
                    throw new IndexOutOfRangeException(String.Format("Parameter \"{0}\" not found in this collection", name));
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文