SoFunction
Updated on 2025-03-07

Example of list usage in C#

This article describes the usage 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>();
  ("John");
  ("Mary");
  ("Rose");
  //Show each element  foreach (string item in studentNames)
  {
    (item);
    ("<br/>");
  }
  ("<br/><br/>");
  //Convert List into symbol-separated string  string studentAllName = (",", ());
  (studentAllName);
  ("<br/><br/>");
  List<decimal> studentScore = new List<decimal>();
  (100);
  (98);
  (59);
  //Sort  ();
  //Invert the sort  ();
  //Show each element  foreach (decimal score in studentScore)
  {
    (score);
    ("<br/>");
  }
  //Total SUM  ("Total Score" + ());
  ("<br/>");
  // Does it exist in List  ((MatchPRE));
  ("<br/><br/>");
  //Convert List to JSon  List<Student> list = new List<Student>();
  for (int i = 0; i < 5; i++)
  {
    Student a = new Student();
     = "Zhang San" + i;
     = i;
     = "male";
    (a);
  }
  string json = new ().Serialize(list);
  (json);
  ("<br/><br/>");
}
private static bool MatchPRE(decimal p)// Conditional matching function, each element in list1 will be passed into P // The function returns after matching{
  if (p == 100)//This sentence is a matching condition. If it matches, return, 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 information about C# related content, please check out the topic of this site:Summary of thread usage techniques for C# programming》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

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