Search code examples
c#defaultinstantiation

Does default keyword create new instance?


if(new DateTime() == default(DateTime))
{
  //blah blah...
}

Code 'new DateTime()' will create new instance and take some memory for itself.

I want to know is Code 'default(DateTime)' also create new instance and take some memory or just compare and then end


Solution

  • As stated here, it creates a new default value. So its same memory allocation as it happens for value type and reference type. First one on stack second on heap

    https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/default#default-literal