.net3.0在类的属性构造,类的初始化;泛型类的初始化,字典的初始化上相比与2.0都有所改进,以下是做好的一个DEMO:
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Initializers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//-------------注释部分为DotNet2.0用法----------------------
//-----类的初始化
//Myclass myclass = new Myclass();
//myclass.Name = "qiujuan";
Staff qj = new Staff { Name = "Qiujuan" };
Staff syf = new Staff { Name = "Shangyufeng" };
Staff zjy = new Staff { Name = "Zhaojinyu" };
//-----泛型类的初始化
//List<Staff> Group3 = new List<Staff>(3);
//Group3.Add(syf);
//Group3.Add(qj);
//Group3.Add(zjy);
List<Staff> Group3 = new List<Staff> { syf, zjy, qj };
//-----字典的初始化
//IDictionary<string, int> age = new Dictionary<string, int>(3);
//age.Add("yf", 27);
//age.Add("jy", 24);
//age.Add("qj",22);
IDictionary<string,int> age=new Dictionary<string,int>{{"yf",27},{"jy",24},{"qj",22}};
this.lblStaff.Text = syf.Name+","+zjy.Name+","+qj.Name;
foreach (Staff s in Group3)
{
s.Name += "-";
this.lblGroup.Text += s.Name;
}
this.lblGroup.Text = this.lblGroup.Text.ToString().TrimEnd('-');
foreach (KeyValuePair<string, int> agekey in age)
{
Response.Write(string.Format("姓名:{0}<br>年龄:{1}<br>", agekey.Key, agekey.Value));
}
}
class Staff
{
//----属性构造器
//private string Name;
//public string Name
//{
// get { return Name; }
// set {Name=value}
//}
public string Name { set; get; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Initializers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//-------------注释部分为DotNet2.0用法----------------------
//-----类的初始化
//Myclass myclass = new Myclass();
//myclass.Name = "qiujuan";
Staff qj = new Staff { Name = "Qiujuan" };
Staff syf = new Staff { Name = "Shangyufeng" };
Staff zjy = new Staff { Name = "Zhaojinyu" };
//-----泛型类的初始化
//List<Staff> Group3 = new List<Staff>(3);
//Group3.Add(syf);
//Group3.Add(qj);
//Group3.Add(zjy);
List<Staff> Group3 = new List<Staff> { syf, zjy, qj };
//-----字典的初始化
//IDictionary<string, int> age = new Dictionary<string, int>(3);
//age.Add("yf", 27);
//age.Add("jy", 24);
//age.Add("qj",22);
IDictionary<string,int> age=new Dictionary<string,int>{{"yf",27},{"jy",24},{"qj",22}};
this.lblStaff.Text = syf.Name+","+zjy.Name+","+qj.Name;
foreach (Staff s in Group3)
{
s.Name += "-";
this.lblGroup.Text += s.Name;
}
this.lblGroup.Text = this.lblGroup.Text.ToString().TrimEnd('-');
foreach (KeyValuePair<string, int> agekey in age)
{
Response.Write(string.Format("姓名:{0}<br>年龄:{1}<br>", agekey.Key, agekey.Value));
}
}
class Staff
{
//----属性构造器
//private string Name;
//public string Name
//{
// get { return Name; }
// set {Name=value}
//}
public string Name { set; get; }
}
}
}