使用C#的ASP .NET中的数组键值

前端之家收集整理的这篇文章主要介绍了使用C#的ASP .NET中的数组键值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是ASP.NET与C#的新的现在我需要解决方案一个问题

PHP中,我可以创建一个这样的数组

$arr[] = array('product_id' => 12,'process_id' => 23,'note' => 'This is Note');

//Example
Array
(
    [0] => Array
        (
            [product_id] => 12
            [process_id] => 23
            [note] => This is Note
        )

    [1] => Array
        (
            [product_id] => 5
            [process_id] => 19
            [note] => Hello
        )

    [2] => Array
        (
            [product_id] => 8
            [process_id] => 17
            [note] => How to Solve this Issue
        )

)

我想在ASP.NET中使用C#创建相同的数组结构.

请帮我解决这个问题.提前致谢.

解决方法

使用词典< TKey,TValue>用于根据键(字符串)快速查找值(您的对象).
var dictionary = new Dictionary<string,object>();
dictionary.Add("product_id",12);
// etc.

object productId = dictionary["product_id"];

为了简化Add操作,您可以使用集合初始化语法,如

var dictionary = new Dictionary<string,int> { { "product_id",12 },{ "process_id",23 },/* etc */ };

编辑

随着您的更新,我将继续定义一个适当的类型来封装您的数据

class Foo
{
    public int ProductId { get; set; }
    public int ProcessId { get; set; }
    public string Note { get; set; } 
}

然后创建一个数组或该类型的列表.

var list = new List<Foo>
           {
                new Foo { ProductId = 1,ProcessId = 2,Note = "Hello" },new Foo { ProductId = 3,ProcessId = 4,Note = "World" },/* etc */
           };

然后,您可以列出可以迭代的强类型对象,绑定到控件等.

var firstFoo = list[0];
someLabel.Text = firstFoo.ProductId.ToString();
anotherLabel.Text = firstFoo.Note;
原文链接:https://www.f2er.com/csharp/94122.html

猜你在找的C#相关文章