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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:36:45+00:00 2026-05-31T00:36:45+00:00

I always run in circles with this problem. i wrote code that uses the

  • 0

I always run in circles with this problem. i wrote code that uses the following interface:

public interface IStorage
{
    T GetElement<T>(String id) where T : class;
    void SetElement<T>(String id, T element) where T : class;
}

But i am just not able to write generic get and set functions. I always end up trying to access dictionaries but all my attempts never even compile. I am mising something huge here, what is it?

  • 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-31T00:36:47+00:00Added an answer on May 31, 2026 at 12:36 am

    The way you defined the interface, method invocation on an interface implementation requires specification of the base type <T>. At that point, you might as well be casting.

    [Edited to note] Also, the base class for a generic method is scoped to the individual method, not to the class/interface, which is probably what was causing your problems.

    First off, I’d simplify your interface to something like this:

    public interface IStorage<T> where T : class
    {
      T this[ string id ] { get ; set ; }
    }
    

    Then I’d implement it something like:

    public class Repository : IStorage<Widget>
    {
      private IDictionary<string,Widget> backingStore = new Dictionary<string,Widget>() ;
    
      public Widget this[string id]
      {
      get
        {
          Widget instance ;
          bool   exists   = backingStore.TryGetValue(id, out instance ) ;
          return instance ; // null if the dictionary doesn't contain the key
        }
      set
        {
          if ( string.IsNullOrWhiteSpace(id) ) throw new ArgumentException("id") ;
          if ( value == null                 ) throw new ArgumentNullException("value") ;
          backingStore.Add( id , value ) ;
          return ;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always run into the same problem when creating web pages. When I add
I have a WPF application that will always run on windows 7, it opens
Question Is there a way to have a method that will always run anytime
We have a system that runs in IIS. The system should always run using
When I run rake , it always starts with a message like this: >
When I run the following script, the event always fires on page load. I
I always run into the following error when trying to read a ByteString: Prelude.read:
I'm trying to always run some code after .find or .where or whatever is
Possible Duplicate: Does a finally block always run? I learned that the finally clause
I need to let a piece of code always run independently of other code.

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.