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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:44:22+00:00 2026-05-16T07:44:22+00:00

I need to create an extension method to array class, but this extension method

  • 0

I need to create an extension method to array class, but this extension method must be able to accept many data types, so it also must be generic.

In the code bellow the extension method just accept byte data type. I want it to also accept ushort and uint for instance.
I believe that the best way to do that is creating a generic type here. But how can I do that using arrays?

Thanks!!!

public static class MyExtensions
{
    public static int GetLastIndex(this byte[] buffer)
    {
        return buffer.GetUpperBound(0);
    }
}
  • 1 1 Answer
  • 1 View
  • 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-16T07:44:22+00:00Added an answer on May 16, 2026 at 7:44 am

    Generics in extension methods aren’t really anything special, they behave just like in normal methods.

    public static int GetLastIndex<T>(this T[] buffer)
    {
        return buffer.GetUpperBound(0);
    }
    

    As per your comment, you could do something like the following to effectively restrict the type of T (adding guard statements).

    public static int GetLastIndex<T>(this T[] buffer) where T : struct
    {
        if (!(buffer is byte[] || buffer is ushort[] || buffer is uint[]))
            throw new InvalidOperationException(
                "This method does not accept the given array type.");
    
        return buffer.GetUpperBound(0);
    }
    

    Note: As Martin Harris pointed out in a comment, you don’t actually need to use generics here. The Array type from which all arrays derive will suffice.

    If you want a more elegant solution, at the cost of slightly more code, you could just create overloads of the method:

    public static int GetLastIndex(this byte[] buffer)
    {
        return GetLastIndex(buffer);
    }
    
    public static int GetLastIndex(this ushort[] buffer)
    {
        return GetLastIndex(buffer);
    }
    
    public static int GetLastIndex(this uint[] buffer)
    {
        return GetLastIndex(buffer);
    }
    
    private static int GetLastIndex(Array buffer)
    {
        return buffer.GetUpperBound(0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may seem a bit odd, but I really need to create a workaround
I need to create an extension method for a GraphicsPath object that determines if
Background : I need to create a custom like extension method for linqtohiberante to
I need a way to create an extension method off of an IEnumerable that
I'm trying to create a generic extension method, that works on typed data tables
i need create an email list sending to many emails. what is best solution
I need create clone repository. but I do not know where can I get
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I am writing a Firefox extension. Right now I need to create a local
I have some problem to implement twig extension. I need to create my own

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.