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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:49:22+00:00 2026-05-12T15:49:22+00:00

I’ve defined a generic class Lazy<T> , for lazy evaluation and caching of the

  • 0

I’ve defined a generic class “Lazy<T>“, for lazy evaluation and caching of the result of a delegate Func<T>.

I also define two implicit cast operators so I can create a Lazy<T> from a Func<T>s, and I can assign a Lazy<T> to a T (gets the Value of the Lazy<T>)

The idea is that you can pass around a Lazy<T> in place of an instance of T, but not do the work to calculate/retrieve the value until it is assigned to an actual instance of T.

// class Lazy<T>
// Encapsulates a value which can be retrieved when first accessed, 
// and is then cached.
class Lazy<T>
{
  private Func<T> _getter;
  private T _cached;
  private bool _isCached;

  // Get/set the getter delegate
  // that 'calculates' the value.
  public Func<T> Getter
  {
    get
    {
      return _getter;
    }
    set 
    {
      _getter = value;
      _cached = default(T);
      _isCached = false;
    }
  }

  // Get/set the value.
  public T Value
  {
    get 
    {
      if (!_isCached) 
      {
        _cached = Getter();
        _isCached = true;
        _getter = null;
      }
      return _cached;
    }
    set
    {
      _cached = value;
      _isCached = true;
      _getter = null;
    }
  }

  // Implicit casts:

  // Create a T from a Lazy<T>
  public static implicit operator T(Lazy<T> lazy) 
  {
    return lazy.Value;
  }

  // Create a Lazy<T> from a Func<T>
  public static implicit operator Lazy<T>(Func<T> getter)
  {
    return new Lazy<T> {Getter = getter};
  }
}

But this class doesn’t work as I expected in one case, highlighted in the test app below:

class Program
{
  static void Main()
  {
    // This works okay (1)
    TestLazy(() => MakeStringList());

    // This also works (2)
    Lazy<string> lazyString = new Func<string>(() => "xyz");
    string s = lazyString;

    //This doesn't compile (3)
    //
    Lazy<IList<string>> lazyStrings = new Func<IList<string>>(MakeStringList);
    IList<string> strings = lazyStrings; //ERROR
  }


  static void TestLazy<T>(Func<T> getter)
  {
    Lazy<T> lazy = getter;
    T nonLazy = lazy;
  }

  private static IList<string> MakeStringList()
  {
    return new List<string> { new string('-', 10) };
  }
}

On the line marked with //ERROR, I get a compile error:

error CS0266: Cannot implicitly convert type Lazy<System.Collections.Generic.IList<string>> to System.Collections.Generic.IList<string>. An explicit conversion exists (are you missing a cast?)

This error is confusing as there does exist an implicit cast from the source to the target type in question.
And, on the face of it, code chunk (3) is doing the same thing as (1)
Also, it differs from (2) only by the type used to specialize the Lazy.

Can anyone explain to me what’s going on here?

  • 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-12T15:49:22+00:00Added an answer on May 12, 2026 at 3:49 pm

    The problem is that you’re trying to convert to IList<T> implicitly, and IList<T> isn’t encompassed by IList<T> (even though they’re the same type) – only conversions to non-interface types are considered in encompassing. From section 6.4.3 of the C# 3.0 spec:

    If a standard implicit conversion
    (§6.3.1) exists from a type A to a
    type B, and if neither A nor B are
    interface-types
    , then A is said to be
    encompassed by B, and B is said to
    encompass A.

    In section 6.4.4, talking about user defined conversions, one of the steps is (emphasis mine):

    • Find the set of applicable user-defined and lifted conversion
      operators, U.

    This set consists of the user-defined
    and lifted implicit conversion
    operators declared by the classes or
    structs in D that convert from a type
    encompassing S to a type encompassed
    by T
    . If U is empty, the conversion is
    undefined and a compile-time error
    occurs.

    IList<T> isn’t encompassed by IList<T>, therefore this step fails.

    The compiler will do “chained” implicit conversions in other scenarios though – so if you actually had a Lazy<List<T>> you could write:

    object strings = lazyStrings;
    

    works, because List<T> is encompassed by object (as both are classes, and there’s a standard implicit conversion from List<T> to object).

    Now as for why this is the case, I suspect it’s to stop odd cases where you’d expect a reference conversion, but you would actually get the implicit conversion. Suppose we had:

    class ListLazy : Lazy<IList<string>>, IList<string>
    {
        // Stuff
    }
    ...
    Lazy<IList<string>> x = new ListLazy();
    IList<string> list = x;
    

    Which conversion should be used? There’s an implicit reference conversion from the actual type to IList<string>… but the compiler doesn’t know that, because the expression is of type Lazy<IList<string>>. Basically interfaces are awkward because they can show up later in the type hierarchy, whereas with a class you always know where you are, if you see what I mean. (Implicit conversions which involve two classes in the same hierarchy are prohibited.)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have two tables with like below codes: Table: Accounts id | username |
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

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.