Answers for "list of list in c#"

C#
18

c# lists

// ------------------- How to initialize Lists? --------------------- //

using System.Collections.Generic   // You need to include this!

// 1º Method
var myList = new List<int>();
//OR
List<int> myList = new List<int>();
  

// 2º Method
List<int> myList = new List<int>() {2, 5, 9, 12};


// 3º Method
string myString = "Hello"
List<char> myList = new List<char>(myString);  // Creates a list of characters from that string 
//OR
var myFirstList = new List<int>() {9, 2, 4, 3, 2, 1};
var mySecondList = new List<int>(myFirstList);   // Copy a list to a second list 




// -------- How to dynamically change the List of elements? --------- //

// Use the Add or Insert method to add one element
myList.Add(4);  

myList.Insert(0,3)  // Insert element "3" in position "0"
  

// Use the AddRange method to add many elements (you can use an array or
// list, for passing the values)
myList.AddRange(new int[3] {3, 5, 5, 9, 2}); 


// Use the Remove method to eliminate specific elements
for (int i = 0; i < myList.Count; i++)   // Use a for loop to remove 
{										// repeated elements
  if ( myList[i] == 5)
  {
  	myList.Remove(myList[i]);
    i--;
  }
}

// Use the Clear method to remove all elements from the list
myList.Clear();




// ---------------- How to create a List of Lists? ------------------ //

List<List<int>> myList = new List<List<int>>(){
	new List<int>() {1,2,3},
	new List<int>() {4,5,6},
	new List<int>() {7,8,9}
};
		
Console.WriteLine(myList.ElementAt(0).ElementAt(1)); // You get "2"
Posted by: Guest on September-16-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
3

to list c#

var arr = new int[] {1, 2, 3};
List<int> list = arr.ToList();
Posted by: Guest on June-17-2020
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