7

In C# we can do something like this:

Int32 i = new Int32();

However the following will return null:

typeof(Int32).GetConstructor(new Type[0])

Why is this?

I checked the documentation and got no clues as to why this happens.

My results can be illustrated in the following piece of code:

using System;

public class Program
{
    public static void Main()
    {
        Int32 i = new Int32();
        Console.WriteLine(i);
        Console.WriteLine(typeof(Int32).GetConstructor(new Type[0]) == null);
    }
}

The output is :

0

True

Aelphaeis
  • 2,593
  • 3
  • 24
  • 42
  • 1
    Check out http://forums.asp.net/t/1268224.aspx?using+reflection+with+primitive+data+types+ – Alexei Levenkov Sep 15 '14 at 01:29
  • @AlexeiLevenkov that is a good read, you should have definitely posted that as an answer. – Aelphaeis Sep 15 '14 at 01:37
  • Maybe. Feel free to crafts answer from that link yourself - I'll happily upvote... Or try to search for existing answers on SO that cover it (but I have not found one...) – Alexei Levenkov Sep 15 '14 at 01:43
  • @AlexeiLevenkov I had searched prior to asking the question; however, I've crafted the answer from your link into an answer here. – Aelphaeis Sep 15 '14 at 01:56
  • [This post](http://stackoverflow.com/a/333840/3302619) contains some good information on value types and why they don't actually have constructors. This is why you're having so much trouble finding one through reflection. – OxCantEven Sep 15 '14 at 01:52

1 Answers1

6

Alexei Levenkov posted a really good answer in the comments so I've decided to take the contents and paraphrase them to answer my question. Reference to original Q & A.

Its a bit thick but here is the answer :

Structures do not necessarily have parameter-less constructors. They can have one but C# does not emit one and the compiler does not require one. The C# standard talks about all value types having "an implicit public parameter-less constructor called the default constructor" but it subsequently notes that implementations are not required to generate a constructor calls and that the calls work as if they are constructors although they are not necessarily constructors.

The reason why reflection may not find the constructor method is because it in fact does not exist. The CLR will allow you to instantiate without a constructor and zero fill the memory locations that the object contains.

Update : I wanted to note that Jon Skeet also answered a question related to this here

Community
  • 1
  • 1
Aelphaeis
  • 2,593
  • 3
  • 24
  • 42
  • Another way of looking at it is that in C#, `int` does have a default constructor, but in .Net it doesn't. – svick Sep 15 '14 at 11:20
  • +1. @Aelphaeis thanks for taking time to write up summary. Consider to accept it as an answer. And consider adding link to Jon Skeet's post found by Dex - [Why can't define default constructor for int](http://stackoverflow.com/a/333840/477420) which covers the same topic. – Alexei Levenkov Sep 15 '14 at 14:11