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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:28:16+00:00 2026-05-21T18:28:16+00:00

Considering the following (pointless, but it’s for illustration purpose) test class : public class

  • 0

Considering the following (pointless, but it’s for illustration purpose) test class :

public class Test
{
    public IEnumerable<string> ToEnumerableStrsWontCompile(IEnumerable<dynamic> t)
    {
        return t.Select(x => ToStr(x));
    }

    public IEnumerable<string> ToEnumerableStrsWillCompile(IEnumerable<dynamic> t)
    {
        var res = new List<string>();

        foreach (var d in t)
        {
            res.Add(ToStr(d));
        }

        return res;
    }

    public string ToStr(dynamic d)
    {
        return new string(d.GetType());
    }
}

Why doesn’t it compile with the following error, on t.Select(x => ToStr(x)) ?

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

No error on the second method.

  • 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-21T18:28:17+00:00Added an answer on May 21, 2026 at 6:28 pm

    I believe what happens here is that since the expression ToStr(x) involves a dynamic variable, the whole expression’s result type is also dynamic; that’s why the compiler thinks that it has an IEnumerable<dynamic> where it expects an IEnumerable<string>.

    public IEnumerable<string> ToEnumerableStrsWontCompile(IEnumerable<dynamic> t)
    {
        return t.Select(x => ToStr(x));
    }
    

    There are two ways you can fix this.

    Use an explicit cast:

    public IEnumerable<string> ToEnumerableStrsWontCompile(IEnumerable<dynamic> t)
    {
        return t.Select(x => (string)ToStr(x));
    }
    

    This tells the compiler that the result of the expression is definitely going to be a string, so we end up with an IEnumerable<string>.

    Replace the lambda with a method group:

    public IEnumerable<string> ToEnumerableStrsWontCompile(IEnumerable<dynamic> t)
    {
        return t.Select(ToStr);
    }
    

    This way the compiler implicitly converts the method group expression to a lambda. Note that since there is no mention of the dynamic variable x in the expression, the type of its result can be immediately inferred to be string because there is only one method to consider, and its return type is string.

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

Sidebar

Related Questions

Considering following code public class A { public static void main(String[] args) { new
Considering the following: public class Person { public Person(string fName, string lName) { this.firstName
Considering the following Scala snippet: case class Foo(v1: String, v2: Int, v3: Any) def
Considering the following code public bool GetFalse() { return false; } public bool GetTrue()
Considering such code: class ToBeTested { public: void doForEach() { for (vector<Contained>::iterator it =
Considering the following model: USER Long: PK String: firstName String: lastName USER_EXT Long: PK
We're writing a compiler for Al Aho's compilers class, and we're considering the following
Which of the following is correct/better, considering that identity property could be null. public
Considering the following code public interface IEntity { int Id { get; set; }
Considering the following string: String s = /static/201105-3805-somerandom/images/optional-folder/filename.gif; How can I remove the static/201105-3805-somerandom/

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.