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

Runtime cost of dynamic type resolution in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dynamic Type Resolution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dynamic vs static method call

What is the output of this C# code snippet?

C Sharp (C#)
using System;
class Program {
    static void Main() {
        dynamic d = "hello";
        string s = "hello";
        Console.WriteLine(d.ToUpper());
        Console.WriteLine(s.ToUpper());
    }
}
AHELLO\nhello
Bhello\nhello
CHELLO\nHELLO
Dhello\nHELLO
Attempts:
2 left
💡 Hint

Both dynamic and static strings call the same method, but dynamic resolution happens at runtime.

Predict Output
intermediate
2:00remaining
Performance difference in repeated dynamic calls

Consider this code measuring elapsed time for 1 million calls. What is the expected output pattern?

C Sharp (C#)
using System;
using System.Diagnostics;
class Program {
    static void Main() {
        dynamic d = "test";
        string s = "test";
        Stopwatch sw = new Stopwatch();

        sw.Start();
        for (int i = 0; i < 1000000; i++) {
            var x = d.ToUpper();
        }
        sw.Stop();
        Console.WriteLine("Dynamic: " + sw.ElapsedMilliseconds);

        sw.Restart();
        for (int i = 0; i < 1000000; i++) {
            var y = s.ToUpper();
        }
        sw.Stop();
        Console.WriteLine("Static: " + sw.ElapsedMilliseconds);
    }
}
ACode throws runtime exception
BDynamic time < Static time
CDynamic time == Static time
DDynamic time > Static time
Attempts:
2 left
💡 Hint

Dynamic calls resolve method at runtime, adding overhead.

🔧 Debug
advanced
2:00remaining
Identify the runtime error in dynamic invocation

What error does this code produce when run?

C Sharp (C#)
using System;
class Program {
    static void Main() {
        dynamic d = 10;
        Console.WriteLine(d.ToUpper());
    }
}
ARuntimeBinderException: 'int' does not contain a definition for 'ToUpper'
BNullReferenceException
CNo error, prints 10
DSyntaxError
Attempts:
2 left
💡 Hint

Check if the type supports the called method.

🧠 Conceptual
advanced
2:00remaining
Why does dynamic type resolution add runtime cost?

Which explanation best describes why dynamic type resolution in C# adds runtime cost?

ABecause the compiler generates extra IL code to check types at runtime and invoke methods dynamically
BBecause dynamic variables use more memory than static variables
CBecause dynamic variables require manual type casting by the programmer
DBecause dynamic variables are compiled to slower machine code
Attempts:
2 left
💡 Hint

Think about what happens behind the scenes when you use dynamic.

Predict Output
expert
2:00remaining
Output of mixed dynamic and static method calls with inheritance

What is the output of this C# program?

C Sharp (C#)
using System;
class Base {
    public virtual string GetName() => "Base";
}
class Derived : Base {
    public override string GetName() => "Derived";
}
class Program {
    static void Main() {
        Base b = new Derived();
        dynamic d = new Derived();
        Console.WriteLine(b.GetName());
        Console.WriteLine(d.GetName());
    }
}
ABase\nDerived
BDerived\nDerived
CDerived\nBase
DBase\nBase
Attempts:
2 left
💡 Hint

Remember how virtual methods behave with static and dynamic calls.