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:

Please rate this snippet

IsNull extension method for C# and VB.NET

You can use the IsNull Extension method by using the snippet below.

Sample C#

public static bool IsNull(this object source)
{
     return source == null;
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsNull(source As Object) As Boolean
	Return source Is Nothing
End Function

As always, this extension method has been added to the Github Fesslersoft.Extensions Repository

Please rate this snippet

How to check if a number is a power of 2 in C# and VB.NET

To check if a number is a power of 2 in C# and VB.NET you can use the following extension method.

Sample C#

public static bool IsPowerOfTwo(this ulong input)
{
    return (input != 0) && ((input & (input - 1)) == 0);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsPowerOfTwo(input As ULong) As Boolean
	Return (input <> 0) AndAlso ((input And (input - 1)) = 0)
End Function

As always, the extension method was added to out Fesslersoft.Extensions library @Github

Please rate this snippet

How to check if a character is Uppercase in C# and VB.NET

To check if a character is Uppercase in C# and VB.NET you can use the following Extension Method

Sample C#

/// <summary>
///     Checks whether the character is an uppercase Character or not
/// </summary>
/// <param name="inputChar"></param>
/// <returns>True or False depending on the character</returns>
public static bool IsUpperCase(this char inputChar)
{
	return inputChar >= 'A' && inputChar <= 'Z';
}

Sample VB.NET

''' <summary>
'''     Checks whether the character is an uppercase Character or not
''' </summary>
''' <param name="inputChar"></param>
''' <returns>True or False depending on the character</returns>
<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsUpperCase(inputChar As Char) As Boolean
	Return inputChar >= "A"C AndAlso inputChar <= "Z"C
End Function




Please rate this snippet

How to scale a Size Object by percentage in C# and VB.NET

To scale a Size Object by percentage in C# and VB.NET you can use the following snippet.

Sample C#

public static Size Scale(this Size size, float scalePercent)
{
	if (Math.Abs(scalePercent - 1) < float.Epsilon)
	{
		return size;
	}

	var height = (int) (size.Height*(scalePercent/100));
	var width = (int)(size.Width * (scalePercent / 100));

	return new Size(width, height);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function Scale(size As Size, scalePercent As Single) As Size
	If Math.Abs(scalePercent - 1) < Single.Epsilon Then
		Return size
	End If

	Dim height = CInt(size.Height * (scalePercent / 100))
	Dim width = CInt(size.Width * (scalePercent / 100))

	Return New Size(width, height)
End Function

Please rate this snippet

How to change the File extension in C# and VB.NET

To change the File extension in C# and VB.NET you can use the following snippet.

Sample C#

public static FileInfo ChangeExtension(FileInfo file, string newExtension)
{
	if (!newExtension.StartsWith("."))
	{
		newExtension = "." + newExtension;
	}
	var fileName = string.Concat(Path.GetFileNameWithoutExtension(file.FullName), newExtension);
	if (File.Exists(fileName))
	{
		File.Delete(fileName);
	}
	File.Move(file.FullName,fileName);
	if (File.Exists(fileName))
	{
		File.Delete(file.FullName);
	}
	return new FileInfo(fileName);
}

Sample VB.NET

Public Shared Function ChangeExtension(file__1 As FileInfo, newExtension As String) As FileInfo
	If Not newExtension.StartsWith(".") Then
		newExtension = Convert.ToString(".") & newExtension
	End If
	Dim fileName = String.Concat(Path.GetFileNameWithoutExtension(file__1.FullName), newExtension)
	If File.Exists(fileName) Then
		File.Delete(fileName)
	End If
	File.Move(file__1.FullName, fileName)
	If File.Exists(fileName) Then
		File.Delete(file__1.FullName)
	End If
	Return New FileInfo(fileName)
End Function

Please rate this snippet

ChangeEncoding extension method for C# and VB.NET

This snippet will give you the ChangeEncoding extension method for C# and VB.NET.

Sample C#

public static string ChangeEncoding(this string input, Encoding encoding)
{
	var bytes = encoding.GetBytes(input);
	return encoding.GetString(bytes);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function ChangeEncoding(input As String, encoding As Encoding) As String
	Dim bytes = encoding.GetBytes(input)
	Return encoding.GetString(bytes)
End Function

Please rate this snippet

OpenIfClosed DBConnection extension method for C# and VB.NET

This snippet will allow you to use the OpenIfClosed DBConnection extension method for C# and VB.NET.

Sample C#

public static void OpenIfClosed(this DbConnection connection)
{
	if (connection!=null && connection.ConnectionString!=String.Empty && connection.State == ConnectionState.Closed)
	{
		connection.Open();
	}
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Sub OpenIfClosed(connection As DbConnection)
	If connection IsNot Nothing AndAlso connection.ConnectionString <> String.Empty AndAlso connection.State = ConnectionState.Closed Then
		connection.Open()
	End If
End Sub

Please rate this snippet