Can we do something like this or something similar?
var staffs = {
"Staff":
[
{ "ID" : 1, "Name" : "John" },
{ "ID" : 2, "Name" : "Mark"}
]
};
foreach (var staff in staffs)
{
Console.WriteLine("ID: {0}", staff.ID);
Console.WriteLine("Name: {0}", staff.Name);
}
Instead of this long code:
public class Test
{
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
return;
List<Staff> staffs = new List<Staff>()
{
new Staff() {ID = 1, Name = "John"},
new Staff() {ID = 2, Name = "Mark"}
};
foreach (var staff in staffs)
{
Console.WriteLine("ID: {0}", staff.ID);
Console.WriteLine("Name: {0}", staff.Name);
}
}
}
public class Staff
{
public int ID { get; set; }
public string Name { get; set; }
}
I tried but Visual Studio shows syntax error.