Sherlock
Sherlock

Reputation: 5627

What does return (Object) null mean in c#?

New to C#, and I'm reviewing some code that has the following return statement:

return (Object) null

What does this mean in C#, what will be returned ?

Thanks

Upvotes: 3

Views: 1049

Answers (3)

devrox
devrox

Reputation: 110

You are casting an Object which has a null value.

Upvotes: 1

Rawling
Rawling

Reputation: 50114

The only place I can think of where this is required is in an anonymous method where the compiler can't infer the return type.

For example,

var boxedThings = strings.Select(s =>
{
    int i;
    if (int.TryParse(s, out i))
        return i;
    double d;
    if (double.TryParse(s, out d))
        return d;
    return (object)null;
});

doesn't compile without the (object).

Upvotes: 11

Kirill Bestemyanov
Kirill Bestemyanov

Reputation: 11964

It is absolutely equal to return null

Upvotes: 6

Related Questions