加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:56:38 所属栏目:百科 来源:网络整理
导读:我是ASP.NET与C#的新的现在我需要解决方案一个问题 在PHP中,我可以创建一个这样的数组 $arr[] = array('product_id' = 12,'process_id' = 23,'note' = 'This is Note');//ExampleArray( [0] = Array ( [product_id] = 12 [process_id] = 23 [note] = This is
我是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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读