C# - using “this” as a parameter to initialize a child class

I want to know that using "this" as a parameter to initialize a child class is a good idea or not? If I want to ask it better, I should ask that in OOP world it's a good idea to access a parent class variable from a child class or not?

Thanks

Answers


It seems that you are planning on doing something like this:

public class MyClass
{
    private OtherClass _someProperty;
    public OtherClass SomeProperty 
    {
        get
        { 
             if (this._someProperty == null) this._someProperty = new OtherClass(this);
             return this._someProperty;
        }
    }
}

public class OtherClass
{
    private MyClass _parent;
    public OtherClass(MyClass parent)
    {
        this._parent = parent;
    }
}

This is quite standard OOP approach. The one thing you should remember for more advanced scenarios is that now you have a cyclic reference between these two objects and Garbage Collector will not be able to collect either of them while one is still referenced.


Need Your Help

How to retrieve the chosen order attribute on category page in magento?

magento

Been googling for a while now, but couldn't find a solution for this - i think - kinda trivial question. I'd like to retrieve the attribute by which a product catalog is ordered. I don't want to be...