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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:19:59+00:00 2026-06-18T01:19:59+00:00

If I have a generic class: class Collection < G > { public G[]

  • 0

If I have a generic class:

class Collection < G >  {
    public G[] stuff
}

and I have two other classes, one of which can convert to the other (though more complex than this!)

class Foo {
    public Foo( int i ) { myInt = i; }
    public int myInt;
}
class Bar {
    public Bar( float f ) { myFloat = f; }
    public float myFloat;
    public static implicit operator Foo( Bar bar ) {
        return new Foo( Math.Ceil(bar.myFloat) );
    }
}

And I have collections of both:

Collection < G >  fooCollection = new Collection < Foo > ();
Collection < G >  barCollection = new Collection < Bar > ();

I want to be able to do something like this:

Collection < G > fooCollection2 = barCollection.Convert( typeof(Foo) );

How would I go about that?

EDIT: this is for Unity, which I believe is still on .NET 2.0.

  • 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-18T01:20:01+00:00Added an answer on June 18, 2026 at 1:20 am
    Collection<Foo> fooCollection = new Collection<Foo> { stuff = barCollection.stuff.Select(bar => (Foo)bar).ToArray() };
    

    If you’d like, you can add extension method to Collection:

    public static Collection<TResult> Select<TResult, T>(this Collection<T> c, Func<T, TResult> projection)
    {
       return new Collection<TResult> { stuff = c.stuff.Select(x => projection(x)).ToArray() };
    }
    

    And then you can call it like so:

    Collection<Foo> fooCollection2 = barCollection.Select(bar => (Foo)bar);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built myself a generic collection class which is defined like this. public
I have a generic Collection class, with a variety of public getter methods. To
I have a generic class NamedValue<TValue> : public class NamedValue<TValue> { public string Name
If say I have some generic class, for example: public class Attribute<T> { }
I have a generic class containing a special collection. An instance of this collection
If I have a generic class: public class MyClass<T> { public T Value; }
Lets say we have a program which contains such classes: public interface AbstractItem {
I have a compiler error when trying to set a generic base collection class
I have a class 'Collection', which has an add method. The add method should
Say I have the following classes: public class BaseConfig { } public class SpecialConfig

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.