1

I have a Unity game using WebGL and c#.

I am running Unity 2020.2.6f1, from what I understand is using c# 7.3

I currently check for null or empty arrays like this:

ObjectType[] myObjectTypeList = FindObjectsOfType<ObjectType>();

if(myObjectTypeList != null && myObjectTypeList.Length > 0)
{
    ...
}

But I read that in c# 7+ you can shorten to this:

if(!(myObjectTypeList?.Length != 0))
{
    ...
}

Is this something I can do in a Unity c# script when I am using FindObjectsOfType?

Thanks!

3 Answers 3

2

In C#7 C# 8.0 you can use the is operator and, your if statement could be

if (myObjectTypeList is {Length: > 0}){
    // [...]
}

which is equal to

if (myObjectTypeList != null && myObjectTypeList.Length > 0){
    // [...]
}

Edit

This answer is wrong.

According to Microsoft's Documentation:

Beginning with C# 8.0, you use a property pattern to match an expression's properties or fields against nested patterns [...]

This feature works only from c#8.0 and later. That said, this answer will still work, since Unity Version 2020.2 uses Roslyn Compiler with C# 8.0 Language Support.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh I see, so that notation checks for not null and length together?
Exactly. But, as per my edit, my answer is wrong.
Funnily enough, it seems like my answer will still work, as, your Unity Version indicates that it uses C# 8.0
Additional information that might also be usefull about is operator: This operator can do multiple things at the same time. Want to typecheck, cast, validate a member value and use an object at the same time? for: object myObjectTypeListAsObject could work like if (myObjectTypeListAsObject is ObjectType[] {Length: > 0} myObjectTypeList) { /*stuff with myObjectTypeList*/ }
2

The ?. is called the Null Conditional Operator. a?.b will resolve to b, unless a is null, in which case it resolves to null. This operator was actually new since C# 6.0.

2 Comments

Ok so I need to check and make sure the array is both not null and also not empty so I don't get null reference errors in Unity. would that still work? thanks!
@SkyeBoniwell - Yes
2

If you can live with the warnings you will get about calling an extension method on a null, this should work:

public static class Extensions
{
    public static bool NotNullHasItems<T> (this IEnumerable<T> collection)
    {
        if (collection == null)
        {
            return false;
        }
        return collection.Any();
    }
}

To test this, I used:

List<string> stuff = null;
var shouldBeFalse = stuff.NotNullHasItems();
stuff = new List<string>();
var shouldAlsoBeFalse = stuff.NotNullHasItems();
stuff.Add("test");
var shouldBeTrue = stuff.NotNullHasItems(); 

and both shouldBeFalse and shouldAlsoBeFalse ended up false while shouldBeTrue was true.

You may want a better name (perhaps reverse the logic and name it IsNullOrEmpty to match string.IsNullOrEmpty)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.