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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:29:31+00:00 2026-06-13T15:29:31+00:00

Sample program below: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericsTest {

  • 0

Sample program below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenericsTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IRetrievable<int, User> repo = new FakeRepository();

            Console.WriteLine(repo.Retrieve(35));
        }
    }

    class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    class FakeRepository : BaseRepository<User>, ICreatable<User>, IDeletable<User>, IRetrievable<int, User>
    {
        // why do I have to implement this here, instead of letting the
        // TKey generics implementation in the baseclass handle it?
        //public User Retrieve(int input)
        //{
        //    throw new NotImplementedException();
        //}
    }

    class BaseRepository<TPoco> where TPoco : class,new()
    {
        public virtual TPoco Create()
        {
            return new TPoco();
        }

        public virtual bool Delete(TPoco item)
        {
            return true;
        }

        public virtual TPoco Retrieve<TKey>(TKey input)
        {
            return null;
        }
    }

    interface ICreatable<TPoco> { TPoco Create(); }
    interface IDeletable<TPoco> { bool Delete(TPoco item); }
    interface IRetrievable<TKey, TPoco> { TPoco Retrieve(TKey input); }
}

This sample program represents the interfaces my actual program uses, and demonstrates the problem I’m having (commented out in FakeRepository). I would like for this method call to be generically handled by the base class (which in my real example is able to handle 95% of the cases given to it), allowing for overrides in the child classes by specifying the type of TKey explicitly. It doesn’t seem to matter what parameter constraints I use for the IRetrievable, I can never get the method call to fall through to the base class.

Also, if anyone can see an alternate way to implement this kind of behavior and get the result I’m ultimately looking for, I would be very interested to see it.

Thoughts?

  • 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-13T15:29:32+00:00Added an answer on June 13, 2026 at 3:29 pm

    That code doesn’t compile for the same reason this simpler example doesn’t compile:

    public interface IBar
    {
        void Foo(int i);
    }
    
    public class Bar : IBar
    {
        public void Foo<T>(T i)
        {
        }
    }
    

    The methods simply don’t have the same signature. Yes, you could call someBar.Foo(5) and it would resolve T to int, but the fact remains that Foo in Bar still doesn’t have the same signature as a method that actually takes an int as a parameter.

    You can further demonstrate this by having both a non-generic and generic method in the type; this doesn’t result in an ambiguity related error:

    public class Bar : IBar
    {
        public void Foo(int i)
        {
    
        }
        public void Foo<T>(T i)
        {
        }
    }
    

    As for actually solving your problem, you could do this:

    class FakeRepository : BaseRepository<User>, ICreatable<User>, IDeletable<User>, IRetrievable<int, User>
    {
        public User Retrieve(int input)
        {
            return Retrieve<int>(input);
        }
    }
    

    This will mean that FakeRespository has both a generic and non-generic version of Retrieve, but in the end all calls are still directed to the generic version.

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

Sidebar

Related Questions

Below have a simple piece of code: using System; using System.Collections.Generic; using System.Linq; using
I run a very simple watin console application like below using System; using System.Collections.Generic;
Below I have written a sample program that I have written to learn about
I'm trying to remove some words in a string using regex using below program.
Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates
The code pasted below is a simple JSF program, with an idea of having
I have written some sample program and DLL to learn the concept of DLL
Having successfully gotten a sample program working, I'm now starting to do Real Work
I have a sample program from Aforge library. It uses a kind of logging
I am trying to make this sample program work (defn foo ([x] (foo x

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.