here is this piece of coding:-
Code:
// C# 4 Wrox , Page 122
// using generic structures , using Nullable<T>
// casting Nullable<T> to T using operatoe overloads
public struct Nullable<T> where T:<struct>
{
public Nullable (T value)
{
this.hasValue = true;
this.value = value;
}
private bool hasValue;
public bool HasValue
{
get{ return hasValue;}
}
private T value
Public T value
{
get { if (!hasValue)
{throw new InvalidOperationException("no value")}
}
return value;
}
}
public static explicit operator T ( Nullable<T> value)
{
return value.Value;
public static implicit operator Nullable<T> (T value)
{
return new Nullable<T>(value);
}
public override string ToString(){
if(!HasValue)
return String.Empty;
return this.value.ToString();}
}
}
class MPEP {public static void Main(){
Nullable<int> x;
x = 4;
X+ = 3;
if(x.HasValue){
int y = x.Value;
}
x = null;
}}
here we see that a code says:-
return value.Value
q1 } the question is how does we access a property ( Value) through a variable (value)?
q2 } How does an operator onerload casts a value