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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:59:22+00:00 2026-05-25T21:59:22+00:00

Given these base classes and interfaces public abstract class Statistic : Entity, IStatistic {

  • 0

Given these base classes and interfaces

public abstract class Statistic : Entity, IStatistic
{
  protected abstract IStatisticsRepository<IStatistic> Repository {get;}

...

public class AverageCheckTime : Statistic

...

public interface IStatisticsRepository<T> : IRepository<T>  where T : IStatistic

...

public interface IAverageCheckTimeRepository : IStatisticsRepository<AverageCheckTime>

...

public class AverageCheckTimeRepository : StatisticRepository<AverageCheckTime>, IAverageCheckTimeRepository

...

public class RepositoryFactory
{
   public static IAverageQueueTimeRepository AverageQueueTimeRepository 
    {
      get { return CurrentServiceLocator.GetInstance<IAverageQueueTimeRepository>(); }
    }

Why does AverageCheckTime’s implementation throw an invalid cast exception:

protected override IStatisticsRepository<IStatistic> Repository
    {
      get { return (IStatisticsRepository<IStatistic>)RepositoryFactory.AverageCheckTimeRepository; }
    }

How do I cast an instance of IAverageCheckTimeRepository as an IStatisticsRepository<IStatistic> which I assumed it already was?


OK, I’ve made these changes…which makes me wonder if I’ve gone over the top with the generics in the first place

    public interface IStatisticsHelper
      {
        void GenerateStatistics();

        List<IStatistic> BuildReport();
      }

...

    public interface IStatisticsRepository<T> : IRepository<T>, IStatisticsHelper where T : IStatistic
      {

      }

...

    public abstract class Statistic : Entity, IStatistic
      {

        protected abstract IStatisticsHelper Repository { get; }

    ...

public class AverageCheckTime : Statistic
  {
    protected override IStatisticsHelper Repository
    {
      get { return RepositoryFactory.AverageCheckTimeRepository; }
    }
  • 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-25T21:59:22+00:00Added an answer on May 25, 2026 at 9:59 pm

    No, C# 3 does not support generic variance. C# 4 does, but you would have to declare that IStatisticsRepository is covariant in T:

    public interface IStatististicsRepository<out T> : IRepository<T>
        where T : IStastistic
    

    Variance isn’t safe in general – it depends on how the generic type parameter is used. C# 4 supports both covariance and contravariance for type arguments which are reference types, but only when the generic type involved is an interface or a delegate, and only when the type parameter is used in the appropriate way within the interface/delegate.

    Without seeing the declaration for IRepository<T>, we can’t tell whether or not it’s safe. For example, if IRepository<T> contains a method like this:

    void Save(string id, T value);
    

    then it wouldn’t be safe, because you’d be able to write:

    IStatisticsRepository<IStatistic> repo = RepositoryFactory.AverageCheckTimeRepository;
    IStatistic foo = new SomeOtherStastisticType();
    repo.Save("Foo", foo);
    

    That would be trying to save a SomeOtherStatisticType value in an AverageCheckTimeRepository, which violates type safety. It’s only safe to make the interface covariant in T if values of type T only come “out” of the interface. (There are some wrinkles around exactly what that means, mind you…)

    For a lot more information on this, see Eric Lippert’s blog series on the topic.

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

Sidebar

Related Questions

This is easier to explain with an example. Given these two classes: public class
Given the following code: public interface IExample { ... } public abstract class BaseExample:
Given a base class that is inherited by plethora of derived classes, and a
This is a purely theoretical question. Given three simple classes: class Base { }
Given file names like these: /the/path/foo.txt bar.txt I hope to get: foo bar Why
Given these two queries: Select t1.id, t2.companyName from table1 t1 INNER JOIN table2 t2
Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key
What are the potential pros and cons of each of these queries given different
Given a BehaviorSubject, what is the practical difference between calling all of these different
Given the Java code below, what's the closest you could represent these two static

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.