Closure on Loop Variables — C# and JavaScript Comparison

Closure on Loop Variables — C# and JavaScript Comparison

Subtle differences with closure on loop variables in different programming languages (C# and JavaScript as examples). Closure is not a new concept. It was first introduced in the 1970s and is now available in most of the programming languages. I am sure most developers are familiar with it and use it on a regular basis. Here are some quick tests to check our understandings — can you tell the output for the four cases below in JavaScript and C#.

Subtle differences with closure on loop variables in different programming languages (C## and JavaScript as examples)

Closure is not a new concept. It was first introduced in the 1970s and is now available in most of the programming languages. I am sure most developers are familiar with it and use it on a regular basis. Here are some quick tests to check our understandings — can you tell the output for the four cases below in JavaScript and C#:

TypeScript (plain JavaScript if you ignore the array typing):

const list = [0, 1, 2, 3];
const funcList: (() => number)[] = [];
const funcList2: (() => number)[] = [];

for (let i = 0; i < list.length; i++) {
  funcList.push(() => list[i]);
}
funcList.map(f => console.log(f()));   // case#1
for (var i = 0; i < list.length; i++) {
  funcList2.push(() => list[i]);
}
funcList2.map(f => console.log(f()));  // case#2

C#:

var list = new int[] { 0, 1, 2, 3 };
var funcList = new List<Func<int>>();
var funcList2 = new List<Func<int>>();

foreach (var item in list)
{
    funcList.Add(() => item);
}
funcList.ForEach(f => Console.WriteLine(f()));   // case#3
for (var i = 0; i < list.Length; i++)
{
    funcList2.Add(() => list[i]);
}
funcList2.ForEach(f => Console.WriteLine(f()));  // case#4

At first glance it might seem like we should be getting below for all four cases across these two languages:

0
1
2
3

Here is the actual output:

Case#1: logs above output as expected

Case#2: logs 4 “undefined”

Case#3: prints above output as expected

Case#4: IndexOutOfRangeException

c-sharp-programming javascript closure c# c-sharp

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

C programming for Beginners | Learn C Programming | C Language

C Language is an evergreen language and is used widely across different industries, This C programming is a must for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. Great Learning brings you this live session on "Introduction to C". In this live session, we will be covering major concepts in C Programming such as Different Variables, Different Data Types that are being used, its Operators, Flow control statements, Structure, and lot more.

5 Best C# (C-Sharp) Programming Courses for Beginners in 2020

In this article, I am going to share some of the best courses to learn C# or C-Sharp in 2020. These courses are suitable for both beginners and experienced programmers who want to fill gaps in their C# knowledge.

Object Oriented Programming in C++ | C++ OOPs Concepts | Learn Object Oriented C++

C++ is general purpose, compiled, object-oriented programming language and its concepts served as the basis for several other languages such as Java, Python, Ruby, Perl etc.

Classes and Objects in C++ | C++ Programming Language - Part2

This Edureka video Classes and Objects in C++ will help you to get started with C++.

Introduction to C++ Programming Language | C++ Tutorial for Beginners - Part 1

This Edureka video on Introduction to C++ Programming will help you to get started with C++.