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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:16:13+00:00 2026-06-16T12:16:13+00:00

summary Ignore the case that an object cannot represented as an array, is it

  • 0
  • summary

    Ignore the case that an object cannot represented as an array, is it possible to define an extension(static) method like:

    public static ? ToArray<TSource>(this TSource source); 
    

    and returns the array of an object if it consists of whatever a sequence of elements? And if yes, what would the ? be?

  • explanation

    I’ve thought about the following declarations:

    public static TElement[] ToArray<TElement>(this IEnumerable<TElement> source); 
    public static ? ToArray<TSource>(this IEnumerable source); 
    

    But I cannot assume an unknown class must have implemented IEnumerable<T> or IEnumerable. I cannot even define the ? in the case it is just IEnumerable which is out of generic definition.

    And I’ve also thought about the Array class:

    public static Array ToArray<TSource>(this TSource source); 
    

    But that means the element type was unknown at compile time.

    So I’m wondering is it possible to know the element type at compile time without a class implementing IEnumerable<T>?

  • 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-16T12:16:14+00:00Added an answer on June 16, 2026 at 12:16 pm

    Unfortunately, IEnumerator predates generics so it carries with it no type information; just a IEnumerator GetEnumerator() making String not so appealing to try and force into this generic implementation you’re trying to accomplish.

    It’s been a long time since I’ve done C# but I imagine:

    public static TElement ToArray<TSource>(this IEnumerable<TElement> source);
    

    … should be fine as:

    public static TElement[] ToArray<TElement>(this IEnumerable<TElement> source);
    

    The important thing to keep in mind about extension methods is they are just syntax sugar. They don’t actually stick around or become injected into the types they are decorating. They are replaced at compile time to static method invocations. Your mileage with extension methods will vary.

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

Sidebar

Related Questions

Consider the following classes: public interface IView { /// <summary> /// Ignore this (its
I've decompiled mscorlib library with ILSpy and noticed that List.Clear method uses Array.Clear(this._items, 0,
Summary: I have passed the name of the id select tag into an Array.
Summary I have an ASP.NET 3.5 website and a mobile detection method inside a
SUMMARY: How to configure a web service such that writing to the Event Log
I have a class like this: [Serializable] public class Structure { #region Constants and
SUMMARY: How to compile in Release mode...I cannot get it to take what I
Sometimes it's useful to derive my entities from a base class like this: public
Is there a method that tests if 2 URLs are equal, ie point to
Summary for those who might not want to read that much: How do I

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.