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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:40:10+00:00 2026-06-05T21:40:10+00:00

Generic interface: interface IEntity<TKey, TValue> { TKey Key { get; set; } TValue Value

  • 0

Generic interface:

interface IEntity<TKey, TValue>
{
    TKey Key { get; set; }
    TValue Value { get; set; }
}

My entity class:

class MyEntity : IEntity<int, string>
{
    public Key { get; set; }
    public Value { get; set; }
}

and a methode to do something with all posible entities in generic way:

void MyMethode<T>(T entity) where T : IEntity<int, string> // there is no point, need T parameters
{
    DoEntity(entity.Key, entity.Value);
}

i need accomplish something like:

void MyMethode<T>(T entity) where T : IEntity<TKey, TValue> // this is not allowed
{
    DoEntity(entity.Key, entity.Value);
}

I need to write common database operations on my entities but the problem is i do not know what type Key and Value will be.

  • 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-05T21:40:11+00:00Added an answer on June 5, 2026 at 9:40 pm

    In that case, you need to define it like this:

    void MyMethode<T, TKey, TValue>(T entity) where T : IEntity<TKey, TValue> 
    {
        DoEntity(entity.Key, entity.Value);
    }
    

    But that seems a bit crazy.

    The better option may be to have a base IEntity which is inherited by IEntity<TKey, TValue>. IEntity would simply expose the value and key as object rather than as specific types:

    interface IEntity
    {
        object Key { get; set; }
        object Value { get; set; }
    }
    
    interface IEntity<TKey, TValue> : IEntity
    {
        TKey Key { get; set; }
        TValue Value { get; set; }
    }
    
    class MyEntity : IEntity<int, string>
    {
        int IEntity<int, string>.Key
        {
            get { ... }
            set { ... }
        }
    
        string IEntity<int, string>.Value
        {
            get { ... }
            set { ... }
        }
    
        object IEntity.Key
        {
            get { ... }
            set { ... }
        }
    
        object IEntity.Value
        {
            get { ... }
            set { ... }
        }
    }
    

    Then you could simply do this:

    void MyMethode<T>(T entity) where T : IEntity
    {
        DoEntity(entity.Key, entity.Value);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code public interface IEntity { int Id { get; set; }
How to get class object of a generic interface? For example, Boolean.class, Date.class. But
Ok I have a generic interface public IConfigurationValidator<T> { void Validate(); } a class
Given the following generic interface and implementing class: public interface IRepository<T> { // U
I have a generic interface: public interface DAO<T> { public T get(long id); public
I have a generic interface: public interface IUnauthorizedRequestRespondable<out T> where T:class { T GetResponseForUnauthorizedRequest();
Lets say I have a class, which implements a generic interface public interface IItem
I have a generic interface IConstrained which is implemented by the generic Constrained class.
Having these generic interface and class: interface TestIntf<T extends TestIntf<T>> { void test(T param);
How to get generic interface type for an instance ? Suppose this code: interface

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.