C# – Why does this function overloading is not working

cc#-3.0genericsoverloading

Hy,
i know it sounds a very stupid question.
Here's what i found:

public static List<SomeDTO> GetData(Guid userId, int languageId)
 {
 // Do something here
 }

    public static List<int> GetData(Guid userId ,int iNumberOfItems)
    {
      var result = GetData(userID,0);

     return  (from r in result select c.id).Take(iNumberOfItems).ToList<int>();
    }

I get a compilation error:

ClassLibrary' already defines a member called 'GetData' with the same parameter types

The second only returning the ids of the first function.

I know this isn't working.
I know there's both returning a List<> type ,but they returning differentypes.
Could somebody explain me why?
How can i solve this?

Update This problem is solved on F# !

Best Answer

You can't override based on the return-type. If you look at your method signature, it looks like the following:

public static List<SomeDTO> GetData(Guid, int)

and

public static List<int> GetData(Guid, int)

What you need to do is one of the following:

  • Change the method names to something clearer.
  • Make sure that the parameters can identify which method to call

Now, when you call your function, the return type isn't specified. And since the parameters look the same, the compiler doesn't know which function to call. But it doesn't need to make a guess, since the signatures are too similar it complains on compilation.