C#-Indexed Properties?

后端 未结 3 1170
余生分开走
余生分开走 2021-01-19 16:35

I\'ve been using Visual Basic for quite a while and have recently made the decision to begin learning C# as a step forward into learning more complex languages.

As a

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-19 16:44

    I don't think you can specify a property only accessible with indexing, but you could just return an indexable value (like an array or List) and use [] on the result:

    public List Aproperty
    {
        get 
        {
             return this.theList;
        }
    }
    
    Aclass foo = this.Apropety[0];
    

    Of course, anything with an indexer will work instead of List.

    Or, you could do it the other way around: define an indexer (on the class itself) that returns an object that has a property Aproperty, used like so: Aclass foo = this[0].Aproperty.

提交回复
热议问题