Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing null value to overloading method where Object and String as param in C#

Tags:

c#

.net

I have two overloaded methods like below

 public class TestClass
    {

        public void LoadTest(object param)
        {
            Console.WriteLine("Loading object...");
        }
        public void LoadTest(string param)
        {
            Console.WriteLine("Loading string...");
        }

    }

After calling this method like below it will show the output as Loading string... Please explain how .net handle this scenario?

 class Program
    {
        static void Main(string[] args)
        {       
            var obj=new TestClass();
            obj.LoadTest(null);
           // obj.LoadType(null);
            Console.ReadLine();
        }
    }
like image 331
Jameel Moideen Avatar asked Aug 19 '13 09:08

Jameel Moideen


People also ask

Can we pass null to a method as parameter?

You cannot pass the null value as a parameter to a Java scalar type method; Java scalar types are always non-nullable. However, Java object types can accept null values. The values of both variable <@I> and variable <@A> are null, since values have not been assigned to them.

Can null be passed as string?

It is a value of the reference variable. The access to a null reference generates a NullPointerException. It is not allowed to pass null as a value to call the methods that contain any primitive data type.

Can we pass null as object?

No, 'null' is a value. In java you are passing arguments to function as reference (except basic types), which value (of this reference) 'points' to object. In this case value of reference points to nothing.

What if we pass null to method?

When we pass a null value to the method1 the compiler gets confused which method it has to select, as both are accepting the null. This compile time error wouldn't happen unless we intentionally pass null value.


4 Answers

The C# compiler takes the most specific overload possible.

As string is an object, and it can have the value of null, the compiler deems string to be more specific.

like image 93
It'sNotALie. Avatar answered Oct 19 '22 18:10

It'sNotALie.


null is a valid string.

It will try to match the most specific type and string is more specific than object

Depending on your use you should probably remove the parameter totally in the object overload.

like image 43
Karthik T Avatar answered Oct 19 '22 19:10

Karthik T


This is just the way C# compiler prioritizes to determine which method is better to call. There is one rule:

If method A has more specific parameter types than method B, then method A is better than method B in overload case.

In your case, apparently string is more specified than object, that is why LoadTest(string param) is called.

You can refer 7.5.3.2 Better function member in C# language specification to get more understanding.

like image 42
cuongle Avatar answered Oct 19 '22 20:10

cuongle


because compiler takes the closest possible specific method that can be accessed (null to string is closer than null to object) that is available. And in this case as both as overload with string is closer, that is why it is called.

This is what MSDN has to say

Once the candidate function members and the argument list have been identified, the selection of the best function member is the same in all cases:

  1. Given the set of applicable candidate function members, the best function member in that set is located.

  2. If the set contains only one function member, then that function member is the best function member.

  3. Otherwise, the best function member is the one function member that is better than all other function members with respect to the given argument list, provided that each function member is compared to all other function members using the rules in Section 7.4.2.2.

  4. If there is not exactly one function member that is better than all other function members, then the function member invocation is ambiguous and a compile-time error occurs.

like image 25
Ehsan Avatar answered Oct 19 '22 20:10

Ehsan