c# - Static propery or static readony field -


i have intialized property/fields "constant" , want know 1 of following line best use :

  1. public static color mycolor { { return color.red; } }
  2. 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

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -