C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: These two classes, Content and Image, implement IValue.Render in different ways. The Implements keyword is used.
Main: A new Dictionary is created and three String keys are added. They point to two Image class instances and one Content instance.
Then: The TryGetValue function is invoked twice. When the IValue is retrieved from the Dictionary, the Render sub is called.
DictionaryResult: The specialized implementation being called from the general Interface type.
VB.NET program that uses Interface
Interface IValue
Sub Render()
End Interface
Class Content : Implements IValue
Public Sub Render() Implements IValue.Render
Console.WriteLine("Content.Render")
End Sub
End Class
Class Image : Implements IValue
Public Sub Render() Implements IValue.Render
Console.WriteLine("Image.Render")
End Sub
End Class
Module Module1
Sub Main()
' Create dictionary of Interface type instances.
Dim dict As Dictionary(Of String, IValue) = New Dictionary(Of String, IValue)
dict.Add("cat1.png", New Image())
dict.Add("image1.png", New Image())
dict.Add("home.html", New Content())
' Get value from Dictionary and call their Render methods.
Dim value As IValue = Nothing
If dict.TryGetValue("cat1.png", value) Then
value.Render()
End If
' Again.
If dict.TryGetValue("home.html", value) Then
value.Render()
End If
End Sub
End Module
Output
Image.Render
Content.Render
Size: Here we implement a Size property from the IWidget interface. We can set, and read, Size from an IWidget reference.
VB.NET program that uses Property on interface
Interface IWidget
Property Size As Integer
End Interface
Class Widget
Implements IWidget
''' <summary>
''' Size property implementation.
''' </summary>
Property Size As Integer Implements IWidget.Size
End Class
Module Module1
Sub Main()
' Use Size Property on Interface.
Dim w As IWidget = New Widget
w.Size = 10
Console.WriteLine(w.Size)
End Sub
End Module
Output
10
Next: Consider this program. It uses just one method, DisplaySize, to handle both arrays and Lists.
ArrayListImportant: With the interface, we avoid duplicating our code for each type (array, List). This makes programs easier to maintain.
VB.NET program that uses IEnumerable interface
Module Module1
Sub Main()
' Use String array as IEnumerable.
Dim values() As String = {"cat", "dog", "mouse"}
DisplaySize(values)
' Use List of Strings as IEnumerable.
Dim valuesList As List(Of String) = New List(Of String)
valuesList.Add("cat")
DisplaySize(valuesList)
End Sub
Sub DisplaySize(ByVal col As IEnumerable(Of String))
' Display count from interface.
Console.WriteLine(col.Count())
End Sub
End Module
Output
3
1
Version 1: We call an Interface Function. It is specified by the IWidget Interface. We call it through an Interface reference.
Version 2: We call a normal Class Function. It is specified directly on the Widget class.
Result: In a Release build, the program that uses an Interface reference to call GetName is slower. GetNameDirect is faster.
VB.NET program that benchmarks Interface Function
Interface IWidget
Function GetName(ByVal size As Integer) As String
End Interface
Class Widget
Implements IWidget
Public Function GetName(size As Integer) As String Implements IWidget.GetName
' An interface method.
If size = 0 Then
Return "Zero"
End If
Return "Unknown"
End Function
Public Function GetNameDirect(size As Integer) As String
' A class method.
If size = 0 Then
Return "Zero"
End If
Return "Unknown"
End Function
End Class
Module Module1
Sub Main()
Dim widget As Widget = New Widget
Dim widget2 As IWidget = widget
Dim m As Integer = 100000000
' Version 1: use Interface method.
Dim s1 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
widget2.GetName(i)
Next
s1.Stop()
' Version 2: use Class method.
Dim s2 As Stopwatch = Stopwatch.StartNew
For i As Integer = 0 To m - 1
widget.GetNameDirect(i)
Next
s2.Stop()
Dim u As Integer = 1000000
Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) /
m).ToString("0.00 ns"))
Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) /
m).ToString("0.00 ns"))
End Sub
End Module
Output
3.82 ns, GetName (Interface Function)
0.95 ns, GetNameDirect (no Interface)