-1

When I inspect my object in the debugger, it has 3 different entries for the property VgnItm.

enter image description here

How does an object get multiple of a property?

Here is the class with multiple of VgnItm property:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Nest;
using Newtonsoft.Json;

namespace Vepo.Domain
{
    [Serializable]
    public class VgnItmEstDto : CreatedBySomeoneDto
    {
        [Required]
        public int VgnItmId { get; set; }
        public int? EstId { get; set; }
        [Required]
        public int NotInEstCount { get; set; } = 0;
        [Required]
        public int InEstCount { get; set; } = 0;
        public double? Price { get; set; }
        [Object]
        public EstDto Est { get; set; }
        public VgnItmDto VgnItm { get; set; }
        public string Discriminator { get; set; }
    }

    public class VgnItmEstDto<TVgnItmDto> : VgnItmEstDto
        where TVgnItmDto : VgnItmDto
    {
        public new TVgnItmDto VgnItm { get; set; }
    }
}
BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
  • 2
    Please show a [mcve]. – Sweeper Apr 28 '23 at 09:51
  • 3
    A likely reason for the duplicate property names is that you redeclared the property in an inherited class, creating another property that has the same name. – Sweeper Apr 28 '23 at 09:53
  • Don't you know the [new modifier](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/new-modifier)? – shingo Apr 28 '23 at 09:56
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Palle Due Apr 28 '23 at 10:06
  • @PalleDue No, this is a completely different question. That is just a plain old null reference exception question. – BeniaminoBaggins Apr 28 '23 at 10:14
  • @MakePeaceGreatAgain So there is no place on the internet for a question about a C# object having the same property multiple times? – BeniaminoBaggins Apr 28 '23 at 10:20

1 Answers1

1

Hiding a property with the new keyword doesn't mean that that property ceases to exist. Of course there are two properties with the same name: the one you declared and the one you hid. If you want to hide the base property with one of a more specific type then you would usually implement the getter and setter to pass through to the base property:

public class VgnItmEstDto<TVgnItmDto> : VgnItmEstDto
    where TVgnItmDto : VgnItmDto
{
    public new TVgnItmDto VgnItm
    {
        get => base.VgnItm;
        set => base.VgnItm = value;
    }
}
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • redirecting the new property to the hidden one won't change anything on serialization. The property will still occur twice. – MakePeaceGreatAgain Apr 28 '23 at 10:09
  • @MakePeaceGreatAgain, it was my impression that the `NullReferenceException` was because the base property was not being set when setting the new property. This will address that. – jmcilhinney Apr 28 '23 at 10:11