C # null chain of check in method call

I am assuming the method call chaining is below.

void DoSomething()
{
    ObjectA a = CreateA();
    if (a != null)
    {
        a.Foo();
    }
}

ObjectA CreateA()
{
    ObjectB b = CreateB();
    if (b != null)
    {    
        ObjectA a = b.ToA();
        return a;
    }
    return null;
}

      

If the depth of the method call is deeper, the zero check will be more superimposed. Is there a good solution for this?

Modified

I have modified the example code. It can't solve my problem which changes CreateA to constructor. The problem is that the null check overlap is not null.

void SetImage()
{
    UISprite image = GetSprite();
    if (image  != null)
    {
        image.spriteName = "hat";
    }
}

UISprite GetSprite()
{
    UISprite image = GetComponent<UISprite>();
    if (image  != null)
    {   
        image.width = 100;
        image.height = 100;
        return image;
    }
    return null;
}

      

+3


source to share


4 answers


As of C # 6.0, you can use the Null-Conditional Operator which allows you to do odd checks:

var result = possiblyNull?.MethodThatCanReturnNull()?.SomeProperty;

      



This construct will produce a result null

if any element in the chain produces null

.

+6


source


You can do

void DoSomething()
{
    CreateA()?.Foo();
}

ObjectA CreateA()
{
    return CreateB()?.ToA();
}

      



Your other approach, if you can't use C # 6, doesn't return nulls, uses empty objects in a way that you never have to deal with null validation (but you can still check if something is a null object)

+2


source


If you are using C # 6.0 or higher you have a simple Null conditional solution for this problem.

see this link

https://msdn.microsoft.com/en-au/library/dn986595.aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=csharp#code-snippet-1

0


source


So, if you (or someone else) can't use the null-conditional operator, is there a good reason to use this pattern of methods that create objects instead of constructors that create objects? Constructors are guaranteed not to return null.

It looks like you have a conversion or nested object hierarchy, but no inherited hierarchy where you could just fall back to polymorphism. Maybe a tool like AutoMapper might be helpful for sequentially coding these ToX () methods?

I'm not sure how it would be "nested". Your CreateB () method will look exactly like your CreateA () code. You don't end up with a pyramid, just a lot of the same methods.

ObjectB CreateB()
    {
        ObjectC c = CreateC();
        if (c != null)
        {    
            ObjectB b = c.ToB();
            return b;
        }
        return null;
    }

      

Most of the time, you do this in an environment where you do not have control over all classes. In this case, the best approach is to write your own conversion functions or AutoMapper (really worth the time). But, if you have a class hierarchy, you can implement an abstract class that will do most of the heavy lifting for you. But to be honest, I would only write something like this if I had a really good reason (something more than just wanting to fuck people). I am including this to demonstrate how much easier life is if you just use a constructor that is guaranteed not to return null;

public abstract class MyAbstractObject<Tobj> where TObj: MyAbstractObject, new()
{
    public static MyAbstractObject CreateObject()
    {
        Tobj subOb = new TObj();
        MyAbstractObject parent = subOb.ToObject();
        return parent;
    }
    public virtual TObj ToObject()
    {
        return CreateObject();
    }
}

public class ObjectD : MyAbstractObject<ObjectC> { }
public class ObjectC : MyAbstractObject<ObjectB> { }
public class ObjectB : MyAbstractObject<ObjectA> { }
public class ObjectA : MyAbstractObject<ObjectA>
{
    public override TObj ToObject()
    {
        return this;
    }
}

static void Main()
{
    ObjectA a = ObjectD.CreateObject();
}

      

0


source







All Articles