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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:15:55+00:00 2026-05-30T14:15:55+00:00

So I setup C# parameter validation using the method described in this blog post.

  • 0

So I setup C# parameter validation using the method described in this blog post. Everything works great for strings for which I have this extension method setup at the base of it all:

public static ArgumentEx<T> NotEmptyString<T>(this ArgumentEx<T> arg) where T : class
{
    if (string.IsNullOrWhiteSpace(arg.Value.ToString()))
    {
        throw new ArgumentException(string.Format("{0} cannot be empty.", arg.Name));
    }
    return arg;
}

However, when I try and add an extension method for ints to determine if they are less than or equal to zero such as this:

public static ArgumentEx<T> NotZeroOrLess<T>(this ArgumentEx<T> arg) where T : class
{
    if (Convert.ToInt32(arg.Value.ToString()) <= 0)
    {
        throw new ArgumentException(string.Format("Please make sure {0} is greater than zero.", arg.Name));
    }
    return arg;
}

I get this error when I try and call it like so userId.RequireThat("userId").NotZeroOrLess();

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

Like I said, doing name.RequireThat("name").NotEmptyString(); works fine, as name is a string. I’ve tried specifying the type for the generic like so:

userId.RequireThat<int>("userId").NotZeroOrLess();

But I get the same error. I have to admit, I’m not too well versed with generics. Can someone explain why this error is occurring? Thanks ahead of time.

  • 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-30T14:15:57+00:00Added an answer on May 30, 2026 at 2:15 pm

    Have you tried to remove where T : class from your extension method public static ArgumentEx<T> NotZeroOrLess<T>(this ArgumentEx<T> arg) where T : class?

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

Sidebar

Related Questions

I have this setup that works perfectly when using http. A silverlight 3 client
I have a form in which I am using remote validation to check if
I'm trying to setup an output parameter using PetaPoco. I found someone using this
I'm looking to setup a custom route which supplies implicit parameter names to a
i currently have a Java Observer/Observable setup in which i switch on some field
I would like to have this setup in Tomcat: virtual host webapp resides directly
I have a route setup for downloading a document which I need a named
Is there a way to setup a Moq Mock using a string parameter instead
Setup Have you ever had the experience of going into a piece of code
Setup I have a website that draws RSS feeds and displays them on the

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.