Let's say that I have a class (ClassA
) containing one method which calls the constructor of another class, like this:
public class ClassA
{
public void CallClassBConstructor()
{
using(ClassB myB = new ClassB()) {...}
}
}
The class ClassB
looks like this:
public class ClassB : IDisposable
{
public ClassB(){}
public ClassB(string myString){}
public ClassB(ClassC myC){}
public void Dispose() {...}
}
...and ClassC
is even more simple:
public class ClassC{}
If I put these classes in their own assembly and compile the whole solution I don't get any errors. But if I replace the using statement with this:
using(ClassB myB = new ClassB("mystring")){...}
I get a compile error asking me to add a reference to [mynamespace].ClassC
in ClassA
. Since I'm not calling ClassB(ClassC myC)
at all this makes no sense to me - why do I have to include the types of other constructors regardless of whether I use them or not? What if ClassC
was included in a licensed or hard-to-aquire assembly? Is this an example of bad design that developers should avoid or am I missing something here?
using
statement is not enough for the compiler to know which overload to use? I'm confused that simply adding the reference makes the compiler happy. – Cahra