c# - Hiding public members of an interface in IL -
consider these lines of code:
concurrentdictionary<string, object> error = new concurrentdictionary<string, object>(); error.add("hello", "world"); //actually throws compiler error
and
idictionary<string, object> noerror = new concurrentdictionary<string, object>(); noerror.add("hello", "world");
i figure out have change il make add
function private.
now in spirit of decoupled code i'd use interface seems concurrent dictionary isn't found of add
method.
is safe use add
(i can't view il don't know if it's thread safe.)? or should use concrete type of concurrentdictionary<tkey, tvalue>
, explicitly use tryadd
.
yes, it's safe.
have @ reference source concurrentdictionary. method idictionary<tkey, tvalue>.add
calls tryadd
, throws exception if key exists.
the hiding of members of interface not requires il modifications done. can done through explicit interface implementation in c#. done leaving off access modifier of method , prefixing method name interface name:
void idictionary<tkey,tvalue>.add(tkey key, tvalue value) {}
there various reasons doing this, maybe don't want clutter concrete interface, or want consumers of class explicit method using if name of methods on interface aren't specific enough. also, allows provide separate implementations methods on different interfaces same signature (not issue concurrentdictionary
think, capability there if need in own classes).
Comments
Post a Comment