Lambda with captures (closures) in C#
📖 Scenario: Imagine you are creating a simple calculator that can remember a number and add it to other numbers later.
🎯 Goal: You will build a small program that uses a lambda function to remember a number (capture it) and add it to new numbers when called.
📋 What You'll Learn
Create an integer variable called
baseNumber with the value 10Create a lambda function called
addBase that takes an integer parameter x and returns the sum of baseNumber and xCall the lambda function
addBase with the value 5 and store the result in a variable called resultPrint the value of
result💡 Why This Matters
🌍 Real World
Closures like this are useful when you want to create small functions that remember some data without needing to pass it every time.
💼 Career
Understanding closures helps in writing clean, reusable code in many programming jobs, especially when working with events, callbacks, or functional programming styles.
Progress0 / 4 steps