I want to know how to change the execution order when creating a chain in C #. The only methods I've seen require the called constructor to be called first, outside the current constructor.
In particular, take the following example:
public class Foo {
private static Dictionary<string, Thing> ThingCache = new Dictionary<string, Thing>();
private Thing myThing;
public Foo(string name) {
doSomeStuff();
if (ThingCache.ContainsKey(name)) {
myThing = ThingCache[name];
} else {
myThing = ExternalStaticFactory.GetThing(name);
ThingCache.Add(name, myThing);
}
doSomeOtherStuff();
}
public Foo(Thing tmpThing) {
doSomeStuff();
myThing = tmpThing;
doSomeOtherStuff();
}
}
Ideally, I would like to reduce code repetition by doing this (note, I acknowledge that in this contrived example, not a lot of code is saved, but I work with code that will do much more good. I use this example for clarity):
public class Foo {
private static Dictionary<string, Thing> ThingCache = new Dictionary<string, Thing>();
private Thing myThing;
public Foo(string name) {
if (ThingCache.ContainsKey(name)) {
this(ThingCache[name]);
} else {
this(ExternalStaticFactory.GetThing(name));
ThingCache.Add(name, myThing);
}
}
public Foo(Thing tmpThing) {
doSomeStuff();
myThing = tmpThing;
doSomeOtherStuff();
}
}
This is possible in VB.Net, but C # does not allow me to call the constructor in the middle of another constructor - only at the beginning, using the syntax Foo (): this ().
, : , , ?