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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:07:48+00:00 2026-06-16T17:07:48+00:00

Got another simple question here that is eluding me. I have 2 classes: namespace

  • 0

Got another simple question here that is eluding me.

I have 2 classes:

namespace Assets
{
   public class BaseAsset
   {
       // Code here
   }
}

And

namespace Assets
{
   public class Asset : BaseAsset
   {
       // Code here
   }
}

I have a function that returns a collection of Asset from the database and I want another function to execute that function and return a collection of BaseAsset.
I have tried this:

    public static Collection<BaseAsset> GetCategoryAssets(int CategoryId, string UserId, string CompanyId)
    {
        return (Collection<BaseAsset>)AssetData.getAssets(CategoryId, UserId, CompanyId);
    }

but as you can guess, it doesn’t work.
If I was working with lists, I could do:

    public static List<BaseAsset> GetCategoryAssets(int CategoryId, string UserId, string CompanyId)
    {
        return AssetData.getAssets(CategoryId, UserId, CompanyId).Cast<BaseAsset>().ToList();
    }

But I would prefer to use a collection, can anyone come up with an elegant solution?

Cheers,
r3plica

  • 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-16T17:07:50+00:00Added an answer on June 16, 2026 at 5:07 pm

    This is a very frequently asked question. The name of the feature that you want is generic covariance; that is, the feature that says “if a giraffe is a kind of animal then a list of giraffes is a kind of list of animals.”

    The problem is that a list of giraffes is not a kind of list of animals. You can put a tiger into a list of animals, but you can’t put a tiger into a list of giraffes, and therefore a list of giraffes cannot be used in any context where a list of animals is expected.

    The reason you should use IEnumerable<T> instead of Collection<T> is because as of C# 4, IEnumerable<T> is covariant in T, provided that the type arguments provided are both reference types. That is, a sequence of strings can be used as a sequence of objects, because both are reference types. But a sequence of ints cannot be used as a sequence of objects, because one is a value type.

    The reason this is safe is because there is no way to insert a tiger into an IEnumerable<Giraffe>.

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

Sidebar

Related Questions

Got a simple question here to a problem that's grown over time. I have
I've got a simple question. In Objective-C, when you have a method you want
I have in my code the concept of command : public abstract class BaseCommand
I am bit new to C# and got a question. I have a class
I've got a piece of text that contains another regular expression. Sample text: ...<rege>!^.*$!mailto:asdf@adsfsdaf.com!</rege>...
I've got another problem in the same code... I'm getting this error: initialization method
I've got another question regarding to basic Android programming: How can I access the
I have a domain with host www.mydomainhost.com . Now i got another domain named
Time for another silly question :) I got some tables in a xhtml-document with
I've got some Workflow Foundation 4 ActivityDesigners that I'd like to have interact with

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.