Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible in C# to force a private function to be callable from the constructor only?

I find that in some cases, there is a lot of code in a constructor, or a class has two or more constructors which have comparable code. In these cases, I often create a private method. In the former case to improve readability, in the latter to prevent duplicate code.

In some of these cases this results in a private method that should ONLY be called from the constructor (for whatever reason). Is there a way to enforce this? I could imagine doing something like this:

using System.Diagnostics;

public class Foo
{
  private bool _constructing = true;

  private Foo()
  {
    _constructing = false;
  }

  public Foo(string someString) : this()
  {
    // constructor-specific code
    Initialize();
  }

  public Foo(double someDouble) : this()
  {
    // constructor-specific code
    Initialize();
  }

  private void Initialize()
  {
    Debug.Assert(!_constructing, "Initialize method should only be called from constructor");

    // shared code
  }
}

but this feels somewhat clunky. Does anyone have a better suggestion?

Edit: added constructor chaining to example; I meant for this to be in the original example.

Edit: I think I missed a point in my original question - while chaining constructors does provide a solution in some cases, the chained code is always executed prior to the code in the constructor that you're chaining from (which, incidentally, is why the above example doesn't work). There are cases where you want to execute some part of shared code, and then do something else. I'll add another example to reflect this:

using System.Diagnostics;

public class Foo
{
  private bool _constructing = true;

  public Foo(string someString)
  {
    // constructor-specific pre-processing code
    Initialize();
    // constructor-specific post-processing code

    _constructing = false;
  }

  public Foo(double someDouble)
  {
    // constructor-specific pre-processing code
    Initialize();
    // constructor-specific post-processing code

    _constructing = false;
  }

  private void Initialize()
  {
    Debug.Assert(!_constructing, "Initialize method should only be called from constructor");

    // shared code
  }
}
like image 856
MHJF Avatar asked Jan 14 '15 10:01

MHJF


People also ask

Is inheritance is possible in C?

C is not an Object Oriented language. Inheritance is a property of Object Oriented languages. There is no Compiler-level support for inheritance in C.

What does |= mean in C?

The ' |= ' symbol is the bitwise OR assignment operator.

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


1 Answers

Constructors can call each other:

class Foo
{
    private Foo()
    {

    }

    public Foo(int value) : this()
    {

    }
}

I think, you could use this feature.

like image 161
Mark Shevchenko Avatar answered Sep 21 '22 16:09

Mark Shevchenko