Runtime & concurrency
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Reflection;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
Type closed = typeof(Dictionary<string, int>);
Type definition = closed.GetGenericTypeDefinition();
Console.WriteLine($"constructed:{closed.IsConstructedGenericType}");
Console.WriteLine($"definition:{definition == typeof(Dictionary<,>)}");
Console.WriteLine($"arguments:{string.Join(",", closed.GetGenericArguments().Select(type => type.Name))}");
Console.WriteLine($"open:{definition.ContainsGenericParameters}");
Reveal answer and reasoning
The output is constructed:True, definition:True, arguments:String,Int32, and open:True. The selected Type is Dictionary with string in the key position and integer in the value position, so it is a constructed generic type. Calling GetGenericTypeDefinition returns the same definition represented by the unbound Dictionary syntax. The arguments retain their declared positions rather than being sorted alphabetically for display.
ContainsGenericParameters is checked on that definition, not on the original closed construction, which explains the final True. Reversing key and value would produce a different closed Type and reversed argument names while preserving the same definition relationship. The common wrong trace calls the final value False because it forgets which Type variable is inspected. The program neither creates a dictionary object nor validates arbitrary runtime-selected arguments, and it supplies no evidence that an AOT artifact contains every possible construction.
What a strong answer covers
- The argument list is positional String then Int32.
- The definition comparison is true.
- The final query is on an open definition.
Common pitfall
Applying the final ContainsGenericParameters query to the wrong Type.
Follow-up questions
- What changes for Dictionary<int,string>?
- Which query must be guarded for a nongeneric type?
Self-review
Choose what to revisit. These marks are your own assessment and are separate from lesson completion.