Answers for "list function in c#"

C#
2

c# list

// This will create a new list called 'nameOfList':
var nameOfList = new List<string> 
{
  "value1",
  "value2",
  "value3"
};
Posted by: Guest on October-28-2020
1

List c#

using System;
using System.Collections.Generic;
using System.Linq;

public class ListExample
{
  static List<Animals> animalsList = new List<Animals>();

  class Animals //You can use "class" or "struct"
  {
    public string animalName;
    public int animalCount;
  }

  public static void Main()
  {
    animalsList.Clear(); //This method clears the animals list

    animalsList.Add(CreateAnimal("Elephant", 5)); //Add a new element to the list
    animalsList.Add(CreateAnimal("Monkey", 18));  //Add a new element to the list
    animalsList.Add(CreateAnimal("Pig", 20));     //Add a new element to the list
    animalsList.Add(CreateAnimal("Cow", 4));      //Add a new element to the list

    for (int i = 0; i < animalsList.Count(); i++)
    {
      Console.WriteLine("There are " + animalsList[i].animalCount + " " + animalsList[i].animalName);
      //Console output:
      //There are 5 Elephant
      //There are 18 Monkey
      //There are 20 Pig
      //There are 4 Cow
    }
  }

  static Animals CreateAnimal(string name, int count) //A simple animal element setup method
  {
    Animals newAnimal = new Animals();
    newAnimal.animalName = name;   //Set animal name
    newAnimal.animalCount = count; //Set animals count

    return newAnimal;
  }
}
Posted by: Guest on October-14-2021
0

lists in c#

public class Part : IEquatable<Part>
    {
        public string PartName { get; set; }
        public int PartId { get; set; }
    }
public class Example
{
    public static void Main()
    {
      // Create a list of parts.
      List<Part> parts = new List<Part>();
      // Add parts to the list.
      parts.Add(new Part() {PartName="crank arm", PartId=1234});
      parts.Add(new Part() { PartName = "chain ring", PartId = 1334 });
      //
      foreach (Part aPart in parts)
        Console.WriteLine(aPart);
    }
}
Posted by: Guest on June-14-2021
0

list C#

List<string> names = new List<string>();List<Object> someObjects = new List<Object>();
Posted by: Guest on September-25-2021

C# Answers by Framework

Browse Popular Code Answers by Language