Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
NULL validation for null object patterns and extension methods
Namespace Chap2_3
{
public class Nullobject
{
public void Test ()
{
Promation promation = promationfactory.create ("fruit promotion");
Promation promation1 = promationfactory.create (null);
if (promation. IsNullOrEmpty ())
{
Extension method, judged to be null
Console.WriteLine ("Promation is null");
}
Else
{
Console.WriteLine ("Promation is not null");
}
if (promation. IsNull)
{
Console.WriteLine ("Promation is null");
}
Else
{
Console.WriteLine ("Promation is not null");
}
if (Promation1. IsNull)
{
Console.WriteLine ("Promation1 is null");
}
Else
{
Console.WriteLine ("Promation1 is not null");
}
Console.readkey ();
}
}
public class Promation
{
Public virtual BOOL IsNull
{
get {return false;}
}
}
public class Nullpromation:promation
{
public override bool IsNull
{
get {return true;}
}
}
public class Promationfactory
{
public static Promation Create (String promationname)
{
if (string. IsNullOrEmpty (Promationname))
{
return new Nullpromation ();
}
Else
{
return new Promation ();
}
}
}
Extension methods
public static Class Objectisnullorempty
{
public static bool IsNullOrEmpty (This object obj)
{
return obj = = null? True:false;
}
}
}
NULL validation for null object patterns and extension methods