在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 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;总结
以上是内存溢出为你收集整理的使用C#的ASP .NET中的数组键值全部内容,希望文章能够帮你解决使用C#的ASP .NET中的数组键值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)