How to Remove duplicates from a IEnumerable using C# or VB.NET

To Remove duplicates from a IEnumerable using C# or VB.NET you can use the snippet below.

Sample .NET 3.5 and newer

Sample C#

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

namespace de.Fesslersoft.DistinctIenumerbable
{
    class Program
    {
        static void Main(string[] args)
        {
            var myList = new List<string>
            {
                "1A", "1A", "1B", "1C", "1C", "1D", "1D"
            };

            myList = myList.Distinct().ToList();

            foreach (var item in myList)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

Sample VB.NET (autogenerated)

Imports System.Collections.Generic
Imports System.Linq

Namespace de.Fesslersoft.DistinctIenumerbable
	Class Program
		Private Shared Sub Main(args As String())
			Dim myList = New List(Of String)() From { _
				"1A", _
				"1A", _
				"1B", _
				"1C", _
				"1C", _
				"1D", _
				"1D" _
			}

			myList = myList.Distinct().ToList()

			For Each item As var In myList
				Console.WriteLine(item)
			Next
			Console.Read()
		End Sub
	End Class
End Namespace

Sample .NET 2.0

Sample C#

using System;
using System.Collections.Generic;

namespace de.Fesslersoft.DistinctIenumerbable
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> myList = new List<string>();
            myList.Add("1A");
            myList.Add("1A");
            myList.Add("1B");
            myList.Add("1C");
            myList.Add("1C");
            myList.Add("1D");
            myList.Add("1D");

            var newList = new List<String>();
            foreach (var item in DistinctIenumerable(myList))
            {
                newList.Add(item);
            }

            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }

        internal static IEnumerable<T> DistinctIenumerable<T>(IEnumerable<T> input)
        {
            var passedValues = new Dictionary<T, bool>();
            foreach (T item in input)
            {
                if (!passedValues.ContainsKey(item))
                {
                    passedValues.Add(item, false);
                    yield return item;
                }
            }
        }
    }
}

Sample VB.NET (autogenerated)

Imports System.Collections.Generic

Namespace de.Fesslersoft.DistinctIenumerbable
	Class Program
		Private Shared Sub Main(args As String())
			Dim myList As List(Of [String]) = New List(Of String)()
			myList.Add("1A")
			myList.Add("1A")
			myList.Add("1B")
			myList.Add("1C")
			myList.Add("1C")
			myList.Add("1D")
			myList.Add("1D")

			Dim newList = New List(Of [String])()
			For Each item As var In DistinctIenumerable(myList)
				newList.Add(item)
			Next

			For Each item As var In newList
				Console.WriteLine(item)
			Next
			Console.Read()
		End Sub

		Friend Shared Function DistinctIenumerable(Of T)(input As IEnumerable(Of T)) As IEnumerable(Of T)
			Dim passedValues = New Dictionary(Of T, Boolean)()
			For Each item As T In input
				If Not passedValues.ContainsKey(item) Then
					passedValues.Add(item, False)
					yield Return item
				End If
			Next
		End Function
	End Class
End Namespace

Please rate this snippet

How to deep copy a IList in C# and VB.NET

To deep copy a IList in C# and VB.NET you can use the snippet.

Sample C#

public static IList<T> Clone<T>(IList<T> listToClone) where T : ICloneable
{
	return listToClone.Select(item => (T)item.Clone()).ToList();
}

Sample VB.NET

Public Shared Function Clone(Of T As ICloneable)(listToClone As IList(Of T)) As IList(Of T)
	Return listToClone.[Select](Function(item) DirectCast(item.Clone(), T)).ToList()
End Function

Please rate this snippet

How to Shuffle a List in C# and VB.NET

To Shuffle a List in C# and VB.NET you can use the snippet below.

Sample C#

public static IList<T> ShuffleIList<T>(IList<T> inputList)
{
	var cryptoServiceProvider = new RNGCryptoServiceProvider();
	var count = inputList.Count;
	while (count > 1)
	{
		var bytes = new byte[1];
		do cryptoServiceProvider.GetBytes(bytes);
		while (!(bytes[0] < count * (Byte.MaxValue / count)));
		var index = (bytes[0] % count);
		count--;
		var input = inputList[index];
		inputList[index] = inputList[count];
		inputList[count] = input;
	}
	return inputList;
}

Sample VB.NET

Public Shared Function ShuffleIList(Of T)(inputList As IList(Of T)) As IList(Of T)
	Dim cryptoServiceProvider = New RNGCryptoServiceProvider()
	Dim count = inputList.Count
	While count > 1
		Dim bytes = New Byte(0) {}
		Do
			cryptoServiceProvider.GetBytes(bytes)
		Loop While Not (bytes(0) < count * ([Byte].MaxValue / count))
		Dim index = (bytes(0) Mod count)
		count -= 1
		Dim input = inputList(index)
		inputList(index) = inputList(count)
		inputList(count) = input
	End While
	Return inputList
End Function

Please rate this snippet

How to convert Stream to ByteArray in C# and VB.NET

To convert Stream to ByteArray in C# and VB.NET you can use the following snippet.

Samples

Sample C#

Sample VB.NET

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Please rate this snippet

How to Bubblesort ILists in C# and VB.NET

To Bubblesort ILists in C# and VB.NET you can use the follwing snippet.

Sample C#

public static IList Sort(IList inputArray)
{
	var counter = inputArray.Count - 1;
	for (var i = 0; i < counter; i++)
	{

		for (var index = counter; index > i; index--)
		{
			if (((IComparable)inputArray[index - 1]).CompareTo(inputArray[index]) <= 0) continue;
			var temp = inputArray[index - 1];
			inputArray[index - 1] = inputArray[index];
			inputArray[index] = temp;
		}
	}
	return inputArray;
}

Sample VB.NET

Public Shared Function Sort(inputArray As IList) As IList
	Dim counter = inputArray.Count - 1
	For i As var = 0 To counter - 1

		For index As var = counter To i + 1 Step -1
			If DirectCast(inputArray(index - 1), IComparable).CompareTo(inputArray(index)) <= 0 Then
				Continue For
			End If
			Dim temp = inputArray(index - 1)
			inputArray(index - 1) = inputArray(index)
			inputArray(index) = temp
		Next
	Next
	Return inputArray
End Function

Please rate this snippet