overloading - C# not inferring overloaded method via return type -
i'm writing c# programlet crawl directory , give me listing of files date in lastly csv line less current date. since programlet, i'm not spending much time making code clean or -- that's matter of opinion, suppose.
the curious thing next set of code snippets. 3 static methods in 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])); homecoming 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'); homecoming smonth + '/' + sday + '/' + syear; } public static datetime datestringgetter() { string datestring = datestringgetter(); homecoming datestringconverter(datestring); }
the error message says:
error 1 type 'poller.program' defines fellow member called 'datestringgetter' same parameter types
the problem method sec overloaded re-create of datestringgetter(), of course of study has same parameter types sec version (none) has 2 different homecoming types. 1 datetime , other string. version datetime homecoming type -- in string of bad coding -- calls version of datestringgetter() string type.
isn't curious? c# not overload methods based on homecoming type alone? think i've done overloading libraries automatically observe homecoming type want based on phone call -- i'm not sure. doesn't sense right this.
so suppose c# not overload homecoming types?
so suppose c# not overload homecoming types?
no, indeed doesn't. homecoming type isn't part of signature.
from section 3.6 of c# 5 specification (emphasis mine):
the signature of method consists of name of method, number of type parameters , type , kind (value, reference, or output) of each of formal parameters, considered in order left right. these purposes, type parameter of method occurs in type of formal parameter identified not name, ordinal position in type argument list of method. the signature of method not include homecoming type, params
modifier may specified right-most parameter, nor optional type parameter constraints.
and
overloading of methods permits class, struct, or interface declare multiple methods same name, provided signatures unique within class, struct, or interface.
and additionally (for completeness):
although out
, ref
parameter modifiers considered part of signature, members declared in single type cannot differ in signature solely ref
, out
.
aside else, restriction helps readability - can hard plenty tell overload beingness called when vary parameters - worse if methods overloaded homecoming type. in case doesn't create sense methods overloaded, opposite things. should overload method if overloads perform same basic task.
as side note, methods don't follow .net naming conventions - , should using standard formatting/parsing methods instead of rolling own.
c# overloading method-overloading
No comments:
Post a Comment