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