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

  • Home
  • SEARCH
  • 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 8940331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:00:33+00:00 2026-06-15T11:00:33+00:00

I have this extension method: public static bool In<T>(this T source, params T[] list)

  • 0

I have this extension method:

public static bool In<T>(this T source, params T[] list)
{
    return list.Contains(source);
}

Now I need to use the above method for a ushort. When I try

ushort p = 3;
if (p.In(1, 2, 3, 4, 5))
   return;

The first line casts 3 to a ushort well. But when 3 is passed as a parameter, I get the error

‘ushort’ does not contain a definition for ‘In’ and the best extension method overload ‘Extensions.In(T, params T[])’ has some invalid arguments.

But this works:

ushort p = 3;
if (Extensions.In(p, 1, 2, 3, 4, 5))
   return;

which is weird.

  1. Why does it work with the second example, but not the first?

  2. What’s a good alternative that would help me here? Since there are no literals for short or ushort I’m failing to find a simpler alternative than manually casting each integer like this:

    ushort p = 3;
    if (p.In((ushort)1, (ushort)2, (ushort)3, (ushort)4, (ushort)5))
       return;
    
  • 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-15T11:00:35+00:00Added an answer on June 15, 2026 at 11:00 am

    Why does it work with second example, but not first?

    First, let’s work out what the compiler infers T to be. Some parameters are ushort and some are int. ushort has an implicit conversion to int and int does not have an implicit conversion to ushort, so T is int.

    The key here is in section 7.6.5.2 of the C# 4 specification (emphasis mine):

    An extension method Ci.Mj is eligible if:

    • Ci is a non-generic, non-nested class
    • The name of Mj is identifier
    • Mj is accessible and applicable when applied to the arguments as a static method as shown above
    • An implicit identity, reference or boxing conversion exists from expr to the type of the first parameter of Mj.

    There is an implicit conversion from ushort to int, but not an identity, reference, or boxing conversion!

    So, the following are legal:

    Extensions.In<ushort>(p, 1, 2, 3, 4, 5);
    Extensions.In<int>(p, 1, 2, 3, 4, 5); // implicit conversion allowed
    Extensions.In(p, 1, 2, 3, 4, 5); // T is int
    p.In<ushort>(1, 2, 3, 4, 5);
    

    but the following are not:

    p.In<int>(1, 2, 3, 4, 5); // implicit conversion not allowed
    p.In(1, 2, 3, 4, 5); // T is int
    

    Note that you can get the same error without generics if you define In as

    public static bool In(this int source, params int[] list)
    {
        return list.Contains(source);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Extension Method public static bool Between<T1, T2>(this T1 Foo, T2 bar)
I created the following extension method: public static bool HasHostAndUrl(this HttpSessionStateBase session) { return
I have created an extension method as such.. public static bool AllFlagsSet<T>(this T input,
Let's assume I have a method like this: public static List<T> Get<T>(this SomeObject<T>, Expressions<Func<T,bool>>
I have an extension method as follows: public static bool SatisfiesSomeCondition(this Post post, SomeObj
I have the following extension method public static bool IsValidCurrency(this string value) { CultureInfo
I am using Ladislav Mrnka's extension method: public static IQueryable<T> IncludeMultiple<T>(this IQueryable<T> query, params
Lets say I have this extention method: public static bool HasFive<T>(this IEnumerable<T> subjects) {
I have extension method: public static IQueryable<TResult> WithFieldLike<TResult>( this IQueryable<TResult> query, Func<TResult, string> field,
I have attempted to create an extension method that looks like this... public static

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.