Examples of list Usage in C

  • 2021-10-25 07:41:23
  • OfStack

This article illustrates the use of list in C #. Share it for your reference, as follows:


protected void Page_Load(object sender, EventArgs e)
{
  List<string> studentNames = new List<string>();
  studentNames.Add("John");
  studentNames.Add("Mary");
  studentNames.Add("Rose");
  // Show each element 
  foreach (string item in studentNames)
  {
    Response.Write(item);
    Response.Write("<br/>");
  }
  Response.Write("<br/><br/>");
  //List Convert to a symbol-delimited string 
  string studentAllName = string.Join(",", studentNames.ToArray());
  Response.Write(studentAllName);
  Response.Write("<br/><br/>");
  List<decimal> studentScore = new List<decimal>();
  studentScore.Add(100);
  studentScore.Add(98);
  studentScore.Add(59);
  // Sort 
  studentScore.Sort();
  // Reverse sort 
  studentScore.Reverse();
  // Show each element 
  foreach (decimal score in studentScore)
  {
    Response.Write(score);
    Response.Write("<br/>");
  }
  // Total SUM
  Response.Write(" Total score " + studentScore.Sum());
  Response.Write("<br/>");
  //List Does it exist in 
  Response.Write(studentScore.Exists(MatchPRE));
  Response.Write("<br/><br/>");
  //List Convert to JSon
  List<Student> list = new List<Student>();
  for (int i = 0; i < 5; i++)
  {
    Student a = new Student();
    a.Name = " Zhang 3" + i;
    a.Age = i;
    a.Sex = " Male ";
    list.Add(a);
  }
  string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
  Response.Write(json);
  Response.Write("<br/><br/>");
}
private static bool MatchPRE(decimal p)// Conditional matching function, list1 Each element in the is passed in P Medium                                       // The function returns after matching 
{
  if (p == 100)// This sentence is a match condition. If it matches, return, and you can change it to the value you want at will 
    return true;
  else
  {
    return false;
  }
}
public struct Student
{
  public string Name;
  public int Age;
  public string Sex;
}

For more readers interested in C # related content, please check the topics on this site: "Summary of Thread Use Skills in C # Programming", "Summary of C # Operating Excel Skills", "Summary of XML File Operation Skills in C #", "C # Common Control Usage Tutorial", "WinForm Control Usage Tutorial", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: