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 783209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:30:40+00:00 2026-05-14T20:30:40+00:00

I have a generic function that is constrained to struct. My inputs are boxed

  • 0

I have a generic function that is constrained to struct. My inputs are boxed (“objects”). Is it possible to unbox the value at runtime to avoid having to check for each possible type and do the casts manually?

See the above example:

   public struct MyStruct
    {
        public int Value;
    }

    public void Foo<T>(T test)
        where T : struct
    {
        // do stuff
    }

    public void TestFunc()
    {
        object o = new MyStruct() { Value = 100 }; // o is always a value type

        Foo(o);
    }

In the example, I know that o must be a struct (however, it does not need to be MyStruct …). Is there a way to call Foo without tons of boilerplate code to check for every possible struct type?

Thank you.

  • 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-05-14T20:30:41+00:00Added an answer on May 14, 2026 at 8:30 pm

    .NET Generics are implemented in a manner that allows value types as a generic type parameter without incurring any boxing/unboxing overhead. Because your’re casting to object before calling Foo you don’t take advantage of that, in fact you’re not even taking advantage of generics at all.

    The whole point of using generics in the first place is to replace the “object-idiom”. I think you’re missing the concept here.
    Whatever type T happens to be, it is available at run-time and because you constrained it to struct guaranteed to be a struct type.

    Your TestFunc could be written like this without problem:

    public void TestFunc()
    {
        MyStruct o = new MyStruct() { Value = 100 }; // o is always a value type
    
        Foo<MyStruct>(o);
    }
    

    Looking at Foo, it would look like this in your example:

    public void Foo<T>(T test)
        where T : struct
    {
        T copy = test; // T == MyStruct
    }
    

    EDIT:

    Ok, since the OP clarified what he wants to call the generic method but doesn’t know the type of his struct (it’s just object). The easiest way to call your generic method with the correct type parameter is to use a little reflection.

    public void TestFunc()
    {
        object o = new DateTime();
    
        MethodInfo method = this.GetType().GetMethod("Foo");
        MethodInfo generic = method.MakeGenericMethod(o.GetType());
        generic.Invoke(this, new object[] {o});
    
    
    }
    public void Foo<T>(T test)
        where T : struct
    {
        T copy = test; // T == DateTime
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service which has a generic function that returns a dataset
I have a generic class that should allow any type, primitive or otherwise. The
I have a generic class that I'm trying to implement implicit type casting for.
I have a generic list of objects in C#, and wish to clone the
I have a generic list of objects in C#, for example sake, here's what
I'd like do implement a generic function with the generic constraint that the Type
I want to create generic function that will need only parameter as Stored procedure
I have a generic function and I want to check whether the type parameter
I am attempting to create a generic mapping function that takes the values of
I have a function that uses reflection to set properties of object A from

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.