繁体   English   中英

如何在 C# 中创建自定义类型的数组?

[英]How to create an array of custom type in C#?

我创建了一个 object 数组,其中包含不同类型的元素:

public static int a;
public static string b;
public static ushort c;

object[] myobj = new obj[]{ a, b, c};

如果我想创建一个包含此 myobj 类型的 arrays 元素的数组,我该怎么做?

我的意思是这样的:

myobj[] myarray = new myobj[];  <= but to do this, myobj should be a type. 

不知道如何解决。

感谢大家。

创建一个包含您想要的类型的类型并创建一个数组。

public class MyType // can be a struct. Depends on usage.
{
  // should really use properties, I know, 
  // and these should probably not be static
  public static int a;
  public static string b;
  public static ushort c;
}

// elsewhere
MyType[] myobj = new MyType[]{};

不知道为什么你想用object[]跳过篮球并且不得不到处施法。

我们如何使用Dictionary来存储您需要的任何类型?

因此,虽然您不会完全拥有myType.a ,但您可以拥有myType.Values["a"] ,它足够接近,使用标准 C# 构造,并为您提供很多灵活性/可维护性

public class MyType
{
    public MyType()
    {
        this.Values = new Dictionary<object, object>();
    }

    public Dictionary<object, object> Values
    {
        get;
        set;
    }
}

和示例用法:

using System;
using System.Collections.Generic;

public static class Program
{
    [STAThread]
    private static void Main()
    {
        var myTypes = new MyType[3];

        myTypes[0] = new MyType();
        myTypes[1] = new MyType();
        myTypes[2] = new MyType();

        for (var current = 0; current < myTypes.Length; ++current)
        {
            // here you customize what goes where
            myTypes[current].Values.Add("a", current);
            myTypes[current].Values.Add("b", "myBvalue");
            myTypes[current].Values.Add("c", (ushort)current);
        }

        foreach (var current in myTypes)
        {
            Console.WriteLine(
               string.Format("A={0}, B={1}, C={2}", 
                              current.Values["a"], 
                              current.Values["b"],
                              current.Values["c"]));
        }

 }

另外,如果需要,您可以轻松地将indexer属性添加到 class,以便您可以使用语法myType["a"]访问元素。 请注意,您应该在添加或检索值时添加错误检查。

public object this[object index]
{
    get
    {
        return this.Values[index];
    }

    set
    {                    
        this.Values[index] = value;
    }
}

这是一个使用索引器的示例。 将条目增加“1”,以便我们看到输出的差异:

for (var current = 0; current < myTypes.Length; ++current)
{
    myTypes[current]["a"] = current + 1;
    myTypes[current]["b"] = "myBvalue2";
    myTypes[current]["c"] = (ushort)(current + 1);
}

foreach (var current in myTypes)
{
    Console.WriteLine(string.Format("A={0}, B={1}, C={2}", 
                                    current["a"], 
                                    current["b"], 
                                    current["c"]));
}

我做了一些更改,但我认为这符合您想要做的精神。

由于属性是数组元素的唯一区别特征,“静态”没有意义,所以我删除了它。

如果您定义 class 如下:

    public class MyType
    {
        /// <summary>
        /// Need a constructor since we're using properties
        /// </summary>
        public MyType()
        {
            this.A = new int();
            this.B = string.Empty;
            this.C = new ushort();
        }

        public int A
        {
            get;
            set;
        }

        public string B
        {
            get;
            set;
        }

        public ushort C
        {
            get;
            set;
        }
    }

你可以像这样使用它:

using System;

public static class Program
{
    [STAThread]
    private static void Main()
    {
        var myType = new MyType[3];

        myType[0] = new MyType();
        myType[1] = new MyType();
        myType[2] = new MyType();

        for (var i = 0; i < myType.Length; ++i)
        {
            myType[i].A = 0;
            myType[i].B = "1";
            myType[i].C = 2;
        }

        // alternatively, use foreach
        foreach (var item in myType)
        {
            item.A = 0;
            item.B = "1";
            item.C = 2;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM