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

The Archive Base Latest Questions

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

I am just begining to explore the basic of using generics and would have

  • 0

I am just begining to explore the basic of using generics and would have thought i could implement this simple pattern to solve a typical problem in my daily use. I have spent days searching for a simple example. I can find examples for looking for .Equals, but not much past that. I want to be able to instantiate with things like:

Spec<double> voltageSpec;
Spec<int> cyclesSpec;
Spec<myClass> fishInTheOceanSpec;

then be able to :

bool isGood = voltageSpec.inSpec(5.0);
bool isGood cyclesSpec.inSpec(2);
bool isGood fishInTheOceanSpec.( new myClass(20));

My attempt is shown below.

/// <summary>
///  Generic object to hold a specification i.e min and max limits.
///  Implements a method to determin if a value is between limits.
/// </summary>
public class Spec<T> : IComparer<T> 
{
    public Spec()
    {
        Min = default(T);
        Max = default(T);
    }
    public T Min { get; set; }
    public T Max { get; set; }
    public bool inSpec(T Value) 
    {
        if ((Comparer<T>.Default.Compare(Value, this.Max) <= 0) &
            (Comparer<T>.Default.Compare(Value, this.Min) >= 0))
            return true;
        else 
            return false;
    }

    public int Compare(T x, T y)
    {
        if (x == y) return 0;
        if (x < y) return -1;
        if (x > y) return 1;
    }

    public Spec<T> Copy()
    {
        return (Spec<T>)this.MemberwiseClone();
    }
}
  • 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:40:46+00:00Added an answer on May 31, 2026 at 12:40 am

    I would refactor as follows – make T be responsible to provide the comparison – this works already for primitive types, your custom classes just have to implement IComparable<T>:

    public class Spec<T> where T : IComparable<T>
    {
        public Spec()
        {
            Min = default(T);
            Max = default(T);
        }
        public T Min { get; set; }
        public T Max { get; set; }
        public bool inSpec(T Value)
        {
            if(Value.CompareTo(this.Max) <=0 &&
                Value.CompareTo(this.Min) >=0)
                return true;
            else
                return false;
        }
    
        public Spec<T> Copy()
        {
            return (Spec<T>)this.MemberwiseClone();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this WP Plugin that I want to edit, but I just can't
Inside the begining of a function I have this: if(false); { return 'TRUE'; }
I've just purchased a Mac and am beginning to explore software development using Cocoa
I am just begining php and mysql. I have two queries from my database.
I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;
Just beginning to learn about structs, I thought I understood how they work, using
I'm just beginning to have a look at Objective-C and Cocoa with a view
I am just beginning to do research into the feasibility of using Amazon's SimpleDB
I'm just beginning to use Regex so bear with my terminology. I have a
I am just beginning to get into desktop application development, and have chosen C#

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.