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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:11:15+00:00 2026-05-15T04:11:15+00:00

There is the next task: I need to check if input parameter (object) is

  • 0

There is the next task:
I need to check if input parameter (object) is array, then return array of input elements. For example I have input array like this:

int [] array = {1,2,3,4};

And method

  private object[] GetArray(object @from)
  {
  }

So, I need to check in this method that input variable is array and after return this array.
For checking I use

if(@from.GetType().IsArray)

And how to create array from object ??? Is it possible ?
Thanks.

  • 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-15T04:11:16+00:00Added an answer on May 15, 2026 at 4:11 am

    If what you want to do is return @from as an object[] — if it is already an object[] — then the simplest way is just:

    private object[] GetArray(object @from)
    {
        return @from as object[] ?? new object[] { @from };
    }
    

    The above might look kind of confusing. Here’s how it works:

    1. If @from is an object[] to begin with, it just returns that (typed as such).
    2. Other wise, the expression @from as object[] evaluates to null. In this case, the null-coalescing operator (??) evaluates the following expression: new object[] { @from }.

    So the result is that this method returns either the already existing object[] array, or an array of length 1 containing @from.

    On the other hand, if you want to populate an object[] from the contents of @from, I’d do this:

    private object[] GetArray(object @from)
    {
        var objects = @from as IEnumerable;
        if (objects != null)
            return objects.Cast<object>().ToArray();
    
        return new object[] { @from };
    }
    

    As LukeH pointed out, you could also check to make sure @from is not a string, if you don’t want GetArray(string) to return an object[] containing char elements:

    private object[] GetArray(object @from)
    {
        var str = @from as string;
        if (str != null)
            return new object[] { str };
    
        var objects = @from as IEnumerable;
        if (objects != null)
            return objects.Cast<object>().ToArray();
    
        return new object[] { @from };
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have next task: I need to load the same file into my web
Is there a way to run PHP 5.3 next PHP 5.4? I'd like it
Is there any event associated with Install Components Screen? Something like Next button disabled
I have the next task. Let's assume that we have below tables in a
For my next task I need to use a very big hash; since I
I have csv input file, lets call it heights.csv . my example file has
I have a task to play with Java Collections framework. I need to obtain
Next to the addBatch() method of PreparedStatement there is also an addBatch(String) method in
Is there any information available for the next version ( 2011 ) of Delphi
Is there a way of doing the old on error resume next routine in

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.