There is somethimes need to have instance of class no matter if valid but not null
public static T Safe<T>(this T obj) where T : new()
{
if (obj == null)
{
obj = new T();
}
return obj;
}
usage will be like:
MyClass myClass = Provider.GetSomeResult();
string temp = myClass.Safe().SomeValue;
instead of:
MyClass myClass = Provider.GetSomeResult();
string temp = "some default value";
if (myClass != null)
{
temp = myClass.SomeValue;
}
sorry if it is a duplicity, but I dont find it.