Difference between revisions of "List Csharp"

From Teknologisk videncenter
Jump to: navigation, search
m
m
 
(One intermediate revision by the same user not shown)
Line 4: Line 4:
 
using System.Collections.Generic;
 
using System.Collections.Generic;
  
Dictionary<string, int> ages = new Dictionary<string, int>();
+
List<int> numbers = new List<int>();
  
// fill the Dictionary
+
// Fill the List<int> by using the Add method
ages.Add("John", 51);    // using the Add method
+
foreach (int number in new int[12]{10, 9, 8, 7, 7, 6, 5, 10, 4, 3, 2, 1})
ages.Add("Diana", 50);
+
{
ages["James"] = 23;      // using array notation
+
    numbers.Add(number);
ages["Francesca"] = 21;
+
}
 +
 
 +
// Insert an element in the penultimate position in the list, and move the last item up
 +
// The first parameter is the position; the second parameter is the value being inserted
 +
numbers.Insert(numbers.Count-1, 99);
 +
 
 +
// Remove the first element whose value is 7 (the 4th element, index 3)
 +
numbers.Remove(7);
  
// iterate using a foreach statement
+
// Remove the element that's now the 7th element, index 6 (10)
// the iterator generates a KeyValuePair item
+
numbers.RemoveAt(6);
Console.WriteLine("The Dictionary contains:");
 
  
foreach (KeyValuePair<string, int> element in ages)
+
// Iterate the remaining 11 elements using a for statement
 +
Console.WriteLine("Iterating using a for statement:");
 +
for (int i = 0; i < numbers.Count; i++)
 
{
 
{
    string name = element.Key;
+
    int number = numbers[i];  // Note the use of array syntax
    int age = element.Value;
+
    Console.WriteLine(number);
    Console.WriteLine($"Name: {name}, Age: {age}");
 
 
}
 
}
 +
 +
// Iterate the same 11 elements using a foreach statement
 +
Console.WriteLine("\nIterating using a foreach statement:");
 +
foreach (int number in numbers){    Console.WriteLine(number);}
 
</source>
 
</source>
 +
 +
[[Category:Csharp]]

Latest revision as of 12:53, 19 January 2017

Example

using System;
using System.Collections.Generic;

List<int> numbers = new List<int>();

// Fill the List<int> by using the Add method
foreach (int number in new int[12]{10, 9, 8, 7, 7, 6, 5, 10, 4, 3, 2, 1})
{
    numbers.Add(number);
}

// Insert an element in the penultimate position in the list, and move the last item up
// The first parameter is the position; the second parameter is the value being inserted
numbers.Insert(numbers.Count-1, 99);

// Remove the first element whose value is 7 (the 4th element, index 3)
numbers.Remove(7);

// Remove the element that's now the 7th element, index 6 (10)
numbers.RemoveAt(6);

// Iterate the remaining 11 elements using a for statement
Console.WriteLine("Iterating using a for statement:");
for (int i = 0; i < numbers.Count; i++)
{
    int number = numbers[i];  // Note the use of array syntax
    Console.WriteLine(number);
}

// Iterate the same 11 elements using a foreach statement
Console.WriteLine("\nIterating using a foreach statement:");
foreach (int number in numbers){    Console.WriteLine(number);}