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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:39:09+00:00 2026-05-10T20:39:09+00:00

In C# I sometimes wish I could make special methods for certain instantiations of

  • 0

In C# I sometimes wish I could make special methods for certain ‘instantiations’ of generic classes.

UPDATE: The following code is just a dumb example of a more abstract problem – don’t focus too much on time series, just the principles of ‘adding extra methods’ for certain T.

Example:

class Timeseries<T>  {      ...     TimeSeries<T> Slice(...) { ... } } 

In the case where T is double, I would like some extra methods, like Integrate(), Interpolate() and so on that make only sense for double, because I need to do arithmetic on them.

There are several ways to do this, but I cannot find one that I’m satisfied with.

1. Inherit into a special class

class TimeseriesDouble : Timeseries<double> {      double Interpolate(...) { ... }     ... } 

cons: TimeseriesDouble.Slice() will return a new Timeseries<double> object, now missing my special methods.

2. External methods

public static double Interpolate(Timeseries<double> ts, ...) { ... } 

cons: Breaks with OO principles. And I don’t want to put my methods away. Also, the methods might need private/protected state.

3. Extension methods

Same as 2, just with a nicer calling syntax.

4. Common base class

class TimeSeries_base { ... } class TimeSeries<T> : TimeSeries_base { .. typesafe versions of methods .. } class TimeSeriesDouble : TimeSeries_base { .. typesafe versions of methods .. } 

cons: too much duplication of things from TimeSeries_base into the two subclasses. The base class might become just a place holder for utility functions for the sub classes.

pro: I can now do things like List<TimeSeries_base> dynamically.

5. Just forget about a common class

I.e., keep Timeseries<T> and TimeseriesDouble separate in the code.

cons: Then I don’t get all the benefit of treating a TimeseriesDouble like a TimeSeries<T>, e.g. combining two timeseries with ZIP(A,B), where one happens to be of doubles.


Any other ideas? Currently, I think I like the design (1) best.

  • 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. 2026-05-10T20:39:10+00:00Added an answer on May 10, 2026 at 8:39 pm

    You could always use the self-referential generics trick:

    public class TimeSeries<T, U> where U : TimeSeries<T, U> {     U Slice(...) }  public class TimeSeriesDouble : TimeSeries<double, TimeSeriesDouble> {     ... } 

    It can get a bit brain-bending, but it can work.

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

Sidebar

Related Questions

While browsing through source code in my IDE I'll sometimes wish I could see
When developing & debugging, I sometimes wish I could write a 1-liner that dumped
I find this atrocious: std::numeric_limits<int>::max() And really wish I could just write this: int::max
Sometimes we have several classes that have some methods with the same signature, but
I've been trying out OCaml. Sometimes its quicker just to test out some code
I develop in C++, and sometimes I wish I could say something like this:
Sometimes i face the following problem : string txt = con.Request.Params[Par_name].ToString();//the original par value
We have following problem. Developers frequently need to make small changes to our web
I've got a date text field I wish to only sometimes attach a DatePicker
I wish I could understand how the loading of xaml files into rehosted designer

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.