ASP.NET匿名オブジェクトとコレクションの使用
2509 ワード
、
1、
:
var vobj = new { id = " ", title = " ", content = " " };
:
var id = vobj.id;
:
2、
:
var article = new[]
{
new { id = " ", title = " ", content = " " }
new { id = " ", title = " ", content = " " }
};
:
var title = article[1].title;
:
3、 List
:
var list = article.ToList(); //Array List
list.Add(new { id = " ", title = " ", content = " " });
article = list.ToArray(); //List Array
:
var content = article[2].content;
:
4、 JavaScriptSerializer
var str = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
:JSON
5、
DropDownList ddl = new DropDownList();
ddl.DataSource = list;
ddl.DataTextField = "title";
ddl.DataValueField = "id";
ddl.DataBind();
:var C# 3.0 , ( )
6、Dictionary
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("key", "value");
:
7、NameValueCollection
NameValueCollection col = new NameValueCollection();
col.Add("key", "value");
:
8、Hashtable
Hashtable ht = new Hashtable();
ht.Add("key","value");
:
9、List
List<string> list = new List<string>();
list.Add("String");
: , /
10、 LINQ
using System.Linq;
using System.Collections;
int[] items = new int[] { 1, 2, 3, 4, 5 };
IEnumerable<int> ints = from item in items
where item > 2.5
select item;
foreach (var p in ints)
{
Console.WriteLine(p);
}
:IEnumerable
: Linq , JSON ;
11、
public System.Collections.IEnumerator GetEnumerator()
{
string[] days = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" };
for (int i = 0; i < days.Length; i++)
{
yield return days[i];
}
}