IsSmallerThan generic extension method for C# and VB.NET

This is the IsSmallerThan generic extension method, this extension method which is part of the Fesslersoft.Extensions. It should only work with numeric values.

Samples

Sample C#

Sample VB.NET


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

Related links:

IsLargerThan generic extension method for C# and VB.NET

This is the IsLargerThan generic extension method, this extension method which is part of the Fesslersoft.Extensions. It should only work with numeric values.

Samples

Sample C#

Sample VB.NET

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

Related links:

Generic Type Conversion snippet for C# and VB.NET

Here you can find a generic Type conversion snippet for C# and VB.NET.

Sample C#

public static T GenericConvert<T, X>(X value) where X : IConvertible
{
	return (T)Convert.ChangeType(value, typeof(T));
}

Sample VB.NET

Public Shared Function GenericConvert(Of T, X As IConvertible)(value As X) As T
	Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
End Function

How to get the type of a generic parameter in C# and VB.NET

To get the type of a generic parameter in C# and VB.NET you can use the following snippet.

Sample C#

Type typeParam = typeof(T);

Sample VB.NET

Dim typeParam As Type = GetType(T)

How to iterate a dictionary in C# and VB.NET

To iterate a dictionary in C# and VB.NET you can use the following snippet.

Sample C#

var myDictionary = new Dictionary<int, string>();
myDictionary.Add(10, "Value1");
myDictionary.Add(20, "Value2");
myDictionary.Add(30, "Value3");

foreach (KeyValuePair<int, string> myPair in myDictionary)
{
    Console.WriteLine("{0} - {1}", myPair.Key, myPair.Value);
}

Sample VB.NET

Dim myDictionary = New Dictionary(Of Integer, String)()
myDictionary.Add(10, "Value1")
myDictionary.Add(20, "Value2")
myDictionary.Add(30, "Value3")

For Each myPair As KeyValuePair(Of Integer, String) In myDictionary
	Console.WriteLine("{0} - {1}", myPair.Key, myPair.Value)
Next

for more informations see KeyValuePair

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