c# - Why does the source code for the Guid constructor contain the line "this = Guid.Empty"? -
c# - Why does the source code for the Guid constructor contain the line "this = Guid.Empty"? -
if source code constructor of guid(string)
in .net 4.5.2 source code follows:
public guid(string g) { if (g==null) { throw new argumentnullexception("g"); } contract.endcontractblock(); = guid.empty; guidresult result = new guidresult(); result.init(guidparsethrowstyle.all); if (tryparseguid(g, guidstyles.any, ref result)) { = result.parsedguid; } else { throw result.getguidparseexception(); } }
the question purpose of line this = guid.empty;
?
from can see if string g
can parsed in tryparseguid
method this
assigned. if can't exception thrown.
suppose wrote:
var guid = new guid("invalidguid");
this cause exception , value of guid undefined assume. why need assign this
guid.empty
?
this more matter of style else - functionally it's superfluous , compiler may optimize away assignment guid.empty
in generated code.
defensive coding recommends variable should have initial value explicitly assigned. why? because reduces ambiguity, unfamiliar details of given programming language/platform. illustration 1 might ask:
what default value of guid? should new guid? should zero's? should other indeterminate state?in stating:
guid id = guid.empty;
all these questions answered reading code , without having resort reading documentation and/or source. further, it's explicit variable id
starts out beingness empty indicates reader it'll have value set later in code.
c# .net
Comments
Post a Comment