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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:36:02+00:00 2026-05-23T03:36:02+00:00

I ran into a problem this week regarding implicit conversions in C# on collections.

  • 0

I ran into a problem this week regarding implicit conversions in C# on collections. While this (using implicit) may not be our final approach, I wanted to at least finish out the code to offer the team as an option. I boiled down the problem to the following sample case:

I have two classes in my example: one that represents a business object (Foo) and one that represents the client version (View Object) of this business item (FooVO), as defined below…

public class Foo
{
    public string Id {get; set;}

    public string BusinessInfo {get; set;}
}

public class FooVO
{
    public string Id {get; set;}

    public static implicit operator FooVO( Foo foo )
    {
        return new FooVO { Id = foo.Id };
    }
}

My problem is when I have a a List of Foo objects and want to convert them to a list of FooVO objects using my implicit operator.

List<Foo> foos = GetListOfBusinessFoos(); // Get business objects to convert

I tried

List<FooVO> fooVOs = foos; // ERROR

and

List<FooVO> fooVOs = (List<FooVO>) foos; // ERROR

and even

List<FooVO> fooVOs = foos.Select( x => x ); // ERROR

I know I can do this in a loop, but I was hoping for straightforward (LINQ?) way to convert the objects in one shot. Any ideas?

Thank you in advance.

Edit
Fixed typo in example

  • 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-23T03:36:02+00:00Added an answer on May 23, 2026 at 3:36 am

    A question much like this gets asked almost every day on SO. You can’t do this because doing so violates type safety:

    List<Giraffe> g = new List<Giraffe>();
    List<Animal> a = g; // Should this be legal?
    a.Add(new Tiger()); // Nope; we just added a tiger to a list of giraffes.
    

    In C# 4.0 you can implicitly convert from IEnumerable<Giraffe> to IEnumerable<Animal> because there is no “Add” method to screw things up. But you can never do a “covariant” conversion like that if the conversion of the element types is user-defined. It has to be a reference or identity conversion.

    You’ll need to create a second list and copy them over one at a time. Or use the LINQ helper methods like Select and ToList to do that work for you.

    The name of the type system concept you want is “covariance”; a covariant relationship is one where you reason “Giraffes are animals therefore sequences of giraffes are sequences of animals”. If this subject interests you then you can read about how we added covariance (and contravariance) to C# 4.0 here:

    http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/default.aspx

    Start from the bottom.

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

Sidebar

Related Questions

I ran into this problem while developing in Objective-C for iOS, but this should
I ran into this problem using EF4 and a self-referential table (implementing an adjacency
I deployed an ASP.NET MVC app using MSDeploy and ran into this problem i
I ran into a problem last week moving from dev-testing where one of my
Ran into this problem today, posting in case someone else has the same issue.
I ran into this problem at my old job, and now again at my
Hii , I am developing an application where I ran across into this problem
I ran into this problem in a Visual Basic program that uses WMI but
I've never ran into this problem before. The menu looks good on Windows, but
Previously, I ran into a problem trying to share a type definition between my

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.