[Solved] C# Constructor cannot call itself


You have a constructor that takes an IDbTransaction and a Action<UnitofWork> and starts by calling the constructor which takes an IDbTransaction and a Action<UnitofWork>, which is to say itself.

That would then immediately call itself, then immediately call itself, then immediately call itself…

If it was allowed, then one of two things would happen:

  1. It would keep doing these calls to itself until there was a StackOverflowException that crashed the application.

  2. The use of the stack was optimised away, so it just called itself forever in an infinite loop.

Either way, it would be pointless, so it’s obviously wrong, so it’s good for the compiler to not allow you to do so.

Instead a constructor must either:

  1. Use this to call a different constructor on the same type.
  2. Use base to call a constructor on the base type.
  3. Use neither (which is the same as using base() to call a parameterless constructor on the base type.

2

solved C# Constructor cannot call itself