C#中使用Property获取数组元素

发布于 2024-08-08 17:12:59 字数 90 浏览 4 评论 0原文

有没有办法使用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 技术交流群。

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

发布评论

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

评论(8

小姐丶请自重 2024-08-15 17:12:59

您是否可能试图保护原始数组?你的意思是你想要通过“属性”(不是它自己的)在数组周围有一个保护性包装器?我正在猜测你问题的细节。这是字符串数组的包装器实现。该数组不能直接访问,只能通过包装器的索引器访问。

using System;

public class ArrayWrapper {

    private string[] _arr;

    public ArrayWrapper(string[] arr) { //ctor
        _arr = arr;
    }

    public string this[int i] { //indexer - read only
        get {
            return _arr[i];
        }
    }
}

// SAMPLE of using the wrapper
static class Sample_Caller_Code {
    static void Main() {
        ArrayWrapper wrapper = new ArrayWrapper(new[] { "this", "is", "a", "test" });
        string strValue = wrapper[2]; // "a"
        Console.Write(strValue);
    }
}

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.

using System;

public class ArrayWrapper {

    private string[] _arr;

    public ArrayWrapper(string[] arr) { //ctor
        _arr = arr;
    }

    public string this[int i] { //indexer - read only
        get {
            return _arr[i];
        }
    }
}

// SAMPLE of using the wrapper
static class Sample_Caller_Code {
    static void Main() {
        ArrayWrapper wrapper = new ArrayWrapper(new[] { "this", "is", "a", "test" });
        string strValue = wrapper[2]; // "a"
        Console.Write(strValue);
    }
}
鹿! 2024-08-15 17:12:59

如果我正确理解您的要求,您可以使用索引器。
索引器(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?

夜还是长夜 2024-08-15 17:12:59

如果属性公开数组:

string s = obj.ArrayProp[index];

如果您的意思是“我可以有一个索引属性”,那么不行 - 但您可以拥有一个带有索引器的类型的属性:

static class Program
{
    static void Main()
    {
        string s = ViaArray.SomeProp[1];
        string t = ViaIndexer.SomeProp[1];
    }
}
static class ViaArray
{
    private static readonly string[] arr = { "abc", "def" };
    public static string[] SomeProp { get { return arr; } }
}
static class ViaIndexer
{
    private static readonly IndexedType obj = new IndexedType();
    public static IndexedType SomeProp { get { return obj; } }
}
class IndexedType
{
    private static readonly string[] arr = { "abc", "def" };
    public string this[int index]
    {
        get { return arr[index]; }
    }
}

If the property exposes the array:

string s = obj.ArrayProp[index];

If you mean "can I have an indexed property", then no - but you can have a property that is a type with an indexer:

static class Program
{
    static void Main()
    {
        string s = ViaArray.SomeProp[1];
        string t = ViaIndexer.SomeProp[1];
    }
}
static class ViaArray
{
    private static readonly string[] arr = { "abc", "def" };
    public static string[] SomeProp { get { return arr; } }
}
static class ViaIndexer
{
    private static readonly IndexedType obj = new IndexedType();
    public static IndexedType SomeProp { get { return obj; } }
}
class IndexedType
{
    private static readonly string[] arr = { "abc", "def" };
    public string this[int index]
    {
        get { return arr[index]; }
    }
}
月下伊人醉 2024-08-15 17:12:59

您需要的是一个可以有输入(索引)的属性。

只有一个这样的属性,称为索引器。

去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!

皓月长歌 2024-08-15 17:12:59

属性不带参数,因此这是不可能的。

例如,您可以构建一个方法。

public string GetStringFromIndex(int i)
{
  return myStringArray[i];
}

当然,您可能想要在该方法中进行一些检查,但您明白了。

Properties don't take parameters, so that won't be possible.

You can build a method, for instance

public string GetStringFromIndex(int i)
{
  return myStringArray[i];
}

Of course you'll probably want to do some checking in the method, but you get the idea.

安人多梦 2024-08-15 17:12:59

我假设您有一个具有私有字符串数组的类,并且您希望能够获取该数组的元素作为您的类的属性。

public class Foo
{
   private string[] bar;

   public string FooBar
   {
       get { return bar.Length > 4 ? bar[4] : null; }
   }
}

不过,这看起来非常糟糕,所以我要么不明白你想要什么,要么可能有更好的方法来做你想要的,但我们需要了解更多信息。

更新:如果您在评论中指出的其他地方有该元素的索引,则可以使用索引器或简单地创建一个获取索引并返回值的方法。我会为本身就是容器的类保留索引器,否则使用方法路由。

public string GetBar( int index )
{
     return bar.Length > index ? bar[index] : null;
}

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.

public class Foo
{
   private string[] bar;

   public string FooBar
   {
       get { return bar.Length > 4 ? bar[4] : null; }
   }
}

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.

public string GetBar( int index )
{
     return bar.Length > index ? bar[index] : null;
}
纵性 2024-08-15 17:12:59

只需从属性返回数组即可;生成的对象将表现为一个数组,因此您可以对其进行索引。

例如:

string s = object.Names[15]

Just return the array from the property; the resulting object will behave as an array, so you can index it.

E.G.:

string s = object.Names[15]
記柔刀 2024-08-15 17:12:59

您所要求的可以完成,如下所示:
您可以初始化一个保存数组的对象,为您提供所需的内容:

public class ArrayIndexer<T> {
    private T[] myArrRef;
    public ArrayIndexer(ref T[] arrRef) {
        myArrRef = arrRef;
    }

    public T this [int index] {
        get { return myArrRef[index]; }
    }
}

然后,在您的类中:

public ArrayIndexer arr;
private SomeType[] _arr;    

//Constructor:
public MyClass(){
    arr = new ArrayIndexer<SomeType>(ref _arr);
}

用法:

myClassObj.arr[2] // Gives the second item in the array.

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:

public class ArrayIndexer<T> {
    private T[] myArrRef;
    public ArrayIndexer(ref T[] arrRef) {
        myArrRef = arrRef;
    }

    public T this [int index] {
        get { return myArrRef[index]; }
    }
}

Then, in your class:

public ArrayIndexer arr;
private SomeType[] _arr;    

//Constructor:
public MyClass(){
    arr = new ArrayIndexer<SomeType>(ref _arr);
}

Usage:

myClassObj.arr[2] // Gives the second item in the array.

Et Voila! An indexed property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文