-3

I'm getting some strange errors trying to make use of a vendor dll written probably in another language, and it made me wonder. For a non static class constructor, at what moment is its constructor called. Does this happen as soon as new is called (i used to think this).

public void initTool()
{ 
    vendorClass x = new vendorClass()
    int i =0;
    i++; ....

Or might it perhaps happen earlier in the code like in the main form type definitions, where x could be defined as that vendor class (thus defining something would trigger constructor code parts to run on beforehand). Perhaps a result of jit optimization or so

public partial class Form1 : Form
{  
    vendorClass x;  // or vendorClass x=null;
    int i; ...

MSDN seams to say that "new" keyword is required in case parameters should be passed into the constructor. However what if it doesnt need that?, in such a case will a dll class constructor be called at the moment of reserving it?.

Note this vendorClass.dll is not in C# written in C++ or plain C, and i dont have the code of it

Peter
  • 2,043
  • 1
  • 21
  • 45
  • "Does this happen as soon as new is called" Yeap. However you could have easily tried this out yourself or simply look into the [docs](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/instance-constructors): "Instance constructors are used to create and initialize any instance member variables when you use the **new** expression to create an object of a class. " – MakePeaceGreatAgain Jun 20 '18 at 15:04
  • 1
    Do you have any specific reason to back up your suspicion that the constructor is called at some time other than when you `new()` an object up? Have you tried recreating the test in your own code, using breakpoints to test? Create a class `Foo` and an empty constructor `public Foo() { /* put some no-op code here so you can breakpoint it*/ }` and then in your `main` class, try declaring it both ways and see what happens. – Carl Bussema Jun 20 '18 at 15:06
  • 1
    It isn't legal for the compiler to run any part of any instance constructor before the `new` (well, not in any way that can be observed, at least), nor does it have any motivation to do so. For `static` constructors the story is completely different, of course. – Jeroen Mostert Jun 20 '18 at 15:11
  • Btw.: **Declaring** a variable (in your case by writing `vendorClass x`) does not "reserve" anything, only **instantiating** does (which is achieved by calling `new ...`). – MakePeaceGreatAgain Jun 20 '18 at 15:11
  • @carl Bussema, i dont have the code of this dll, so i cannot insert some test in it. I assume it was written in C, maybe C++ but not in C# (i need to work with buffers and pointers a lot to get it to work) the existence of some types made me wonder if got initialized earlier, that would explain a few things, it could also be that the early initialization has a cause in related in other structs or classes, offered by this dll. – Peter Jun 21 '18 at 06:15
  • You could still author your own C or C++ class/struct and try it with breakpoints to see what happens. – Carl Bussema Jun 21 '18 at 15:56
  • if i was a naked c programmer, yes, i'm into c# – Peter Jun 22 '18 at 06:17

1 Answers1

1

If vendorClass is type of class:

vendorClass x; // constructor is not called, variable x is uninitialized

vendorClass x = new vendorClass(); //constructor is called, x != null

There is a difference with struct type:

vendorClass x; //  all members are default-initialized

vendorClass x = new vendorClass(); // the same
Backs
  • 24,430
  • 5
  • 58
  • 85
  • After `vendorClass x;` the variable `x` is [_uninitialized_](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/variables#definite-assignment), **not** `null`. And when `vendorClass` is actually a `struct`, it's not quite accurate to say a "default constructor without parameters [is] called"; [structs cannot have parameterless constructors](https://stackoverflow.com/questions/333829) and when constructed this way [all members are default-initialized](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-structs). – TypeIA Jun 20 '18 at 15:44