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 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

How to make a List or Collection of class read-only in C# and VB.NET

To make a List or Collection of class read-only in C# and VB.NET you can use the following snippet.

Sample C#

public class Test
{
	private readonly List<int> _testList = new List<int>();

	public IList<int> TestList 
	{
		get { return _testList.AsReadOnly(); }
	}

	public Test()
	{
		_testList.Add(0);
		_testList.Add(1);
		_testList.Add(2);
		_testList.Add(3);
		_testList.Add(4);
		_testList.Add(5);
	}
}

Sample VB.NET

Public Class Test
	Private ReadOnly _testList As New List(Of Integer)()

	Public ReadOnly Property TestList() As IList(Of Integer)
		Get
			Return _testList.AsReadOnly()
		End Get
	End Property

	Public Sub New()
		_testList.Add(0)
		_testList.Add(1)
		_testList.Add(2)
		_testList.Add(3)
		_testList.Add(4)
		_testList.Add(5)
	End Sub
End Class

Please rate this snippet

How to split a string into collection in VBA

To split a string into collection in VBA you can use the following snippet.

Sample VBA

Public Function SplitStringToCollection(sInput As String, Optional sSplitter As String = ";") As Collection
Dim lCounter As Long
Dim sTemp() As String

On Error GoTo errHandler
Set SplitStringToCollection = New Collection
sTemp = Split(sInput, sSplitter)
For lCounter = 0 To UBound(sTemp)
    If (CStr(Replace(sTemp(lCounter), sSplitter, vbNullString)) <> vbNullString) Then
        SplitStringToCollection.Add CStr(Replace(sTemp(lCounter), sSplitter, vbNullString))
    End If
Next lCounter
Exit Function

errHandler:
Set SplitStringToCollection = New Collection
'handle the exception your way
End Function

Please rate this snippet

How to sort a collection in VBA

To sort a collection in VBA you can use the following snippet.
This snippet is using bubblesort.

Sample VBA

Public Function SortCollection(colInput As Collection) As Collection
Dim iCounter As Integer
Dim iCounter2 As Integer
Dim temp As Variant

Set SortCollection = New Collection
For iCounter = 1 To colInput.Count - 1
    For iCounter2 = iCounter + 1 To colInput.Count
        If colInput(iCounter) > colInput(iCounter2) Then
           temp = colInput(iCounter2)
           colInput.Remove iCounter2
           colInput.Add temp, temp, iCounter
        End If
    Next iCounter2
Next iCounter
Set SortCollection = colInput
End Function

for more informations see Bubble sort

Please rate this snippet