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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:50:13+00:00 2026-05-22T22:50:13+00:00

I have a generic method with this signature: private void MyGenericMethod<T>(T arg) where T

  • 0

I have a generic method with this signature:

private void MyGenericMethod<T>(T arg) where T : class
{}

If I pass, say, an integer to this method, I get an ArgumentException that the passed value does not match the restriction. This is great, but how can I predetermine that what I am passing will match the “class” constraint so that the exception is not thrown?

  • 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-22T22:50:14+00:00Added an answer on May 22, 2026 at 10:50 pm

    The compiler will already do that for you – you should actually see:

    The type ‘int’ must be a reference type in order to use it as parameter ‘T’ in the generic type or method ‘blah.MyGenericMethod(T)’

    at compile-time.

    The tricky scenarios are:

    • generics upon generics upon generics – all those type-constraints stack, so you end up with where T : class a lot. Sometimes it is better to use runtime validation against T
    • reflection (MakeGenericMethod, etc) – again, just check at runtime

    Also, note that where T : class doesn’t actually mean T is a class – it means it is a reference-type, which can include interfaces and delegates. Likewise, where T : struct doesn’t actually mean T is a struct – it means it is a struct that is not Nullable<>.

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

Sidebar

Related Questions

I have a generic method with this (dummy) code (yes I'm aware IList has
If I have a generic class like this: public class Repository<T> { public string
Using SubSonic3, I have this generic method (thanks to linq guy, James Curran): public
Let's say I have the following method: public static int CountNonNullMembers<T>(this IEnumerable<T> enumerable) {
There is a generic method that takes a class as parameter and I have
I have a class Exporter which has a generic method which accepts an IEnumerable<T>
I have a generic class that has a method 'Call' on it with a
What would I do if I want to have a generic method that only
I have a 'generic' boiler plate static method for checking for InvokeRequired and invoking
I have a method to return a group of objects as a generic list

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.