The call is ambiguous between the following methods in C#

I was writing some Unit Tests on default ASP.NET MVC Movie Application. Application can be created by following tutorials here or alternatively you can download sample application as well. Okay so while writing unit test, I had to pass null value for id in Edit action. Compiler was not happy me passing null and I got compile time error as given in below image,


Error is clearly saying that there is ambiguous between two methods. On examining Edit action found it was overloaded,

  • Id parameter
  • Instance of Movie parameter

Now when I passed null, complier was not able to decide which overloaded Edit action needed to be called. Because null can be pass for integer id value and for object Movie as well. And that’s the reason complier was throwing compile time error.

How to solve it? Of course you can change definition of Edit action. But we may not want to do it. Other option is to forcefully pass null either as movie or integer id. Null can be passed as integer id as below,


Remember this solution not restricted only for MVC and can work in any code written using C#. So, the call is ambiguous between the following methods in C# can be resolved as easily as given discussed above.

Convert List to List using ConvertAll in C#

I see many developers convert one list to another list using foreach. It is ok and get the job done but there is better way to convert one list to another list using ConvertAll();

Just to explain you scenario better, let us assume you got two classes as below. InternalStudent class is representing Student class internally in the project and this is not supposed to be exposed in Service etc.

public class InternalStudent
        public string IName { get; set; }
        public int IAge { get; set; }

Another class is Student

  public class Student
        public string Name { get; set; }
        public int Age { get; set; }

Now you have List of InternalStudents as below,

List<InternalStudent> lstInterStudent = new List<InternalStudent>
                new InternalStudent {IName="dj",IAge=32},
                new InternalStudent{IName="pinal",IAge=36}

Requirement is to convert List<InternalStudent> to List<Student>. Usually we convert one list to another list using foreach as given below,

List<Student> lstStudent = new List<Student>();
foreach(InternalStudent s in lstInterStudent)
                lstStudent.Add(new Student
                        Name = s.IName,
                        Age = s.IAge

There is no problem as such in this way of conversion from one generic list to another generic list. However there is better way to do this conversion.

List<Student> lstStudent = lstInterStudent.ConvertAll(x => new Student
                    Age = x.IAge,
                    Name = x.IName

We are using ConvertAll to convert one generic list to another generic list. I hope you find this post useful. Thanks for reading.