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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:48:09+00:00 2026-05-20T10:48:09+00:00

I have a custom class that is basically a SortedList with a few extra

  • 0

I have a custom class that is basically a SortedList with a few extra properties and methods. I would like to do some additional processing when a new key/value pair is added (i.e when the .Add method is called). I can hide the .Add method or use another method name (ex:.AddPair) and then call base.Add in that method. Preferred approach? Why?

Hide .Add method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;    
namespace Inheritence_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            DYseries d = new DYseries() { FieldName = "test" };
            d.Add(new DateTime(2010, 12, 1), 2345);
            d.Add(new DateTime(2010, 12, 5), 2340);
            d.Add(new DateTime(2010, 12, 2), 2343);
            Console.WriteLine("fieldName {0} \n count {1} \n max {2} \n min {3}", d.FieldName, d.Count(), d.Keys.Max(), d.Keys.Min());
        }
    }
    class DYseries : SortedList<DateTime, double>
    {
        public string FieldName { get; set; }
        new public void Add(DateTime date, double value)
        {
            base.Add(date,value);
            // additional processing here
            Console.WriteLine("Added date {0}.\n   Max date: {1}",date, this.Keys.Max());
        }

    }
}

or

using another method name:

class Program
    {
        static void Main(string[] args)
        {
            DYseries d = new DYseries() { FieldName = "test" };
            d.AddPair(new DateTime(2010, 12, 1), 2345);
            d.AddPair(new DateTime(2010, 12, 5), 2340);
            d.AddPair(new DateTime(2010, 12, 2), 2343);
            d.AddPair(new DateTime(2010, 12, 9), 2348);
            Console.WriteLine("fieldName {0} \n count {1} \n max {2} \n min {3}", d.FieldName, d.Count(), d.Keys.Max(), d.Keys.Min());
        }
    }
    class DYseries : SortedList<DateTime, double>
    {
        public string FieldName { get; set; }
        public void AddPair(DateTime date, double value)
        {
            base.Add(date,value);
            // additional processing here
            Console.WriteLine("Added date {0}.\n   Max date: {1}",date, this.Keys.Max());
        }

    }

Is there a preferred approach? Will one approach (hiding?) potentially cause problems?

  • 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-20T10:48:10+00:00Added an answer on May 20, 2026 at 10:48 am

    Use the second approach. The first breaks good OO design, and you’re not going to be sure if your method will be called or the base class. Consider this use of your class:

    SortedList<DateTime, double> myList = new DYseries();
    myList.Add(date, value);  // This will call the base, not your implementation!
    

    I’ve never come across a valid reason to use new; there are always other ways to acomplish what you want without breaking good OO design.

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

Sidebar

Related Questions

Basically I have a custom List class that contains different fruits. Assume that each
I have a custom class that implements that IComparable. This class is stored in
I have a custom class that implements ICollection , and this class is readonly,
I have a custom class loader so that a desktop application can dynamically start
I have a string variable that represents the name of a custom class. Example:
I have a std::multimap where key is a custom class. Something like this: Class
I have a custom class that has as an instance variable of a coordinate:
I have a java class that looks like public class MyClass { private final
I have a custom class that extends List which I am using as a
I have a class that inherits from a dictionary in order to add some

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.