Extension methods are used for the Readability purpose.It's a static method.
In C# we have to create STATIC CLASS but in VB.Net, we have to create a MODULE like below snippets:-
Note:-
1). In C#, we have to give this keyword in the 1st parameter list
2). In VB.Net,we have to define <System.Runtime.CompilerServices.Extension()> _ attribute in function.
//C#
public static class Text_Validation
{
}
//VB.Net
Public Module Text_Validation
End Module
We can understand this by an example:-
//In C#:-
public static class Text_Validation
{
public static string Add_Text(this string str)
{
if(string.IsNullOrEmpty(str))
{
str = "Add Text";
}
return str;
}
}
//In VB.Net:-
Public Module Text_Validation
<System.Runtime.CompilerServices.Extension()> _
Public Function Add_Text(ByVal str As String) As String
If String.IsNullOrEmpty(str) Then
str = "Add Text"
End If
Return str
End Function
End Module
//Our aspx page will look like this:-
<asp:TextBox ID="txt_contents" runat="server"></asp:TextBox>
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
txt_contents.Text = txt_contents.Text.Add_Text()
End Sub
//If the TextBox is empty then it will add "Add Text" in TextBox.