Evgeni Velikov
Evgeni Velikov

Reputation: 382

When we use new keyword for create new DateTime (object)

What is the different between this

DateTime thisTime = new DateTime();

and this

DateTime thisTime;

Who is the better performance, and who is the better practice for used. For beginning when is create is have the same date times. I used thisTime to add dateTime from file who has 8000 rows, with method ParseExact().

Upvotes: 2

Views: 1040

Answers (2)

Gabriel Sadaka
Gabriel Sadaka

Reputation: 1746

There is no difference they both return the default value of DateTime in C# which is 1/1/0001 12:00:00 AM and performance wise they are both the same. I would recommend the second option as it uses less code.

In C# DateTime is a struct not a class therefore it can't be null and will always default to 1/1/0001 12:00:00 AM regardless of how you declare it.

The different ways are: DateTime thisTime;, DateTime thisTime = new DateTime(); and DateTime thisTime = default(DateTime);

Also instead of using ParseExact I would recommend using TryParseExact so the code doesn't throw an exception if the string doesn't match the format.

DateTime thisTime;
if (DateTime.TryParseExact("Inputted Date", "Date Format", 
                System.Globalization.CultureInfo.InvariantCulture,
                DateTimeStyles.None, out thisTime))
{
    //success code
}
else
{
    //failure code
}

Upvotes: 2

poke
poke

Reputation: 387825

I’m going to explain this with a different type first, you’ll see why later.

string myString;

This is a variable declaration. It allocates some space in the memory to be able to hold string objects. It does not however fill that space with any content. So the variable contains null which essentially means that there is no object.

myString = "foo";

This is a variable assignment. It fills the memory allocated before with some value. That value is a reference to the string object which contains "foo". That means that the memory location that was prepared for the variable does not contain the string itself, but just a note where else to find it.

string myString = "foo";

This combines both above and initializes the declared variable directly.


Now, for DateTime this is a bit different. DateTime is a value type, that means that variables of that type do not only store a reference but instead the actual content of the object. Another simple type is an int where the variable just stores the integer value directly.

Since variables of value types contain the value directly, they are not references, and as such they cannot be null. Instead, declared variables of value types will be automatically initialized with the default value for that type.

In general, this is achieved by using default(TypeName), e.g. default(DateTime) (this also works for reference types, e.g. default(string) which returns null). And for value types, default(DateTime) happens to be the same as new DateTime().

However, the C# compiler will usually enforce you to explicitely initialize variables before you use them. So even when technically, the memory would be prefilled with the default value, the compiler will complain, if you don’t explicitely set a value.

So if you want to use thisTime, you will have to assign a value to it. But you do not have to assign the default value to it, if you plan to assign a completely different value anyway. So if you e.g. plan to use ParseExact later to get a DateTime, then just leave the variable uninitialized until you assign it.

E.g.

// do this
DateTime thisTime;
thisTime = DateTime.ParseExact(…);

// but not this
DateTime thisTime = new DateTime(); // or default(DateTime)
thisTime = DateTime.ParseExact(…);

Upvotes: 4

Related Questions