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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:17:22+00:00 2026-05-27T13:17:22+00:00

Is it possible to define how to cast a built in object to an

  • 0

Is it possible to define how to cast a built in object to an interface in C#? Interfaces can’t define operators. I have a very simple interface that allow index access, but not mutation:

public interface ILookup<K, V>
{
    V this[K key] { get; }
}

I’d like to be able to cast a Dictionary<K, V> to an ILookup<K, V>. In my ideal dream world this would look like:

//INVALID C#
public interface ILookup<K, V>
{
    static implicit operator ILookup<K, V>(Dictionary<K, V> dict)
    {
         //Mystery voodoo like code. Basically asserting "this is how dict
         //implements ILookup
    }
    V this[K key] { get; }
}

What I’ve worked out as a workaround is this:

public class LookupWrapper<K, V> : ILookup<K, V>
{
    private LookupWrapper() { }

    public static implicit operator LookupWrapper<K, V>(Dictionary<K, V> dict)
    {
        return new LookupWrapper<K, V> { dict = dict };
    }

    private IDictionary<K, V> dict;
    public V this[K key] { get { return dict[key]; } }
}

This works and means I can now directly cast from Dictionary to ILookup, but boy does it feel convoluted…

Is there a better way to force a conversion to an interface?

  • 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-27T13:17:23+00:00Added an answer on May 27, 2026 at 1:17 pm

    Thanks to @MBabcock’s comment I realized I was thinking about this wrong. I promoted the interface to a full on class as follows:

    public class Lookup<K, V>
    {
        private readonly Func<K, V> lookup;
        protected Lookup(Func<K, V> lookup)
        {
            this.lookup = lookup;
        }
    
        public static implicit operator Lookup<K, V>(Dictionary<K, V> dict)
        {
            return new Lookup<K, V>(k => dict[k]);
        }
    
        public V this[K key]
        {
            get { return lookup(key); }
        }
    }
    

    Any further conversions I need like this I can simply add an implicit operator

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

Sidebar

Related Questions

Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Is it possible to define an interface (e.g. MyClass Implements MyInterface) whose method/property definitions
I have (for example) an object of type A that I want to be
Possible Duplicate: Cast string to enum with enum attribute I have a enum like
Is it possible to define an Interface with optional implementation methods? For example I
I currently have this macro #define bind(OBJECT, ROLE) \ assert( sizeof(*ROLE) == 1 );
Is it possible to define a default value that will be returned in case
Is it possible to define an interface where one of the objects is a
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
Is it possible to define a binding between an exchange (direct or topic) and

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.