You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Don't use ref or out parameters (AV1562) They make code less understandable and might cause people to introduce bugs. Instead, return compound objects.
New language features to consider:
out var
The next example would be disallowed by this rule in its current form:
boolsuccess=int.TryParse(text,outintnumber);
ref returns and ref locals
This provides the ability to return by reference and to store references in local variables. Strictly speaking, this feature is not affected by this rule, but it is somewhat related. Example:
classExample{privaterefintGetMaxValue(refintvalue1,refintvalue2){if(value1>value2){returnrefvalue1;}returnrefvalue2;}publicvoidTest(){intnumber1=1;intnumber2=2;refintmax=refGetMaxValue(refnumber1,refnumber2);max=5;Console.WriteLine(number2);// prints 5}}
Tuples and deconstruction
The next example uses the compiler-recognized Deconstruct method (which must have out parameters), to enable deconstruction of the Point class into tuple elements.
Add an exception to this rule for the TryParse pattern (and include example above).
Extend "Instead, return compound objects." to: "Instead, return compound objects or tuples."
ref returns/locals were introduced to bridge the performance gap with unmanaged unsafe code. It seems worth the effort only in unusual cases, so I don't think we should promote its usage. So let's not mention it.
Although CSharpGuidelinesAnalyzer should be made aware of the Deconstruct method and not report rule violations for it, I think we should not confuse readers by mentioning this feature here.
The text was updated successfully, but these errors were encountered:
Existing rule:
Don't use ref or out parameters (AV1562)
They make code less understandable and might cause people to introduce bugs. Instead, return compound objects.
New language features to consider:
The next example would be disallowed by this rule in its current form:
This provides the ability to return by reference and to store references in local variables. Strictly speaking, this feature is not affected by this rule, but it is somewhat related. Example:
The next example uses the compiler-recognized
Deconstruct
method (which must haveout
parameters), to enable deconstruction of the Point class into tuple elements.Proposal:
TryParse
pattern (and include example above).unmanagedunsafe code. It seems worth the effort only in unusual cases, so I don't think we should promote its usage. So let's not mention it.Deconstruct
method and not report rule violations for it, I think we should not confuse readers by mentioning this feature here.The text was updated successfully, but these errors were encountered: