C# not inferring overloaded method via return type

I'm writing a C# programlet to crawl a directory and give me a listing of files whose date in the last CSV line is less than the current date. Since this is a programlet, I'm not really spending too much time making the code very clean or anything -- but that's all a matter of opinion, I suppose.

The curious thing is the following set of code snippets. Three static methods all in the same class.

   public static DateTime dateStringConverter(string mmddyyyy, char delim='/')
    {
            string[] date = mmddyyyy.Split(delim);
            DateTime fileTime = new DateTime(Convert.ToInt32(date[2]), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
            return fileTime;
    }

        public static string dateStringGetter()
        {
            string sYear = DateTime.Now.Year.ToString();
            string sMonth = DateTime.Now.Month.ToString().PadLeft(2, '0');
            string sDay = DateTime.Now.Day.ToString().PadLeft(2, '0');
            return sMonth + '/' + sDay + '/' + sYear;
        }

        public static DateTime dateStringGetter()
        {
            string datestring = dateStringGetter();
            return dateStringConverter(datestring);
        }

The error message says:

Error   1   Type 'Poller.Program' already defines a member called 
'dateStringGetter' with the same parameter types    

The problem method is the second overloaded copy of dateStringGetter(), which of course has the same parameter types as the second version (none) but has two completely different return types. One is DateTime and the other is string. The version with the DateTime return type -- in a string of bad coding -- calls the version of dateStringGetter() with a string type.

Isn't this curious? That C# will not overload methods based on return type alone? I think I've done overloading with libraries that will automatically detect the return type I want based on the call -- but I'm not sure. Something doesn't feel right about this.

So I suppose C# does not overload return types?

Jon Skeet
people
quotationmark

So I suppose C# does not overload return types?

No, indeed it doesn't. The return type isn't part of the signature.

From section 3.6 of the C# 5 specification (emphasis mine):

The signature of a method consists of the name of the method, the number of type parameters and the type and kind (value, reference, or output) of each of its formal parameters, considered in the order left to right. For these purposes, any type parameter of the method that occurs in the type of a formal parameter is identified not by its name, but by its ordinal position in the type argument list of the method. The signature of a method specifically does not include the return type, the params modifier that may be specified for the right-most parameter, nor the optional type parameter constraints.

and

Overloading of methods permits a class, struct, or interface to declare multiple methods with the same name, provided their signatures are unique within that class, struct, or interface.

And additionally (for completeness):

Although out and ref parameter modifiers are considered part of a signature, members declared in a single type cannot differ in signature solely by ref and out.

Aside from anything else, this restriction helps with readability - it can be hard enough to tell which overload is being called sometimes even when they vary by parameters - it would be even worse if methods could be overloaded by return type. In this case it doesn't even make sense for the methods to be overloaded, as they do opposite things. You should only overload a method if all the overloads perform the same basic task.

As a side note, your methods don't currently follow .NET naming conventions - and you should be using standard formatting/parsing methods instead of rolling your own.

people

See more on this question at Stackoverflow