Imports System
Imports System.Collections.Generic
' Define a class representing a fruit
Public Class FruitClass
' Property to store the name of the fruit
Public Property Name As String
' Constructor to initialize the FruitClass object with a name
Public Sub New(name As String)
Me.Name = name
End Sub
' Override the ToString method to return the name of the fruit
Public Overrides Function ToString() As String
Return Name
End Function
End Class
' Main module where program execution begins
Module Program
Sub Main()
' Create a HashSet collection to store unique FruitClass objects
' The HashSet ensures that duplicate fruits (based on their property values) are not added
Dim fruits As New HashSet(Of FruitClass) From {
New FruitClass("Apple"), ' Create a FruitClass object with the name "Apple"
New FruitClass("Banana"), ' Create a FruitClass object with the name "Banana"
New FruitClass("Strawberry") ' Create a FruitClass object with the name "Strawberry"
}
' Iterate through the HashSet and print the name of each fruit
For Each fruit In fruits
Console.WriteLine(fruit) ' Print the fruit object, which calls the overridden ToString method
Next
End Sub
End Module
' run:
'
' Apple
' Banana
' Strawberry
'