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

  • Home
  • SEARCH
  • 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 126941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:23:51+00:00 2026-05-11T05:23:51+00:00

I am wondering what it would take to make something like this work: using

  • 0

I am wondering what it would take to make something like this work:

using System;  class Program {     static void Main()     {         var f = new IFoo {                      Foo = 'foo',                     Print = () => Console.WriteLine(Foo)             };     } }  interface IFoo {     String Foo { get; set; }     void Print(); } 

The anonymous type created would look something like this:

internal sealed class <>f__AnonymousType0<<Foo>j__TPar> : IFoo {     readonly <Foo>j__TPar <Foo>i__Field;      public <>f__AnonymousType0(<Foo>j__TPar Foo)     {         this.<Foo>i__Field = Foo;     }      public <Foo>j__TPar Foo     {         get { return this.<Foo>i__Field; }     }      public void Print()     {         Console.WriteLine(this.Foo);     } } 

Is there any reason that the compiler would be unable to do something like this? Even for non-void methods or methods that take parameters the compiler should be able to infer the types from the interface declaration.

Disclaimer: While I do realize that this is not currently possible and it would make more sense to simply create a concrete class in this instance I am more interested in the theoretical aspects of this.

  • 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. 2026-05-11T05:23:52+00:00Added an answer on May 11, 2026 at 5:23 am

    There would be a few issues with overloaded members, indexers, and explicit interface implementations.

    However, you could probably define the syntax in a way that allows you to resolve those problems.

    Interestingly, you can get pretty close to what you want with C# 3.0 by writing a library. Basically, you could do this:

    Create<IFoo> (     new     {         Foo = 'foo',         Print = (Action)(() => Console.WriteLine(Foo))     } ); 

    Which is pretty close to what you want. The primary differences are a call to ‘Create’ instead of the ‘new’ keyword and the fact that you need to specify a delegate type.

    The declaration of ‘Create’ would look like this:

    T Create<T> (object o) { //... } 

    It would then use Reflection.Emit to generate an interface implementation dynamically at runtime.

    This syntax, however, does have problems with explicit interface implementations and overloaded members, that you couldn’t resolve without changing the compiler.

    An alternative would be to use a collection initializer rather than an anonymous type. That would look like this:

    Create {     new Members<IFoo>     {         {'Print', ((IFoo @this)=>Console.WriteLine(Foo))},         {'Foo', 'foo'}     } } 

    That would enable you to:

    1. Handle explicit interface implementation by specifying something like ‘IEnumerable.Current’ for the string parameter.
    2. Define Members.Add so that you don’t need to specify the delegate type in the initializer.

    You would need to do a few things to implement this:

    1. Writer a small parser for C# type names. This only requires ‘.’, ‘[]’, ‘<>’,ID, and the primitive type names, so you could probably do that in a few hours
    2. Implement a cache so that you only generate a single class for each unique interface
    3. Implement the Reflection.Emit code gen. This would probably take about 2 days at the most.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This issue has to do with the CSS spec on… May 12, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer SELECT tem.name, COUNT(*) FROM ( SELECT name FROM results UNION… May 12, 2026 at 8:55 am
  • Editorial Team
    Editorial Team added an answer This worked for me: m_ribbonBar.ForceRecalcLayout(); //CMFCRibbonBar m_ribbonBar; May 12, 2026 at 8:55 am

Related Questions

I've been wondering why would I use Exceptions in my PHP. Let's take a
I am about to write a program to keep track of my school assignments
I have a large system that I have coded and I wish to make
In another posting: Does Linq-To-Sql support composable queries there was discussion on how to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.