1: it isn't clear (from your post) if the values ever change; if they are
set *once* (in the instance/static ctor for the object/type as appropriate)
then you should be fine, and I would mark the backing field as readonly to
indicate this behaviour; if they are updated during execution then no: this
is not reliably thread-safe
2: if the method does not update local state (static fields etc), then you
possibly don't need to do anything special. In particular: if you would
literally be making the method non-static (and not changing any other fields
etc on the class), then this won't achieve anything anyway. Are you worried
about the variables declared inside the method? If so, note that these
typically exist on the calling thread's stack, not against the class, so
each caller (even with 50 at once) has their own copy of these. If you are
worried about static fields that are updated by the method, then yes: this
needs protecting. This would normally be done using a lock on a private,
readonly object (static as required); note that there is also an attribute
you can decorate the required members with, but I can't remember its name
(since I always use lock for more granular control).
Marc
|