📖 Scenario: Imagine you are building a simple calculator that can add numbers. Sometimes you know the types of numbers at compile time, and sometimes you only know them at runtime. You want to see how using dynamic types affects the speed of your program.
🎯 Goal: You will create two methods: one using static typing and one using dynamic typing. Then you will measure and compare the time each method takes to add numbers many times. This will help you understand the runtime cost of dynamic type resolution.
📋 What You'll Learn
Create a method called AddStatic that takes two int parameters and returns their sum.
Create a method called AddDynamic that takes two dynamic parameters and returns their sum.
Create a loop that calls each method 1,000,000 times and measures the time taken using Stopwatch.
Print the elapsed time for both methods to compare.
💡 Why This Matters
🌍 Real World
Understanding the cost of dynamic typing helps developers write faster programs when performance matters.
💼 Career
Many jobs require optimizing code performance and understanding how language features affect speed.
Progress0 / 4 steps
1
Create the static addition method
Write a method called AddStatic that takes two int parameters named a and b and returns their sum as an int.
C Sharp (C#)
Hint
Define a method with static int AddStatic(int a, int b) and return a + b.
2
Create the dynamic addition method
Add a method called AddDynamic that takes two dynamic parameters named a and b and returns their sum.
C Sharp (C#)
Hint
Define AddDynamic with dynamic parameters and return a + b.
3
Measure time for static addition
Create a Stopwatch called swStatic. Use a for loop from 0 to 999,999 to call AddStatic(i, i). Start and stop the stopwatch around the loop.
C Sharp (C#)
Hint
Use Stopwatch to time the loop calling AddStatic one million times.
4
Measure time for dynamic addition and print results
Create a Stopwatch called swDynamic. Use a for loop from 0 to 999,999 to call AddDynamic(i, i). Start and stop the stopwatch around the loop. Then print the elapsed milliseconds for both swStatic and swDynamic using Console.WriteLine.
C Sharp (C#)
Hint
Use another Stopwatch for AddDynamic. Print both elapsed times with Console.WriteLine.
Practice
(1/5)
1. What does dynamic keyword in C# do at runtime?
easy
A. It performs type checking at compile time
B. It prevents any type errors completely
C. It delays type checking until the program runs
D. It converts all types to strings automatically
Solution
Step 1: Understand the role of dynamic
The dynamic keyword tells C# to skip compile-time type checking for that variable.
Step 2: Identify when type checking happens
Type checking for dynamic variables happens only when the program runs, not before.
Final Answer:
It delays type checking until the program runs -> Option C
Quick Check:
Dynamic = runtime type check [OK]
Hint: Dynamic means check types only when running [OK]
Common Mistakes:
Thinking dynamic checks types at compile time
Believing dynamic prevents all errors
Assuming dynamic converts types automatically
2. Which of the following is the correct syntax to declare a dynamic variable in C#?