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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:04:37+00:00 2026-06-02T13:04:37+00:00

I am working on an application in c#. To make this application work, I

  • 0

I am working on an application in c#. To make this application work, I have found myself doing some things that feel quite unnatural for the language I have selected. After going through many refactorings and refinements, I have come to the realization that the functionality that I am trying to implement is really a form of ‘lazy evaluation’ (I think). This is what I would like…

// Criteria 1: Lazy evaluation of expressions
int x = LazyEvaluated.New<int>();
int y = LazyEvaluated.New<int>();
int z = LazyEvaluated.New<int>();

z.Assign(x + y);
Assert.ThrowsException(z.Evalutate());
x.Assign(1);
Assert.ThrowsException(z.Evalutate());
y.Assign(2);
Assert.Equals(3, z.Evaluate());
x.Assign(3);
Assert.Equals(5, z.Evaluate());

// Criteria 2: Referencing relative to parent object    
Car myCar = LazyEvaluated.New<Car>();
Engine engineInMyCar = LazyEvaluated.New<Engine>();
double displacementOfMyEngine = LazyEvaluated.New<double>();

engineInMyCar = myCar.Engine;
displacementOfMyEngine = engineInMyCar.Displacement;

Car subaru = new Car(new FlatFourEngine());
Car falcon = new Car(new InlineSixEngine());

myCar.Assign(subaru);
Assert.IsTypeOf<FlatFourEngine>(engineInMyCar.Evaluate());
Assert.IsEqual(2.0, displacementOfMyEngine.Evaluate());

myCar.Assign(falcon);
Assert.IsTypeOf<InlineSixEngine>(engineInMyCar.Evaluate());
Assert.IsEqual(4.0, displacementOfMyEngine.Evaluate());

And these are the simple class definitions that I have used for illustration…

public class Car {
    private readonly Engine engine;            
    public Car(Engine engine) { this.engine = engine; }
    public Engine Engine { get { return engine; } }
}

public abstract class Engine {
    public abstract double Displacement { get; }
}        

public class FlatFourEngine : Engine {
    public override double Displacement { get { return 2.0; } }
}

public class InlineSixEngine : Engine {
    public override double Displacement { get { return 4.0; } }
}    

The reason I want this functionality is mostly for separation-of-concerns. To illustrate, let’s take the car analogue a bit further. My car is made up of lots of bits; engine, tyres, interior etc. My tyres need to be changed every 2 years by a tyre shop. My engine needs to have an oil change every 6 months by a mechanic. The interior needs to be vacuumed every 3 months by a detailer. It would be nice to able to do something along the lines of…

tyreShop.ReplaceTyres(myCar.Tyres, every2Years);
mechanic.ChangeOil(myCar.Engine, every6Months);
detailer.VacuumInterior(myCar.Interior, every3Months);

Some reasons for this approach;

  • I would like to set-and-forget. Once I schedule a part of my car in for a service, I want it to happen on an ongoing basis without any further input from me.
  • I don’t want to have to call each of my service centres and notify them the instant that I purchase a new car. In fact, they don’t need to know that I have a new car until it comes time for a service. If they try to service my car and it doesn’t exist, then it’s ok for them to throw an exception.
  • I want each of my services to be as simple and focussed as possible. Why give the tyre shop a reference to my entire car (including engine, interior, etc), when the only thing they really care about is the tyres?
  • I don’t want the tyreshop to give me a new set of tyres and force me to fit (assign) them to the car myself.

I do not have any formal education in CS, and my only programming experience is with c & c# languages. I’ve just spent the last 6 months getting familiar with c# & .Net. For the most part, I really like the language and early-evaluation in general. But I am now questioning whether I would have been better off tackling my particular problem in a different language with inbuilt support for lazy evaluation.

My questions are;

  1. If I were to start again, what would be the best language to tackle such a problem in?
  2. Are there any existing platforms for c# that provide support for lazy evaluation?
  3. What are the alternatives for making this work in c#? Are there any particular design patterns I should read up on?
  • 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-02T13:04:38+00:00Added an answer on June 2, 2026 at 1:04 pm

    It sounds like what you’re really after is scheduling an action to take place later, repeatedly. I’d suggest that delegates – probably using lambda expressions – are the most appropriate approach for this:

    scheduler.Schedule(every3Months, () => tyreShop.ReplaceTyres(myCar));
    

    This way the only piece of your code which needs to know about the laziness aspect is the scheduler itself. All the rest of your code can assume that when you call a method, you want that action to take place right now, which is much more idiomatic C#.

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

Sidebar

Related Questions

I am working on an ASP.NET application that make a lot of jquery and
I'm working on an application where users have to make a call and type
I have been working on an application which allows the user to make a
I am working on splitting out an existing, working application that I currently have
I have an application script that is working correctly, but I have a few
I've been working on a guitar tuner Java application for quite some time and
I am working on an application that involves some gis stuff. There would be
I'm working on a project where I need to make a Flex application that
I'm working with an application, and I am able to make C# scripts to
I'm working on an html5 application built on CouchDB. I want to make sure

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.