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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:25:52+00:00 2026-06-11T04:25:52+00:00

I know and I think I understand about the (co/conta)variance issue with IEnumerables. However

  • 0

I know and I think I understand about the (co/conta)variance issue with IEnumerables. However I thought the following code would not be affected by it.

[DataContract]
public class WcfResult<T>
{
    public WcfResult(T result)
    {
        Result = result;
    }

    public WcfResult(Exception error)
    {
        Error = error;
    }

    public static implicit operator WcfResult<T>(T rhs)
    {
        return new WcfResult<T>(rhs);
    }

    [DataMember]
    public T Result { get; set; }
    [DataMember]
    public Exception Error { get; set; }
}

This class is to mimic BackgroundWorker’s RunWorkerCompletedEventArgs so I can return errors from my WCF service without faulting the connection.

Most of my code is working fine so I can do things like this

public WcfResult<Client> AddOrUpdateClient(Client client)
{
    try
    {
        //AddOrUpdateClient returns "Client"
        return client.AddOrUpdateClient(LdapHelper);
    }
    catch (Exception e)
    {
        return new WcfResult<Client>(e);
    }
}

and it works fine, however the folowing code gives a error

public WcfResult<IEnumerable<Client>> GetClients(ClientSearcher clientSearcher)
{
    try
    {
        //GetClients returns "IEnumerable<Client>"
        return Client.GetClients(clientSearcher, LdapHelper, 100);
    }
    catch (Exception e)
    {
        return new WcfResult<IEnumerable<Client>>(e);
    }
}

Where the error is

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<myNs.Client>'
to 'myNs.WcfResult<System.Collections.Generic.IEnumerable<myNs.Client>>'. An explicit
conversion exists (are you missing a cast?)

What is going wrong that is causing this error to happen?

  • 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-11T04:25:53+00:00Added an answer on June 11, 2026 at 4:25 am

    Ah, you’ve been foiled by a less-than-obvious restriction of the C# Language Specification.

    For a given source type S and target type T, if S or T are nullable
    types, let S0 and T0 refer to their underlying types, otherwise S0 and
    T0 are equal to S and T respectively. A class or struct is permitted
    to declare a conversion from a source type S to a target type T only
    if all of the following are true:

    · S0 and T0 are different types.

    · Either S0 or T0 is the class or struct type in which the
    operator declaration takes place.

    · Neither S0 nor T0 is an interface-type.

    · Excluding user-defined conversions, a conversion does not
    exist from S to T or from T to S.

    Now, it doesn’t seem like this would apply because your implicit conversion function takes a generic parameter, but this restriction seems to apply to types used as generic arguments just as much. I took your example and changed IEnumerable to List (a full type, not just an interface) and it compiled without error.

    To make a long story short, you’re simply going to need to wrap any expression that returns an interface type in the WcfResult constructor, because the implicit cast won’t be available for it.

    return new WcfResult<IEnumerable<Client>>(Client.GetClients(clientSearcher, LdapHelper, 100));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, I do not know what I should understand when we talk about dynamic
I think I know C++ reasonably well and I am thinking about implementing something
Well I think I know the answer to this, but I would appreciate anybody
I'd like to know what you think about this part of our program is
I have an issue with co/contra-variance. I understand you can't have both input and
I know about jquery date-picker but cant understand how to implement it inside a
I don't know much about database optimization, but I'm trying to understand this case.
What do you think about if(!DoSomething()) return; In Clean Code this is viewed as
I come from a background of MoM. I think I understand ESB conceptually. However,
Every time I think I understand about working with vectors, what appears to be

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.