Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9239879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:05:36+00:00 2026-06-18T08:05:36+00:00

Given int foo = 1; Type unboundType = typeof(List<>); Type w = unboundType.MakeGenericType(typeof(int)); if

  • 0

Given

        int foo = 1;
        Type unboundType = typeof(List<>);
        Type w = unboundType.MakeGenericType(typeof(int));

        if (w == typeof(List<int>))
        {
            Console.WriteLine("Yes its a List<int>");

            try
            {
                ((List<int>)(object)w).Add(foo);
            }
            catch(InvalidCastException)
            {
                Console.WriteLine("No you can't cast Type");
            }
        }   

I can verify that the type indeed matches a constructed type and perform an action based on said constructed type. However, I cannot cast Type to it’s class using as or an explicit cast. Is there a practical purpose for allowing developers to create a Type of unbound type or does this functionality exist solely to support the language in some way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:05:36+00:00Added an answer on June 18, 2026 at 8:05 am

    Not everything can be done at compile time. Sometimes, particularly in library code, you need to take what you are given. In scenarios where you are given just an object or a Type and need to do some clever processing, unbound types can be really helpful; for example:

    object obj = NoIdeaWhatThisReturns();
    IList list = (IList)Activator.CreateInstance(
                     typeof(List<>).MakeGenericType(obj.GetType()));
    list.Add(obj);
    

    Basically; scenarios that use a lot of reflection or meta-programming will probably find themselves using unbound types at some point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a ParameterInfo p from this: void foo(int modopt(IsLong) n); p.GetOptionalCustomModifiers() returns a System.Runtime.CompilerServices.IsLong;
Given this class: class C { private: struct Foo { int key1, key2, value;
Given: template<class T> struct test { void foo(int b); void testFunc( int a )
Given the following code: void foo( int* array ) { // ... } void
Given is the following example: class Foo(object): def __init__(self, value=0): self.value=value def __int__(self): return
Given the following C source code: const int foo(void) { return 42; } gcc
I'd like to recover type information using reflection. I have public Foo(object coll, string
Minimal program: #include <stdio.h> #include <type_traits> template<typename S, typename T> int foo(typename T::type s)
given public Class Example { public static void Foo< T>(int ID){} public static void
Given int a; , I know that the following returns the largest value that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.