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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:16:00+00:00 2026-06-04T08:16:00+00:00

Populating a request object for a web-service, I need to dynamically add items to

  • 0

Populating a request object for a web-service, I need to dynamically add items to some arrays.

I hoped to simplify it by implementing an extension method:

public static class ArrayExtensions<T> where T : class
{
    public static T[] Extend<T>(T[] originalArray, T addItem)
    {
        if (addItem == null)
        {
            throw new ArgumentNullException("addItem");
        }
        var arr = new[] { addItem };
        if (originalArray == null)
        {
            return arr;
        }
        return originalArray.Concat(arr).ToArray();
    }
}

So that this old code:

if (foo.bazArr == null)
{
    foo.bazArr  = new[] { baz };
}
else
{
    foo.bazArr = new[] { baz }.Concat(foo.bazArr).ToArray(); // (i know this inserts the new item at the beginning, but that's irrelevant, order doesn't matter)
}

could be rewritten as:

foo.bazArr = foo.bazArr.Extend(baz); // won't compile

The error is: 'System.Array' does not contain a definition for 'Extend' and no extension method 'Extend' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

Whereas calling the extension method directly like so:

foo.bazArr = ArrayExtensions<someService.bazType>.Extend(foo.bazArr, baz);

compiles fine.

Why is that so? Why can’t the compiler infer the type on its own here, if the array is strongly-typed?


EDIT – correct code below:

public static class ArrayExtensions
{
    public static T[] Extend<T>(this T[] originalArray, T addItem) where T : class
    {
        if (addItem == null)
        {
            throw new ArgumentNullException("addItem");
        }
        var arr = new[] { addItem };
        if (originalArray == null)
        {
            return arr;
        }
        return originalArray.Concat(arr).ToArray(); // although Concat is not recommended for performance reasons, see the accepted answer
    }
}

For this popular question, here’s another good simple example:

public static class Extns
    {
    // here's an unbelievably useful array handling extension for games!

    public static T AnyOne<T>(this T[] ra) where T:class
        {
        int k = ra.Length;
        int r = Random.Range(0,k);
        return ra[r];
        // (add your own check, alerts, etc, to this example code)
        }
    }

and in use ..

someArrayOfSoundEffects.AnyOne().Play();
someArrayOfAnimations.AnyOne().BlendLeft();
winningDisplay.text = successStringsArray.AnyOne() +", " +playerName;
SpawnEnormousRobotAt( possibleSafeLocations.AnyOne() );

and so on. For any array it will give you one random item. Used constantly in games to randomise effects etc. The array can be any type.

  • 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-04T08:16:02+00:00Added an answer on June 4, 2026 at 8:16 am

    Missing this:

    public static T[] Extend<T>(this T[] originalArray, T addItem)
    

    Without the this it is not an extension method.

    Additional note: extending an array one item at a time is expensive. A List<T> would be far preferable. Check to see if your web-service tools offer lists as an option.

    Even with arrays, using Enumerable.Concat is probably overkill here; I would simply measure the two arrays, allocate a new one, and use the CopyTo method of each to write into place in the new array.

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

Sidebar

Related Questions

I'm populating a ListView with items and add images from an ImageList (in the
I'm having some weird issues with dynamically populating a table in Javascript. Behold the
im populating the data in the combo box using dataset tables...with some table name..now
When populating a textarea with special characters from a jQuery GET request, the special
I have a SELECT object and I'm populating it with AJAX(JQUERY). $(#setor).change(function(){ $(#loading).css(display, inline-block);
Possible Duplicate: Easy way of populating Javabeans based on request parameters Hi, I have
Using Java 1.6 wsimport I generated source from a WSDL for a web service.
I have a tree that gets populated through the web service - this part
Hi I have an MVC textbox I'm populating like: <p>Find: @Html.TextBox(SearchString, Request.Cookies[query].Value as string,
I'm populating a Repeater Control dynamically based on a querystring value. On Page_Load, 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.