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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:17:11+00:00 2026-05-14T19:17:11+00:00

Please consider the following snippet from an implementation of the Interpreter pattern: public override

  • 0

Please consider the following snippet from an implementation of the Interpreter pattern:

public override object Execute(Interpreter interpreter, object ctx) {
    var list = ctx as IEnumerable<string>;
    return (list != null) ? list.FirstOrDefault() : null;
}

What about if I want to use the same function for integers?

public override object Execute(Interpreter interpreter, object ctx) {
    var list = ctx as IEnumerable<string>;
    if (list != null) return list.FirstOrDefault();

    var list = ctx as IEnumerable<int>;
    return (list != null) ? list.FirstOrDefault() : null;
}

What I wanted was something like:

public override object Execute(Interpreter interpreter, object ctx) {
    var list = ctx as IEnumerable;
    return (list != null) ? list.FirstOrDefault() : null;
}

But Linq doesn’t act on IEnumerables. Instead, to get to this solution, I would be forced to write something like:

public override object Execute(Interpreter interpreter, object ctx) {
    var list = ctx as IEnumerable;
    if (list != null) foreach(var i in list) { yield return i; return; }
    return null;
}

Or use a generic method:

public override T Execute<T>(Interpreter interpreter, object ctx) {
    var list = ctx as IEnumerable<T>;
    return (list != null) ? list.FirstOrDefault() : null;
}

Which would break the Interpreter pattern (as it was implemented in this system). Covariance would also fail (at least in C#3), though would it work, it would be the exact behavior I wanted:

public override object Execute(Interpreter interpreter, object ctx) {
    var list = ctx as IEnumerable<object>;
    return (list != null) ? list.FirstOrDefault() : null;
}

So, my question is: what’s the best way to achieve the intended behavior?


Addendum: digEmAll suggested something like the following code:

var list = ctx as IEnumerable;
return (list != null) ? list.Cast<object>().FirstOrDefault() : null;

This is a good answer for stuff like FirstOrDefault(). The problem comes with things like Reverse:

var list = ctx as IEnumerable;
return (list != null) ? list.Cast<object>().Reverse() : null;

I could fed this method a List<int> but I would get back a List<object>. No way to get around covariance, I know. So I guess digEmAll code is the best answer so far.


Thanks 🙂

  • 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-14T19:17:11+00:00Added an answer on May 14, 2026 at 7:17 pm

    Something like this, could fit your needs ?

    public override object Execute(Interpreter interpreter, object ctx) {
        var nonGenericlist = ctx as IEnumerable;
        if (nonGenericlist == null)
           return null;
        var list = nonGenericlist.Cast<object>();
        return (list != null) ? list.FirstOrDefault() : null;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please consider the following snippet: public interface MyInterface { public int getId(); } public
please consider the following snippet: SimpleDateFormat parser = new SimpleDateFormat(MMdd); parser.setLenient(false); try { Date
Please consider the following code: public class Person ( public string FirstName {get; set;}
Please consider the following java source: package com.stackoverflow; public class CondSpeed { private static
Please consider the following context from Innate : # Default application for Innate def
Please consider the following simple use case: public class Foo { public virtual int
Please consider the following code: class Abase{}; class A1:public Abase{}; class A2:public A1{}; //etc
Please consider the two following snippets of code: (function f() { var x; try
Please consider the following: I am storing around 1.2 Million TIF files ranging from
Please consider the following function body: var isValidated = true; $(selector1).each(function(){ //do validation with

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.