Click here to Skip to main content
15,892,072 members
Articles / Programming Languages / Visual Basic 10

A Look under the hood of the .NET Framework

Rate me:
Please Sign up or sign in to vote.
4.95/5 (78 votes)
11 Feb 2013CPOL49 min read 187.7K   1.5K   166  
Journey to the center of the .NET Framework with a chance of IL along the way!
Namespace ForEachExamples
	Public NotInheritable Class ForEachExamples

		Private Sub New()
		End Sub

		Public Shared Sub ForEach(ByVal l As IEnumerable, ByVal handler As Action(Of Object))
			For Each obj As Object In l
				handler.Invoke(obj)
			Next
		End Sub

		Public Shared Sub ForEachRewritten(ByVal l As IEnumerable, ByVal handler As Action(Of Object))
			Dim e As IEnumerator
			Try
				Dim obj As Object
				e = l.GetEnumerator
				Do While e.MoveNext
					obj = e.Current
					handler.Invoke(obj)
				Loop
			Finally
				If TryCast(e, IDisposable) IsNot Nothing Then
					DirectCast(e, IDisposable).Dispose()
				End If
			End Try
		End Sub

		Public Shared Sub GenericForEach(Of T)(ByVal l As IEnumerable(Of T), ByVal handler As Action(Of T))
			For Each obj As T In l
				handler.Invoke(obj)
			Next
		End Sub

		Public Shared Sub GenericForEachRewritten(Of T)(ByVal l As IEnumerable(Of T), ByVal handler As Action(Of T))
			Dim e As IEnumerator(Of T)
			Try
				Dim obj As T
				e = l.GetEnumerator
				Do While e.MoveNext
					obj = e.Current
					handler.Invoke(obj)
				Loop
			Finally
				If e IsNot Nothing Then
					e.Dispose()
				End If
			End Try
		End Sub

	End Class
End Namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO JUUN Software
Netherlands Netherlands
Sander Rossel is a Microsoft certified professional developer with experience and expertise in .NET and .NET Core (C#, ASP.NET, and Entity Framework), SQL Server, Azure, Azure DevOps, JavaScript, MongoDB, and other technologies.

He is the owner of JUUN Software, a company specializing in custom software. JUUN Software uses modern, but proven technologies, such as .NET Core, Azure and Azure DevOps.

You can't miss his books on Amazon and his free e-books on Syncfusion!

He wrote a JavaScript LINQ library, arrgh.js (works in IE8+, Edge, Firefox, Chrome, and probably everything else).

Check out his prize-winning articles on CodeProject as well!

Comments and Discussions