0
0
C Sharp (C#)programming~30 mins

Runtime cost of dynamic type resolution in C Sharp (C#) - Mini Project: Build & Apply

Choose your learning style9 modes available
Runtime Cost of Dynamic Type Resolution
📖 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#)
Need a 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#)
Need a 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#)
Need a 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#)
Need a hint?

Use another Stopwatch for AddDynamic. Print both elapsed times with Console.WriteLine.