Hello. I am not sure if this is the right thread to post. If not please direct me to the right one :).
I have following incorrect compiler behavior in VS2005/C#
following property implementation produce compilation error:
class A
{
private bool flag;
...
public DateTime MyTime
{
get { return flag DateTime.Now : null; }
}
while the following code does not:
class A
{
private bool flag;
...
public DateTime MyTime
{
get { if (flag) return DateTime.Now else null; }
}

Compiler error
moff
This gives you error because your are returning null in one of 2 cases.
This doesnot gives you any error becuase you are not retuning null in any case even in false because you are not using the return keyword.
Reason: DateTime is a Structure and in C# structures are value types and Classes are Reference type by default in C#. A Structure can never have a null value unlike classes and to initilize a structure you dont need to use new keyword becuse its already instantiated to it default value when you declare it unlike class when you declarean object of it, it has a null refence until you use new keyword to instantiate it.
I hope this is quite simple and to the point explaination of your problem!
Best Regards,
abhas
Now the code provided is designed to return a value type that can be initialized to null, via the public DateTime , which is the goal of the code...but the compiler does use that to trump the operator processing. You will have to do without the operator in this situation due to the design needed by the processing.
TopDean
get
{
if (flag)
return DateTime.Now //<--- shoud have ;
else
null; <---- should have return
}
Olavo
Please show us the error message.
--
SvenC
THE RAZI