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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:31:31+00:00 2026-06-01T07:31:31+00:00

I have quite a few problems, probably misunderstandings, about the new keyword with properties

  • 0

I have quite a few problems, probably misunderstandings, about the new keyword with properties in derived classes. Most of the documentation deals with new on derived methods. For instance:

[DataContract]
[KnownType(typeof(PostCategory))]
public class BaseCategory : IEquatable<BaseCategory>
{
//...

    /// <summary>
    /// Enables access to child categories (immediate ones). Filled in by data access provider. Not serializable.
    /// </summary>
    public SortedDictionary<string, BaseCategory> Children { get; set; }
//...
}

[DataContract]
[KnownType(typeof(BaseCategory))]
public class PostCategory : BaseCategory
{
//...
    public new SortedDictionary<string, PostCategory> Children { get; set; }
//...
}

Then in my data repository implementation I have a generic implemenation for all the classes that derive from BaseCategory. I do this:

    public class CachedXmlCategorizationProvider<T> : ICategorizationRepository<T> where T : BaseCategory, new()
    {
        private readonly ICacheProvider _cacheProvider = AppServices.Cache;
        private readonly string _file = Path.Combine(XmlProvider.DataStorePhysicalPath, typeof (T).Name, "categorization.xml");
        private SortedDictionary<string, T> _dictionary;

    //...
        private SortedDictionary<string, T> LoadFromDatastore()
        {
            if (!DefaultsExist())
            {
                return CreateDefaults();
            }

            var dcs = new DataContractSerializer(typeof (SortedDictionary<string, T>));
            XmlDictionaryReader reader =
                XmlDictionaryReader.CreateTextReader(new FileStream(_file, FileMode.Open, FileAccess.Read),
                                                     new XmlDictionaryReaderQuotas());

            _dictionary = (SortedDictionary<string, T>) dcs.ReadObject(reader, true);
/* HERE! Apparently this fills in the Children property of the base class */
            // fill in Children
            foreach (var pair in _dictionary)
            {
                pair.Value.Children = GetChildren(pair.Value.Id);
            }
            reader.Close();
            return _dictionary;
        }
    //...
    }

and when I try to access the Children property of a specific PostCategory instance at runtime it is null and the base.Children is set to the correct dictionary of Children as set by the GetChildren() method.

The thing is, my code relies on checking the PostCategory.Children instance and because it’s null my code is failing.

What is the correct way to achieve what I want (have one base class and change the type of a property in derived class)?

  • 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-06-01T07:31:32+00:00Added an answer on June 1, 2026 at 7:31 am

    Inheritance for the new keyword does not function in the same manner as virtual override members, in that it does not function at all. If you access the member via a reference to a base class, the base class’s member is accessed and the hidden member is completely bypassed. In the case of proper inheritance, this sort of call would go down the derived classes had any of them overridden the base member.

    If you want proper behaviour you will need to make the base property virtual and override it in a derived class.

    It appears as though you are trying to make the derived type more specific. This could possibly be achieved using interfaces and co-variance.

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

Sidebar

Related Questions

I'm currently using QDataStream to serialize my classes. I have quite a few number
After migrating from XCODE 4 to 4.2 i have quite a few problems with
I have quite a few buttons in my application, they vary in sizes based
I have quite a few situations where I have database structures similar to: TABLE
I have quite a few dictionaries where the key is a composite of several
I have quite a few libraries and models that get loaded into a controller,
In our code we have quite a few cases of this pattern: class outerClass
A question that seems to have quite a few options for Python, but none
I have tried quite a few codes but none of them seem to work
I have written quite a few Ruby programs, but if I need to show

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.