C#中使用Property获取数组元素
有没有办法使用Property从字符串数组中获取特定元素(基于索引)。我更喜欢使用公共属性而不是公开字符串数组。我正在研究 C#.NET 2.0
问候
Is there a way to get a specific element (based in index) from a string array using Property. I prefer using public property in place of making the string array public. I am working on C#.NET 2.0
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您是否可能试图保护原始数组?你的意思是你想要通过“属性”(不是它自己的)在数组周围有一个保护性包装器?我正在猜测你问题的细节。这是字符串数组的包装器实现。该数组不能直接访问,只能通过包装器的索引器访问。
Are you possibly trying to protect the original array; do you mean you want a protective wrapper around the array, through "a Property" (not of its own)? I'm taking this shot at guessing the details of your question. Here's a wrapper implementation for a string array. The array cannot be directly access, but only through the wrapper's indexer.
如果我正确理解您的要求,您可以使用索引器。
索引器(C# 编程指南)
编辑:现在我已经阅读了其他人,也许您可以公开一个返回数组副本的属性?
If I understand correctly what you are asking, You can use an indexer.
Indexers (C# Programming Guide)
Edit: Now that I've read the others, maybe you can expose a property that returns a copy of the array?
如果属性公开数组:
如果您的意思是“我可以有一个索引属性”,那么不行 - 但您可以拥有一个带有索引器的类型的属性:
If the property exposes the array:
If you mean "can I have an indexed property", then no - but you can have a property that is a type with an indexer:
您需要的是一个可以有输入(索引)的属性。
只有一个这样的属性,称为索引器。
去MSDN上查一下。
快捷方式:使用内置代码片段:转到您的班级并输入“indexer”,然后按 Tab 键两次。中提琴!
What you need is a Property that can have input (an index).
There is only one property like that, called an Indexer.
Look it up on MSDN.
A shortcut: use a built in code snippet: go to your class and type 'indexer' then press tab twice. Viola!
属性不带参数,因此这是不可能的。
例如,您可以构建一个方法。
当然,您可能想要在该方法中进行一些检查,但您明白了。
Properties don't take parameters, so that won't be possible.
You can build a method, for instance
Of course you'll probably want to do some checking in the method, but you get the idea.
我假设您有一个具有私有字符串数组的类,并且您希望能够获取该数组的元素作为您的类的属性。
不过,这看起来非常糟糕,所以我要么不明白你想要什么,要么可能有更好的方法来做你想要的,但我们需要了解更多信息。
更新:如果您在评论中指出的其他地方有该元素的索引,则可以使用索引器或简单地创建一个获取索引并返回值的方法。我会为本身就是容器的类保留索引器,否则使用方法路由。
I'm assuming that you have a class that has a private string array and you want to be able to get at an element of the array as a property of your class.
This seems horribly hacky, though, so I'm either not understanding what you want or there's probably a better way to do what you want, but we'd need to know more information.
Update: If you have the index of the element from somewhere else as you indicate in your comment, you could use an indexer or simply create a method that takes the index and returns the value. I'd reserve the indexer for a class that is itself a container and use the method route otherwise.
只需从属性返回数组即可;生成的对象将表现为一个数组,因此您可以对其进行索引。
例如:
Just return the array from the property; the resulting object will behave as an array, so you can index it.
E.G.:
您所要求的可以完成,如下所示:
您可以初始化一个保存数组的对象,为您提供所需的内容:
然后,在您的类中:
用法:
Et Voila!索引属性。
What you're asking can be done, like so:
You can initialize an object that holds your array, giving you exactly what you need:
Then, in your class:
Usage:
Et Voila! An indexed property.