Tricky Q on value types & ref types.. guess o/p | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Tricky Q on value types & ref types.. guess o/p

  class Program { static String theLocation; static DateTime theTime; static void Main() { Console.WriteLine(theLocation == null ? "theLocation is null" : theLocation); Console.WriteLine(theTime == null ? "theTime is null" : theTime.ToString()); } }  

4th Jul 2017, 11:54 AM
Guru Giri
Guru Giri - avatar
9 Answers
+ 2
What is your question ?
4th Jul 2017, 12:19 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
what will be the output and its explanation
4th Jul 2017, 12:25 PM
Guru Giri
Guru Giri - avatar
+ 2
The output will be : theLocation is null theTime is null It will be like this because you did not initialize them and C# use null as default value for classes instance.
4th Jul 2017, 12:30 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
@prunier there will default date time in output as datetime value types try to run that code
4th Jul 2017, 12:34 PM
Guru Giri
Guru Giri - avatar
+ 2
theTime is a value type. As such, an uninitialized DateTime. variable is set to the default value of midnight on 1/1/1. the year 1 A.D
4th Jul 2017, 1:35 PM
Guru Giri
Guru Giri - avatar
+ 2
because for that i was confused i was searching its reason so i put this w simultaneously here too
4th Jul 2017, 1:42 PM
Guru Giri
Guru Giri - avatar
+ 2
Okay, I hope I helped even if my answer had mistakes :)
4th Jul 2017, 1:43 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
I tried and it seems so, I did not know that :)
4th Jul 2017, 12:46 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Yeah, I know that value type have default values other than null. But if you know the answer, then why did you ask the question in the first time ? ^^
4th Jul 2017, 1:41 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar