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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:04:32+00:00 2026-06-18T02:04:32+00:00

I have a question about foreach behavior in C# . My custom class implements

  • 0

I have a question about foreach behavior in C#.

My custom class implements a custom GetEnumerator. This method returns another object that is implicitly convertible to string.

However if I do foreach(string s in customClass) it fails during run-time (“Unable to cast object of type .. to string”).

However if I do string x = new B() it works like a charm.

NOTE: There is nothing in particular I need to achieve here, I just want to understand what is going on. I am particularly interested in this non-generic behavior.

Any ideas? What fundamental knowledge am I missing?

Code to replicate this:

public class A : IEnumerable
{
    #region IEnumerable Members

    public IEnumerator GetEnumerator()
    {
        yield return new B();
    }

    #endregion
}

public class B
{
    public static implicit operator string( B b )
    {
        return "to-stringed implicit";
    }
}

// CODE:

A a = new A();

// Works.
B b = new B();
string xxx = b;

// Doesnt work.
foreach( string str in a )
{
}
  • 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-18T02:04:33+00:00Added an answer on June 18, 2026 at 2:04 am

    Your implicit conversion can only be used if the compiler sees it can be used at compile-time:

    B b = new B();
    string str = b;
    

    It can’t be used at run-time:

    B b = new B();
    object obj = b;
    string str = obj; // will fail at run-time
    

    Basically, this is because it would be far too expensive to look through all the possible conversions from obj to a string that might work. (See this Eric Lippert blog post).


    Your IEnumerator returns objects, so calling foreach (string str in a) is trying to convert an object to a B at runtime.

    var e = a.GetEnumerator();
    e.MoveNext();
    object o = e.Current;
    string str = o; // will fail at run-time
    

    If you instead use foreach(B item in a) { string str = item; ... }, the runtime conversion is from object to B (which works, because each object is a B), and the conversion from B to str can be made by the compiler.

    var e = a.GetEnumerator();
    e.MoveNext();
    object o = e.Current;
    B item = o;        // will work at run-time because o _is_ a B
    string str = item; // conversion made by the compiler
    

    A different way to fix this would be to make A implement IEnumerable<B> instead of just IEnumerable. Then, foreach (string str in a) translates more as

    var e = a.GetEnumerator();
    e.MoveNext();
    B b = e.Current; // not object!
    string str = b;  // conversion made by the compiler
    

    so the compiler can make the conversion without you having to change your foreach loop.

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

Sidebar

Related Questions

I have this question about c# language's dynamic binding behavior. Consider the following object
I have question about parsing in Html helper : I have sth like: @foreach
I have a question about this formula from a book: EFW (cm,kg)= 10^(-1,7492+(0,166*BPD)+(0,046*AC)-(2,646*AC*BPD/1000)) The
I am writing a script in Perl and have a question about Perl's foreach
I have a question about the .click function in Jquery. I have this code:
I have a question about foreach in tcl: foreach id 6 8 { #do
I have a question about arrays and foreach. If i have an array like
Hey guys, I have a question about asp.net I have this table with checkbox
I have a question about: how can I use foreach the service's namespaces in
I have a question about the calendar class. I can populate days in the

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.