I’m currently tempted to write the following:
public class Class1()
{
public Class1()
{
MyProperty = new Class2(this);
}
public Class2 MyProperty { get; private set; }
}
public class Class2()
{
public Class2(Class1 class1)
{
ParentClass1 = class1;
}
public Class1 ParentClass1 { get; set; }
}
Is passing “this” as an argument a sign of a design problem? What would be a better approach?
No, there’s no fundamental design problem with passing
this. Obviously, this can be misused (creating coupling that’s too tight by having a related class depend on values stored in your instance when values of its own would be called for, for example), but there’s no general problem with it.