Var

Here we see how you can use List collections with the var keyword. This can greatly shorten your lines of code, which sometimes improves readability. The var keyword has no effect on performance, only readability for programmers.
Var
Program that uses var with List [C#] using System.Collections.Generic; class Program { static void Main() { var list1 = new List<int>(); // <- var keyword used List<int> list2 = new List<int>(); // <- Is equivalent to } }
C# List Concat

Two Lists can be combined. With the Concat extension method, we do this without a loop. Concat is located in the System.Linq namespace. In this example we examine the Concat method on Lists.
C# Remove Duplicates

Programs sometimes must remove duplicate List elements. There are many ways to remove these elements. Some are easier to implement than others. One approach uses the Distinct extension method.
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// List with duplicate elements.
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(3);
list.Add(4);
list.Add(4);
list.Add(4);
foreach (int value in list)
{
Console.WriteLine("Before: {0}", value);
}
// Get distinct elements and convert into a list again.
List<int> distinct = list.Distinct().ToList();
foreach (int value in distinct)
{
Console.WriteLine("After: {0}", value);
}
}
}
Output
Before: 1
Before: 2
Before: 3
Before: 3
Before: 4
Before: 4
Before: 4
After: 1
After: 2
After: 3
After: 4
Comments
Post a Comment