c# - Static propery or static readony field -
i have intialized property/fields "constant" , want know 1 of following line best use :
public static color mycolor { { return color.red; } }public static readonly color myothercolor = color.red;
is there runtime differences after (lazy)initialization ? performance usages differents ?
the field usage guidelines recommend using public static read-only fields predefined object instances. example:
public struct color { // predefined immutable instance of containing type public static readonly color red = new color(0x0000ff); ... } in case, i'd use property:
public class myclass { // not predefined instance of containing type => property // it's constant today, knows, tomorrow value may come // configuration file. public static color mycolor { { return color.red; } } } update
it's crystal clear when see answer, using ilspy in system.drawing shows me following code: public static color red { { return new color(knowncolor.red); } }
the guidelines linked above (which use color example) .net 1.1 , have possibly evolved. don't think can go wrong using property. .net 4.0 field guidelines similar, use datetime.maxvalue , datetime.minvalue examples of predefined object instances.
Comments
Post a Comment