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 check if an array contains a value in Javascript

To check if an array contains a value in Javascript you can use the snippet below.

Sample Javascript

Array.prototype.inArray = function (inputValue) {
	var counter;
	for (counter=0; counter < this.length; counter++) {
		if (this[counter] === inputValue) {
			return true;
		}
	}
	return false;
};

Please rate this snippet

How to empty an array in Javascript

To empty an array in Javascript you can use the following methods.

Sample Javascript

myArray = [];

This code will set the variable A to a new empty array (take care if you have references to the “old” array).

or

myArray.length = 0

This will clear the existing array by setting its length to 0.

or

myArray.splice(0,myArray.length)

Working, but not recommended for this task. see Array.prototype.splice()

or

while(myArray.length > 0) {
    myArray.pop();
}

Fastest solution, without breaking references.

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

How to join a array to string in Python

To join a array to string in Python you can use the following snippet.

Sample Python

def JoinArray(input, delimiter, appendToEnd):
	delimiter = ';'
	joinedString = delimiter.join(input)
	if appendToEnd:
		joinedString = joinedString + delimiter
		return joinedString
	else:
		return joinedString

list = ['This', 'is', 'a', 'Test','!']
print JoinArray(list, ';', False)

Please rate this snippet

How to join a array to string in Java

To join a array to string in Java you can use the following snippet.

Sample Java

public static String JoinArraylist(List<String> input, String delimiter, Boolean appendToEnd){
	Iterator<String> iteratorString = input.iterator();
	StringBuilder stringBuilder = new StringBuilder();
	for (;;) {
		stringBuilder.append(iteratorString.next());
		if (!iteratorString.hasNext()){ 
			break;
		}
		stringBuilder.append(delimiter);
	}
	if(appendToEnd){
		stringBuilder.append(delimiter);
	}
	return stringBuilder.toString();
}

Please rate this snippet