0

What is the difference between object x = null and object x = (object) null in C#?

Edit - I haven't personally used this code. I wanted to know how MVC route handling works so opened this to understand it and found (object) null used in it. so want to know the difference.

4
  • Have you noticed any difference when you run both code? Commented Feb 12, 2016 at 23:34
  • Eser I edited the question to add more detail. Commented Feb 12, 2016 at 23:54
  • I don't know why someone down voted this question. I don't know the difference, if there is no difference why does MVC framework cast null to object? Commented Feb 12, 2016 at 23:59
  • 1
    Answered your question about why ASP.NET uses it like that. Commented Feb 13, 2016 at 0:17

3 Answers 3

3

In the specific example you provided (why it is used like that in the ASP.NET source code) the answer is: yes, it makes a difference whether the (object) cast is there or not.

We have this method:

public static Route MapRoute(this RouteCollection routes, string name, string url)
{
    return MapRoute(routes, name, url, null /* defaults */, (object)null /* constraints */);
}

Lets assume we call it like this:

RouteCollection.MapRoute(routes, "SomeName", "SomeUrl");

Because the (object)null cast is in this method, the following method will be called:

public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints)
{
    return MapRoute(routes, name, url, defaults, constraints, null /* namespaces */);
}

Because the last parameter is explicitly object (we casted to that).

Without the cast the following method would be called:

public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces)
{
    return MapRoute(routes, name, url, defaults, null /* constraints */, namespaces);
}

So basically, the cast is there as an aid to choose the more specific method over the less specific.

You can test it yourself with a simpler example:

public static class TestClass
{
    public static void TestMethod(object parameter)
    {
    }

    public static void TestMethod(string[] parameter)
    {
    }
}

And then call:

TestClass.TestMethod(null); // will call TestMethod(string[] parameter)

TestClass.TestMethod((object)null); // will call TestMethod(object parameter)
Sign up to request clarification or add additional context in comments.

Comments

2

Nothing.

Visual Studio will probably even tell you that the explicit cast is redundant.

Comments

0

There is no difference both will set x to null.

public static void Main()
{
    object a =  (object)null; // casts null to object basically creating a null object and assigning it to a making it null
    object b =  null; // directly assigns b the value of null
    Console.WriteLine(a == b);
}

There might be a negligible overhead when casting null to object as opposed to just setting it to null.

https://dotnetfiddle.net/elilu7

To further confirm that there is no difference and using OPs reference to Microsofts own code in the System.Web.Mvc.RouteCollectionExtensions.cs, which would add validity to OPs question:

//This method cast to object
public static Route MapRoute(this RouteCollection routes, string name, string url)
{
    return MapRoute(routes, name, url, null /* defaults */, (object)null /* constraints */);
}

//This method does not
public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces)
{
    return MapRoute(routes, name, url, null /* defaults */, null /* constraints */, namespaces);
}

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.