c# - I feel that the 1st argument of Enum.Format() is a bit redundant -


i've got code blow:

enum days {     day1,     day2,     day3 } class program {     static void main(string[] args)     {         console.writeline(enum.format(typeof(days), days.day2, "d"));     } } 

i feel 1st argument of enum.format() redundant: because 2nd argument specified type of enum, compiler have information type of "day2" "days". why compile doesn't deduce 1st argument "typeof(days)" self, why have specify it?

in other words, mean, if .net function of enum.format have 2 arguments, why can't be? type can know value argument.

because can use underlying type of enum in value parameter, like:

public enum myenum {     foo = 1 }  string str = enum.format(typeof(myenum), 1, "g"); // foo 

but note that:

public enum myenum : long {     foo = 1 }  string str = enum.format(typeof(myenum), 1l, "g"); 

as i've written, have use underlying type! in case, long.

very indirectly spelled out in enum.format:

argumentexception: type of value not underlying type of enumtype.

so implicitly, if value of underlying type of enumtype, there no exception , result returned.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -