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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:19:20+00:00 2026-05-14T19:19:20+00:00

So I’ve written a quick bit of code to help quickly convert between business

  • 0

So I’ve written a quick bit of code to help quickly convert between business objects and view models. Not to pimp my own blog, but you can find the details here if you’re interested or need to know.

One issue I’ve run in to is that I have a custom collection type, ProductCollection, and I need to turn that in to a string[] in on my model. Obviously, since there is no default implicit cast, I’m getting an exception in my contract converter.

So, I thought I would write the next little bit of code and that should solve the problem:

public static implicit operator string[](ProductCollection collection) {
    var list = new List<string>();
    foreach (var product in collection)
    {
        if (product.Id == null)
        {
            list.Add(null);
        }
        else
        {
            list.Add(product.Id.ToString());
        }
    }
    return list.ToArray();
}

However, it still fails with the same cast exception. I’m wondering if it has something to do with being in reflection? If so, is there anything that I can do here?? I’m open to architectural solutions, too!

  • 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-14T19:19:21+00:00Added an answer on May 14, 2026 at 7:19 pm

    First, the implicit operator allows for implcit conversions (no casting directive). explicit operators operate on casts.

    Nonetheless, that isn’t the real issue here. Operators are not polymorphic (they’re overloaded, not overridden); that is, in order to take advantage of an overloaded operator, you must be referring to a class in the context of the class that defines it.

    For example:

    public class Foo
    {
        public static implicit operator Bar(Foo foo) { return new Bar(); }
    }
    
    public class Bar { }
    
    ...
    
    void Baz()
    {
        Foo foo = new Foo();
    
        Bar bar = foo; // OK
    
        object baz = foo;
    
        bar = baz; // won't compile, there's no defined operator at the object level
    
        bar = (Bar)baz; // will compile, but will fail at runtime for the same reason
    }
    

    Without knowing what you’re doing and how you’re doing it, I can’t offer much in the way of a meaningful alternative. Some ideas, however:

    1. Override ToString (or use a common base class / interface for your entities with your own version if you don’t want to use ToString) on your entities, then enumerate the collections generically to construct your string array
    2. Provide a ToStringArray function on your collections (by way of a common base class or interface) that does something similar

    The biggest point is not to use operators for this purpose; they won’t help you.

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

Sidebar

Ask A Question

Stats

  • Questions 454k
  • Answers 454k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer DNS Rebinding can be used to bypass Same Origin Policy… May 15, 2026 at 9:54 pm
  • Editorial Team
    Editorial Team added an answer It's also possible with the actual HTML 5 Geolocation functions… May 15, 2026 at 9:54 pm
  • Editorial Team
    Editorial Team added an answer resolveClassMethod. Nuff said. May 15, 2026 at 9:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.