C# - Delete ChildObject from ParentObject

Posted on 2012-08-16
Last Modified: 2012-08-17
Hi Experts,

I have a class as below

Class person
   public string Name{get;set;}
   public  person child{get;set;}
i need to delete child  from person.
i have used syntax as below
person p  = new person();

// based on certain condition  i am deleting as below
p.child = null;

Is this the right Approach or do we have an other Options to delete child object. Please advice.
Question by:B_Pati
    LVL 8

    Expert Comment

    I believe your code is fine. Setting the value to NULL instead of string.Empty is better because then the variable does not have any value in the memory. In other words it doesn't exist.
    LVL 85

    Accepted Solution

    That looks fine to me as well.

    If you need something else to occur when the child gets deleted, though, then you can add code in the "set" portion of the property:
        public class person
            public string Name{get;set;}
            private person _child;
            public person child
                get { return _child; }
                    if (value == null)
                        if (_child != null)
                            // ... do something with existing child before deletion ...
                            Console.WriteLine("Before Child Deletion");
                    _child = value;

    Open in new window

    What if a person has more than one child though?  You might want a List<person> to represent children instead...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now