the stylecop tool provides warnings ... - documentation & help · documentation rules (sa1600-)...

704
The StyleCop tool provides warnings that indicate style and consistency rule violations in C# code. By default, the tool integrates into Visual Studio 2008 and Visual Studio 2010, and it can also be integrated into MSBuild-based build systems. It is also possible to author custom rules to run within the tool, and to create a wrapper host to integrate the tool into a custom build environment. StyleCop Rules Documentation Starting with StyleCop 4.3.2, it is possible to suppress the reporting of rule violations by adding suppression attributes within the source code. Rule Suppressions

Upload: others

Post on 21-Aug-2020

46 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TheStyleCoptoolprovideswarningsthatindicatestyleandconsistencyruleviolationsinC#code.Bydefault,thetoolintegratesintoVisualStudio2008andVisualStudio2010,anditcanalsobeintegratedintoMSBuild-basedbuildsystems.Itisalsopossibletoauthorcustomrulestorunwithinthetool,andtocreateawrapperhosttointegratethetoolintoacustombuildenvironment.

StyleCopRulesDocumentation

StartingwithStyleCop4.3.2,itispossibletosuppressthereportingofruleviolationsbyaddingsuppressionattributeswithinthesourcecode.

RuleSuppressions

Page 2: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TheStyleCoptoolprovideswarningsthatindicatestyleandconsistencyruleviolationsinC#code.Thewarningsareorganizedintoruleareassuchasdocumentation,layout,naming,ordering,readability,spacing,andsoforth.Eachwarningsignifiesaviolationofastyleorconsistencyrule.ThissectionprovidesanexplanationofeachofthedefaultStyleCoprules.

Page 3: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

InThisSectionDocumentationRules(SA1600-)

Ruleswhichverifythecontentandformattingofcodedocumentation.

LayoutRules(SA1500-)

Ruleswhichenforcecodelayoutandlinespacing.

MaintainabilityRules(SA1400-)

Ruleswhichimprovecodemaintainability.

NamingRules(SA1300-)

Ruleswhichenforcenamingrequirementsformembers,types,andvariables.

OrderingRules(SA1200-)

Ruleswhichenforceastandardorderingschemeforcodecontents.

ReadabilityRules(SA1100-)

Ruleswhichensurethatthecodeiswell-formattedandreadable.

SpacingRules(SA1000-)

Ruleswhichenforcespacingrequirementsaroundkeywordsandsymbolsinthecode.

Page 4: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RelatedTopicsStartingwithStyleCop4.3.2,itispossibletosuppressthereportingofruleviolationsbyaddingsuppressionattributeswithinthesourcecode.

RuleSuppressions

Page 5: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Ruleswhichverifythecontentandformattingofcodedocumentation.

SA1600:ElementsMustBeDocumented

SA1601:PartialElementsMustBeDocumented

SA1602:EnumerationItemsMustBeDocumented

SA1603:DocumentationMustContainValidXml

SA1604:ElementDocumentationMustHaveSummary

SA1605:PartialElementDocumentationMustHaveSummary

SA1606:ElementDocumentationMustHaveSummaryText

SA1607:PartialElementDocumentationMustHaveSummaryText

SA1608:ElementDocumentationMustNotHaveDefaultSummary

SA1609:PropertyDocumentationMustHaveValue

SA1610:PropertyDocumentationMustHaveValueText

SA1611:ElementParametersMustBeDocumented

SA1612:ElementParameterDocumentationMustMatchElementParameters

SA1613:ElementParameterDocumentationMustDeclareParameterName

SA1614:ElementParameterDocumentationMustHaveText

SA1615:ElementReturnValueMustBeDocumented

SA1616:ElementReturnValueDocumentationMustHaveValue

SA1617:VoidReturnValueMustNotBeDocumented

SA1618:GenericTypeParametersMustBeDocumented

SA1619:GenericTypeParametersMustBeDocumentedPartialClass

SA1620:GenericTypeParameterDocumentationMustMatchTypeParameters

SA1621:GenericTypeParameterDocumentationMustDeclareParameterName

SA1622:GenericTypeParameterDocumentationMustHaveText

SA1623:PropertySummaryDocumentationMustMatchAccessors

SA1624:

Page 6: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

PropertySummaryDocumentationMustOmitSetAccessorWithRestricedAccess

SA1625:ElementDocumentationMustNotBeCopiedAndPasted

SA1626:SingleLineCommentsMustNotUseDocumentationStyleSlashes

SA1627:DocumentationTextMustNotBeEmpty

SA1628:DocumentationTextMustBeginWithACapitalLetter

SA1629:DocumentationTextMustEndWithAPeriod

SA1630:DocumentationTextMustContainWhitespace

SA1631:DocumentationTextMustMeetCharacterPercentage

SA1632:DocumentationTextMustMeetMinimumCharacterLength

SA1633:FileMustHaveHeader

SA1634:FileHeaderMustShowCopyright

SA1635:FileHeaderMustHaveCopyrightText

SA1636:FileHeaderCopyrightTextMustMatch

SA1637:FileHeaderMustContainFileName

SA1638:FileHeaderFileNameDocumentationMustMatchFileName

SA1639:FileHeaderMustHaveSummary

SA1640:FileHeaderMustHaveValidCompanyText

SA1641:FileHeaderCompanyNameTextMustMatch

SA1642:ConstructorSummaryDocumentationMustBeginWithStandardText

SA1643:DestructorSummaryDocumentationMustBeginWithStandardText

SA1644:DocumentationHeadersMustNotContainBlankLines

SA1645:IncludedDocumentationFileDoesNotExist

SA1646:IncludedDocumentationXPathDoesNotExist

SA1647:IncludeNodeDoesNotContainValidFileAndPath

SA1648:InheritDocMustBeUsedWithInheritingClass

SA1649:FileHeaderFileNameDocumentationMustMatchTypeName

Page 7: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementsMustBeDocumented

CheckId SA1600

Category DocumentationRules

Page 8: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#codeelementismissingadocumentationheader.

Page 9: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifanelementiscompletelymissingadocumentationheader,oriftheheaderisempty.InC#thefollowingtypesofelementscanhavedocumentationheaders:classes,constructors,delegates,enums,events,finalizers,indexers,interfaces,methods,properties,andstructs.

Page 10: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorfill-inadocumentationheaderfortheelement.

thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 11: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PartialElementsMustBeDocumented

CheckId SA1601

Category DocumentationRules

Page 12: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#partialelementismissingadocumentationheader.

Page 13: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifapartialelement(anelementwiththepartialattribute)iscompletelymissingadocumentationheader,oriftheheaderisempty.InC#thefollowingtypesofelementscanbeattributedwiththepartialattribute:classes,methods.

Whendocumentationisprovidedonmorethanonepartofthepartialclass,thedocumentationforthetwoclassesmaybemergedtogethertoformasinglesourceofdocumentation.Forexample,considerthefollowingtwopartsofapartialclass:

///<summary>

///DocumentationforthefirstpartofClass1.

///</summary>

publicpartialclassClass1

{

}

///<summary>

///DocumentationforthesecondpartofClass1.

///</summary>

publicpartialclassClass1

Page 14: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

}

Thesetwodifferentpartsofthesamepartialclasseachprovidedifferentdocumentationfortheclass.WhenthedocumentationforthisclassisbuiltintoanSDK,thetoolbuildingthedocumentationwilleitherchoosetouseonlyonepartofthedocumentationfortheclassandignoretheotherparts,or,insomecases,itmaymergethetwosourcesofdocumentationtogether,toformastringlike:“DocumentationforthefirstpartofClass1.DocumentationforthesecondpartofClass1.”

Forthesereasons,itcanbeproblematictoprovideSDKdocumentationonmorethanonepartofthepartialclass.However,itisstilladvisabletodocumenteachpartoftheclass,toincreasethereadabilityandmaintainabilityofthecode,andStyleCopwillrequirethateachpartoftheclasscontainheaderdocumentation.

Thisproblemissolvedthroughtheuseofthe<content>tag,whichcanreplacethe<summary>tagforpartialclasses.TherecommendedpracticefordocumentingpartialclassesistoprovidetheofficialSDKdocumentationfortheclassonthemainpartofthepartialclass.Thisdocumentationshouldbewrittenusingthestandard<summary>tag.Allotherpartsofthepartialclassshouldomitthe<summary>tagcompletely,andreplaceitwitha<content>tag.ThisallowsthedevelopertodocumentallpartsofthepartialclasswhilestillcentralizingalloftheofficialSDKdocumentationfortheclassontoonepartoftheclass.The<content>tagswillbeignoredbytheSDKdocumentationtools.

Page 15: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorfill-inadocumentationheaderfortheelement.

Forexample,thefollowingexampleshowstwopartsofapartialclass,onecontaininga<summary>headerandanothercontaininga<content>header.

///<summary>

///Representsacustomerinthedatabase.

///</summary>

publicpartialclassCustomer

{

}

///<content>

///Containsauto-generatedfunctionalityfortheCustomerclass.

///</content>

publicpartialclassCustomer

{

}

Page 16: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName EnumerationItemsMustBeDocumented

CheckId SA1602

Category DocumentationRules

Page 17: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnitemwithinaC#enumerationismissinganXmldocumentationheader.

Page 18: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenanitemwithinanenumerationismissingaheader.Forexample:

///<summary>

///Typesofanimals.

///</summary>

publicenumAnimals

{

Dog,

Cat,

Horse

}

Page 19: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addadocumentationheaderforeachitemwithintheenum.Forexample:

///<summary>

///Typesofanimals.

///</summary>

publicenumAnimals

{

///<summary>

///Representsadog.

///</summary>

Dog,

///<summary>

///Representsacat.

///</summary>

Cat,

///<summary>

///Representsahorse.

Page 20: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

///</summary>

Horse

}

Page 21: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationMustContainValidXml

CheckId SA1603

Category DocumentationRules

Page 22: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmlwithinaC#element’sdocumentheaderisbadlyformed.

Page 23: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

AviolationofthisruleoccurswhenthedocumentationXmlisbadlyformedandcannotbeparsed.ThiscanoccuriftheXmlcontainsinvalidcharacters,orifanXmlnodeismissingaclosingtag,forexample.

Page 24: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,replacethebadlyformedXmlwithvalidXmlthatcanbeparsedbyastandardXmlparser.

ThefollowingexampleshowsaclasscontaininginvalidXmlwithinitsdocumentationheader.Theclosingtagforthe<summary>nodeisinvalid.

///<summary>

///AnexampleofbadlyformedXml.

///</summa3ry>

publicclassExample

{

}

Page 25: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementDocumentationMustHaveSummary

CheckId SA1604

Category DocumentationRules

Page 26: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmlheaderdocumentationforaC#elementismissinga<summary>tag.

Page 27: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhentheelementdocumentationismissinga<summary>tag.

Page 28: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addandfill-ina<summary>tagfortheelement,containingadescriptionoftheelement.

ThefollowingexampleshowsaclasscontaininginvalidXmlwithinitsdocumentationheader.Theclosingtagforthe<summary>nodeisinvalid.

///<summary>

///Representsacustomerinthedatabase.

///</summary>

publicclassCustomer

{

}

Page 29: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PartialElementDocumentationMustHaveSummary

CheckId SA1605

Category DocumentationRules

Page 30: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThe<summary>or<content>tagwithinthedocumentationheaderforaC#codeelementismissingorempty.

Page 31: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenthedocumentationheaderforapartialelement(anelementwiththepartialattribute)ismissinga<summary>or<content>tag,orcontainsanempty<summary>or<content>tagwhichdoesnotcontainadescriptionoftheelement.InC#thefollowingtypesofelementscanbeattributedwiththepartialattribute:classes,methods.

Whendocumentationisprovidedonmorethanonepartofthepartialclass,thedocumentationforthetwoclassesmaybemergedtogethertoformasinglesourceofdocumentation.Forexample,considerthefollowingtwopartsofapartialclass:

///<summary>

///DocumentationforthefirstpartofClass1.

///</summary>

publicpartialclassClass1

{

}

///<summary>

///DocumentationforthesecondpartofClass1.

///</summary>

publicpartialclassClass1

Page 32: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

}

Thesetwodifferentpartsofthesamepartialclasseachprovidedifferentdocumentationfortheclass.WhenthedocumentationforthisclassisbuiltintoanSDK,thetoolbuildingthedocumentationwilleitherchoosetouseonlyonepartofthedocumentationfortheclassandignoretheotherparts,or,insomecases,itmaymergethetwosourcesofdocumentationtogether,toformastringlike:“DocumentationforthefirstpartofClass1.DocumentationforthesecondpartofClass1.”

Forthesereasons,itcanbeproblematictoprovideSDKdocumentationonmorethanonepartofthepartialclass.However,itisstilladvisabletodocumenteachpartoftheclass,toincreasethereadabilityandmaintainabilityofthecode,andStyleCopwillrequirethateachpartoftheclasscontainheaderdocumentation.

Thisproblemissolvedthroughtheuseofthe<content>tag,whichcanreplacethe<summary>tagforpartialclasses.TherecommendedpracticefordocumentingpartialclassesistoprovidetheofficialSDKdocumentationfortheclassonthemainpartofthepartialclass.Thisdocumentationshouldbewrittenusingthestandard<summary>tag.Allotherpartsofthepartialclassshouldomitthe<summary>tagcompletely,andreplaceitwitha<content>tag.ThisallowsthedevelopertodocumentallpartsofthepartialclasswhilestillcentralizingalloftheofficialSDKdocumentationfortheclassontoonepartoftheclass.The<content>tagswillbeignoredbytheSDKdocumentationtools.

Page 33: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addandfill-ina<summary>or<content>tagwithadescriptionofthecodeelement.

Thefollowingexampleshowsapartialclasswithafill-in<summary>tag.

///<summary>

///Representsacustomerinthedatabase.

///</summary>

publicpartialclassCustomer

{

}

Page 34: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementDocumentationMustHaveSummaryText

CheckId SA1606

Category DocumentationRules

Page 35: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThe<summary>tagwithinthedocumentationheaderforaC#codeelementisempty.

Page 36: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenthedocumentationheaderforanelementcontainsanempty<summary>tagwhichdoesnotcontainadescriptionoftheelement.

Page 37: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,fill-inthe<summary>tagwithadescriptionofthecodeelement.

Example:

Thefollowingexampleshowsamethodwhichcontainsanempty<summary>tag.

///<summary></summary>

///<paramname="customerId">TheIDofthecustomertofind.</param>

///<returns>Thecustomer,ornullifthecustomercouldnotbe

///found.</returns>

publicCustomerFindCustomer(intcustomerId)

{

//...findsthecustomer...

}

Page 38: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PartialElementDocumentationMustHaveSummaryText

CheckId SA1607

Category DocumentationRules

Page 39: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThe<summary>or<content>tagwithinthedocumentationheaderforaC#codeelementisempty.

Page 40: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenthedocumentationheaderforapartialelement(anelementwiththepartialattribute)containsanempty<summary>tagor<content>tagwhichdoesnotcontainadescriptionoftheelement.InC#thefollowingtypesofelementscanbeattributedwiththepartialattribute:classes,methods.

Whendocumentationisprovidedonmorethanonepartofthepartialclass,thedocumentationforthetwoclassesmaybemergedtogethertoformasinglesourceofdocumentation.Forexample,considerthefollowingtwopartsofapartialclass:

///<summary>

///DocumentationforthefirstpartofClass1.

///</summary>

publicpartialclassClass1

{

}

///<summary>

///DocumentationforthesecondpartofClass1.

///</summary>

publicpartialclassClass1

Page 41: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

}

Thesetwodifferentpartsofthesamepartialclasseachprovidedifferentdocumentationfortheclass.WhenthedocumentationforthisclassisbuiltintoanSDK,thetoolbuildingthedocumentationwilleitherchoosetouseonlyonepartofthedocumentationfortheclassandignoretheotherparts,or,insomecases,itmaymergethetwosourcesofdocumentationtogether,toformastringlike:“DocumentationforthefirstpartofClass1.DocumentationforthesecondpartofClass1.”

Forthesereasons,itcanbeproblematictoprovideSDKdocumentationonmorethanonepartofthepartialclass.However,itisstilladvisabletodocumenteachpartoftheclass,toincreasethereadabilityandmaintainabilityofthecode,andStyleCopwillrequirethateachpartoftheclasscontainheaderdocumentation.

Thisproblemissolvedthroughtheuseofthe<content>tag,whichcanreplacethe<summary>tagforpartialclasses.TherecommendedpracticefordocumentingpartialclassesistoprovidetheofficialSDKdocumentationfortheclassonthemainpartofthepartialclass.Thisdocumentationshouldbewrittenusingthestandard<summary>tag.Allotherpartsofthepartialclassshouldomitthe<summary>tagcompletely,andreplaceitwitha<content>tag.ThisallowsthedevelopertodocumentallpartsofthepartialclasswhilestillcentralizingalloftheofficialSDKdocumentationfortheclassontoonepartoftheclass.The<content>tagswillbeignoredbytheSDKdocumentationtools.

Page 42: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,fill-inthecontentsofthesummarytagorcontenttagwithadescriptionofthecodeelement.

Thefollowingexampleshowsamethodwhichcontainsanempty<summary>tag.

///<summary></summary>

///<paramname="customerId">TheIDofthecustomertofind.</param>

///<returns>Thecustomer,ornullifthecustomercouldnotbefound.</returns>

publicCustomerFindCustomer(intcustomerId)

{

//...findsthecustomer...

}

Tofixtheviolation,addvalidsummarytext.Forexample:

///<summary>AttemptstolocatearecordforthecustomerwiththegivenID.</summary>

///<paramname="customerId">TheIDofthecustomertofind.</param>

///<returns>Thecustomer,ornullifthecustomercouldnotbefound.</returns>

publicCustomerFindCustomer(intcustomerId)

{

Page 43: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

//...findsthecustomer...

}

Page 44: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementDocumentationMustNotHaveDefaultSummary

CheckId SA1608

Category DocumentationRules

Page 45: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThe<summary>tagwithinanelement’sXmlheaderdocumentationcontainsthedefaulttextgeneratedbyVisualStudioduringthecreationoftheelement.

Page 46: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

VisualStudioprovideshelperfunctionalityforaddingnewelementssuchasclassestoaproject.VisualStudiowillcreateadefaultdocumentationheaderforthenewclassandfillinthisheaderwithdefaultdocumentationtext.

Aviolationofthisruleoccurswhenthe<summary>tagforacodeelementstillcontainsthedefaultdocumentationtextgeneratedbyVisualStudio.

Page 47: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,replacethedefaultdocumentationtextwithnewtextdescribingthecontentsofthecodeelement.

ThefollowingexampleshowsaclasswhichcontainsthedefaultsummarytextgeneratedbyVisualStudio.

///<summary>

///SummarydescriptionfortheExampleclass.

///</summary>

publicclassExample

{

}

Page 48: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PropertyDocumentationMustHaveValue

CheckId SA1609

Category DocumentationRules

Page 49: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmlheaderdocumentationforaC#propertydoesnotcontaina<value>tag.

Page 50: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Thedocumentationforpropertiesmayincludea<value>tag,whichdescribesthevalueheldbytheproperty.

Aviolationofthisruleoccurswhenthe<value>tagforapropertyismissing.

Page 51: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addandfill-ina<value>tagwithinthedocumentationheaderfortheproperty.

Thefollowingexampleshowsapropertywhichcontainsa<value>tagwithinitsdocumentationheader.

///<summary>

///Getsthenameofthecustomer.

///</summary>

///<value>Thenameofthecustomer.</value>

publicboolName

{

get{returnthis.name;}

}

Page 52: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PropertyDocumentationMustHaveValueText

CheckId SA1610

Category DocumentationRules

Page 53: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmlheaderdocumentationforaC#propertycontainsanempty<value>tag.

Page 54: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Thedocumentationforpropertiesmayincludea<value>tag,whichdescribesthevalueheldbytheproperty.

Aviolationofthisruleoccurswhenthe<value>tagforapropertyisempty.

Page 55: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,fill-inadescriptionofthevalueheldbythepropertywithinthe<value>tag.

Example:

Thefollowingexampleshowsapropertywhichcontainsa<value>tagwithinitsdocumentationheader.

///<summary>

///Getsthenameofthecustomer.

///</summary>

///<value>Thenameofthecustomer.</value>

publicboolName

{

get{returnthis.name;}

}

Page 56: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementParametersMustBeDocumented

CheckId SA1611

Category DocumentationRules

Page 57: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#method,constructor,delegateorindexerelementismissingdocumentationforoneormoreofitsparameters.

Page 58: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifanelementcontainingparametersismissingdocumentationforoneormoreofitsparameters.

Page 59: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorfill-indocumentationtextwithina<param>tagforeachparameterwithintheelement.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 60: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementParameterDocumentationMustMatchElementParameters

CheckId SA1612

Category DocumentationRules

Page 61: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThedocumentationdescribingtheparameterstoaC#method,constructor,delegateorindexerelementdoesnotmatchtheactualparametersontheelement.

Page 62: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifthedocumentationforanelement’sparametersdoesnotmatchtheactualparametersontheelement,oriftheparameterdocumentationisnotlistedinthesameorderastheelement’sparameters.

Page 63: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,correcttheparameterdocumentationsothatthe<param>tagsinthedocumentationappearinthesameorderastheelement’sparameters,andsothatthereisone<param>tagforeachparameterontheelement.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 64: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementParameterDocumentationMustDeclareParameterName

CheckId SA1613

Category DocumentationRules

Page 65: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseA<param>tagwithinaC#element’sdocumentationheaderismissinganameattributecontainingthenameoftheparameter.

Page 66: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifthedocumentationforanelementcontainsa<param>tagwhichismissinganameattribute,orwhichcontainsanemptynameattribute.

Page 67: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorfill-inthenameattributeforthe<param>tagtoindicatethenameoftheparameterthatthedocumentationisfor.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 68: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementParameterDocumentationMustHaveText

CheckId SA1614

Category DocumentationRules

Page 69: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseA<param>tagwithinaC#element’sdocumentationheaderisempty.

Page 70: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifthedocumentationforanelementcontainsa<param>tagwhichisemptyanddoesnotcontainadescriptionoftheparameter.

Page 71: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,fill-inadescriptionoftheparameterwithinthe<param>tag.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 72: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementReturnValueMustBeDocumented

CheckId SA1615

Category DocumentationRules

Page 73: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#elementismissingdocumentationforitsreturnvalue.

Page 74: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifanelementcontainingareturnvalueismissinga<returns>tag.

Page 75: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addandfill-indocumentationtextwithina<returns>tagdescribingthevaluereturnedfromtheelement.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 76: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementReturnValueDocumentationMustHaveText

CheckId SA1616

Category DocumentationRules

Page 77: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThe<returns>tagwithinaC#element’sdocumentationheaderisempty.

Page 78: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifanelementcontainsanempty<returns>tag.

Page 79: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,fill-indocumentationtextwithinthe<returns>tagdescribingthevaluereturnedfromtheelement.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 80: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName VoidReturnValueMustNotBeDocumented

CheckId SA1617

Category DocumentationRules

Page 81: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#codeelementdoesnotcontainareturnvalue,orreturnsvoid,butthedocumentationheaderfortheelementcontainsa<returns>tag.

Page 82: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifanelementwhichreturnsvoidcontainsa<returns>tagwithinitsdocumentationheader.

Page 83: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removethe<returns>tagfromtheelement.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Printsthegivenname.

///</summary>

///<paramname="firstName">Thefirstname.</param>

///<paramname="lastName">Thelastname.</param>

publicvoidPrintNames(stringfirstName,stringlastName)

{

Console.WriteLine(firstName+""+lastName);

}

Page 84: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName GenericTypeParametersMustBeDocumented

CheckId SA1618

Category DocumentationRules

Page 85: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAgenericC#elementismissingdocumentationforoneormoreofitsgenerictypeparameters.

Page 86: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifanelementcontaininggenerictypeparametersismissingdocumentationforoneormoreofitsgenerictypeparameters.

Page 87: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorfill-indocumentationtextwithina<typeparam>tagforeachgenerictypeparameterontheelement.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Asamplegenericclass.

///</summary>

///<typeparamname="S">Thefirstgenerictypeparameter.</typeparam>

///<typeparamname="T">Thesecondgenerictypeparameter.</typeparam>

publicclassClass1<S,T>

{

}

Page 88: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName GenericTypeParametersMustBeDocumentedPartialClass

CheckId SA1619

Category DocumentationRules

Page 89: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAgeneric,partialC#elementismissingdocumentationforoneormoreofitsgenerictypeparameters,andthedocumentationfortheelementcontainsa<summary>tag.

Page 90: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenageneric,partialelementismissingdocumentationforoneormoreofitsgenerictypeparameters,andthedocumentationfortheelementcontainsa<summary>tagratherthana<content>tag.

Whendocumentationisprovidedonmorethanonepartofthepartialclass,thedocumentationforthetwoclassesmaybemergedtogethertoformasinglesourceofdocumentation.Forexample,considerthefollowingtwopartsofapartialclass:

///<summary>

///DocumentationforthefirstpartofClass1.

///</summary>

publicpartialclassClass1

{

}

///<summary>

///DocumentationforthesecondpartofClass1.

///</summary>

publicpartialclassClass1

Page 91: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

}

Thesetwodifferentpartsofthesamepartialclasseachprovidedifferentdocumentationfortheclass.WhenthedocumentationforthisclassisbuiltintoanSDK,thetoolbuildingthedocumentationwilleitherchoosetouseonlyonepartofthedocumentationfortheclassandignoretheotherparts,or,insomecases,itmaymergethetwosourcesofdocumentationtogether,toformastringlike:“DocumentationforthefirstpartofClass1.DocumentationforthesecondpartofClass1.”

Forthesereasons,itcanbeproblematictoprovideSDKdocumentationonmorethanonepartofthepartialclass.However,itisstilladvisabletodocumenteachpartoftheclass,toincreasethereadabilityandmaintainabilityofthecode,andStyleCopwillrequirethateachpartoftheclasscontainheaderdocumentation.

Thisproblemissolvedthroughtheuseofthe<content>tag,whichcanreplacethe<summary>tagforpartialclasses.TherecommendedpracticefordocumentingpartialclassesistoprovidetheofficialSDKdocumentationfortheclassonthemainpartofthepartialclass.Thisdocumentationshouldbewrittenusingthestandard<summary>tag.Allotherpartsofthepartialclassshouldomitthe<summary>tagcompletely,andreplaceitwitha<content>tag.ThisallowsthedevelopertodocumentallpartsofthepartialclasswhilestillcentralizingalloftheofficialSDKdocumentationfortheclassontoonepartoftheclass.The<content>tagswillbeignoredbytheSDKdocumentationtools.

Whenagenericelementcontainsa<summary>tagwithinitsdocumentationheader,StyleCopassumesthatthisisthemainpartoftheclass,andrequirestheheadertocontain<typeparam>tagsforeachofthegenerictypeparametersontheclass.However,ifthedocumentationheaderfortheclasscontainsa<content>tagratherthana<summary>tag,StyleCopwillassumethatthegenerictypeparametersaredefinedonanotherpartoftheclass,andwillnotrequire<typeparam>tagsonthispartoftheclass.

Page 92: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorfill-indocumentationtextwithina<typeparam>tagforeachgenerictypeparameterontheelement,orchangethe<summary>tagtoa<content>tagifthisisnotthemainpartofthepartialclass.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Asamplegenericclass.

///</summary>

///<typeparamname="S">Thefirstgenerictypeparameter.</typeparam>

///<typeparamname="T">Thesecondgenerictypeparameter.</typeparam>

publicclassClass1<S,T>

{

}

Page 93: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName GenericTypeParameterDocumentationMustMatchTypeParameters

CheckId SA1620

Category DocumentationRules

Page 94: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThe<typeparam>tagswithintheXmlheaderdocumentationforagenericC#elementdonotmatchthegenerictypeparametersontheelement.

Page 95: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifthe<typeparam>tagswithintheelement’sheaderdocumentationdonotmatchthegenerictypeparametersontheelement,ordonotappearinthesameorderastheelement’stypeparameters.

Page 96: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addandfill-inone<typeparam>tagforeachgenerictypeparameterontheelement,andmakesurethatthe<typeparam>tagsappearinthesameorderastheelement’stypeparameters.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Asamplegenericclass.

///</summary>

///<typeparamname="S">Thefirstgenerictypeparameter.</typeparam>

///<typeparamname="T">Thesecondgenerictypeparameter.</typeparam>

publicclassClass1<S,T>

{

}

Page 97: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName GenericTypeParameterDocumentationMustDeclareParameterName

CheckId SA1621

Category DocumentationRules

Page 98: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseA<typeparam>tagwithintheXmlheaderdocumentationforagenericC#elementismissinganameattribute,orcontainsanemptynameattribute.

Page 99: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursiftheelementcontainsa<typeparam>tagwithinitsXmlheaderdocumentationwhichdoesnotdeclarethenameofthetypeparameter.

Page 100: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorfill-inthenameattributeforeach<typeparam>tag,indicatingthenameofthetypeparameterthatthedocumentationisfor.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Asamplegenericclass.

///</summary>

///<typeparamname="S">Thefirstgenerictypeparameter.</typeparam>

///<typeparamname="T">Thesecondgenerictypeparameter.</typeparam>

publicclassClass1<S,T>

{

}

Page 101: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName GenericTypeParameterDocumentationMustHaveText

CheckId SA1622

Category DocumentationRules

Page 102: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseA<typeparam>tagwithintheXmlheaderdocumentationforagenericC#elementisempty.

Page 103: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursiftheelementcontainsanempty<typeparam>tagwithinitsXmlheaderdocumentation.

Page 104: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,fill-ineach<typeparam>tagwithinadescriptionofthegenerictypeparameter.

Thefollowingexampleshowsamethodwithavaliddocumentationheader:

///<summary>

///Asamplegenericclass.

///</summary>

///<typeparamname="S">Thefirstgenerictypeparameter.</typeparam>

///<typeparamname="T">Thesecondgenerictypeparameter.</typeparam>

publicclassClass1<S,T>

{

}

Page 105: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PropertySummaryDocumentationMustMatchAccessors

CheckId SA1623

Category DocumentationRules

Page 106: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThedocumentationtextwithinaC#property’s<summary>tagdoesnotmatchtheaccessorswithintheproperty.

Page 107: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccursifaproperty’ssummarydocumentationdoesnotmatchtheaccessorswithintheproperty.

Theproperty’ssummarytextmustbeginwithwordingdescribingthetypesofaccessorsexposedwithintheproperty.Ifthepropertycontainsonlyagetaccessor,thesummarymustbeginwiththeword“Gets”.Ifthepropertycontainsonlyasetaccessor,thesummarymustbeginwiththeword“Sets”.Ifthepropertyexposesbothagetandsetaccessor,thesummarytextmustbeginwith“Getsorsets”.

Forexample,considerthefollowingproperty,whichexposesbothagetandsetaccessor.Thesummarytextbeginswiththewords“Getsorsets”.

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

publicstringName

{

get{returnthis.name;}

set{this.name=value;}

}

IfthepropertyreturnsaBooleanvalue,anadditionalruleisapplied.ThesummarytextforBooleanpropertiesmustcontainthewords“Getsavalueindicatingwhether”,“Setsavalueindicatingwhether”,or“Getsorsetsavalue

Page 108: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

indicatingwhether”.Forexample,considerthefollowingBooleanproperty,whichonlyexposesagetaccessor:

///<summary>

///Getsavalueindicatingwhethertheitemisenabled.

///</summary>

publicboolEnabled

{

get{returnthis.enabled;}

}

Insomesituations,thesetaccessorforapropertycanhavemorerestrictedaccessthanthegetaccessor.Forexample:

///<summary>

///Getsthenameofthecustomer.

///</summary>

publicstringName

{

get{returnthis.name;}

privateset{this.name=value;}

}

Inthisexample,thesetaccessorhasbeengivenprivateaccess,meaningthatitcanonlybeaccessedbylocalmembersoftheclassinwhichitiscontained.Thegetaccessor,however,inheritsitsaccessfromtheparentproperty,thusitcanbe

Page 109: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

accessedbyanycaller,sincethepropertyhaspublicaccess.

>Inthiscase,thedocumentationsummarytextshouldavoidreferringtothesetaccessor,sinceitisnotvisibletoexternalcallers.

StyleCopappliesaseriesofrulestodeterminewhenthesetaccessorshouldbereferencedintheproperty’ssummarydocumentation.Ingeneral,theserulesrequirethesetaccessortobereferencedwheneveritisvisibletothesamesetofcallersasthegetaccessor,orwheneveritisvisibletoexternalclassesorinheritingclasses.

Thespecificrulesfordeterminingwhethertoincludethesetaccessorintheproperty’ssummarydocumentationare:

1.Thesetaccessorhasthesameaccesslevelasthegetaccessor.Forexample:

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

protectedstringName

{

get{returnthis.name;}

set{this.name=value;}

}

2.Thepropertyisonlyinternallyaccessiblewithintheassembly,andthesetaccessoralsohasinternalaccess.Forexample:

internalclassClass1

{

///<summary>

Page 110: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

///Getsorsetsthenameofthecustomer.

///</summary>

protectedstringName

{

get{returnthis.name;}

internalset{this.name=value;}

}

}

internalclassClass1

{

publicclassClass2

{

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

publicstringName

{

get{returnthis.name;}

internalset{this.name=value;}

Page 111: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

}

}

3.Thepropertyisprivateoriscontainedbeneathaprivateclass,andthesetaccessorhasanyaccessmodifierotherthanprivate.Intheexamplebelow,theaccessmodifierdeclaredonthesetaccessorhasnomeaning,sincethesetaccessoriscontainedwithinaprivateclassandthuscannotbeseenbyotherclassesoutsideofClass1.Thiseffectivelygivesthesetaccessorthesameaccesslevelasthegetaccessor.

publicclassClass1

{

privateclassClass2

{

publicclassClass3

{

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

publicstringName

{

get{returnthis.name;}

internalset{this.name=value;}

Page 112: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

}

}

}

4.Wheneverthesetaccessorhasprotectedorprotectedinternalaccess,itshouldbereferencedinthedocumentation.Aprotectedorprotectedinternalsetaccessorcanalwaysbeenseenbyaclassinheritingfromtheclasscontainingtheproperty.

internalclassClass1

{

publicclassClass2

{

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

internalstringName

{

get{returnthis.name;}

protectedset{this.name=value;}

}

}

Page 113: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

privateclassClass3:Class2

{

publicClass3(stringname){this.Name=name;}

}

}

Page 114: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,updatetheproperty’ssummarytextsothatthedescriptionbeginswiththeproperwording,dependinguponthetypeofthepropertyandthetypesofaccessorswithintheproperty.

Page 115: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PropertySummaryDocumentationMustOmitSetAccessorWithRestrictedAccess

CheckId SA1624

Category DocumentationRules

Page 116: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThedocumentationtextwithinaC#property’s<summary>tagtakesintoaccountalloftheaccessorswithintheproperty,butoneoftheaccessorshaslimitedaccess.

Page 117: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenoneoftheaccessorswithinthepropertyhaslimitedaccess(usuallythesetaccessor),butthesummarydocumentationtextforthepropertystillreferstobothaccessors.

Normally,aproperty’ssummarytextmustbeginwithwordingdescribingthetypesofaccessorsexposedwithintheproperty.Ifthepropertycontainsonlyagetaccessor,thesummarymustbeginwiththeword“Gets”.Ifthepropertycontainsonlyasetaccessor,thesummarymustbeginwiththeword“Sets”.Ifthepropertyexposesbothagetandsetaccessor,thesummarytextmustbeginwith“Getsorsets”.

However,whenanaccessorwithinthepropertyisgivenanaccesslevelwhichismorelimitedthantheaccessleveloftheproperty,thisaccessorshouldbeomittedfromthesummarydocumentation.

Itcansometimesbenon-obviouswhetherthesetaccessorwithinapropertyisactuallylessaccessiblethanthegetaccessor.Forexample,considerthecasewhereapublicpropertyiscontainedwithinaninternalclass,andthesetaccessorisgiveninternalaccessor.Ineffect,boththegetandsetaccessorshavethesameaccesslevel.Inthiscase,thesummarydocumentationshouldrefertoboththegetandsetaccessors,sincetheyeffectivelyhavethesameaccesslevel.

Insomesituations,thesetaccessorforapropertycanhavemorerestrictedaccessthanthegetaccessor.Forexample:

///<summary>

///Getsthenameofthecustomer.

///</summary>

publicstringName

Page 118: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

get{returnthis.name;}

privateset{this.name=value;}

}

Inthisexample,thesetaccessorhasbeengivenprivateaccess,meaningthatitcanonlybeaccessedbylocalmembersoftheclassinwhichitiscontained.Thegetaccessor,however,inheritsitsaccessfromtheparentproperty,thusitcanbeaccessedbyanycaller,sincethepropertyhaspublicaccess.

Inthiscase,thedocumentationsummarytextshouldavoidreferringtothesetaccessor,sinceitisnotvisibletoexternalcallers.

StyleCopappliesaseriesofrulestodeterminewhenthesetaccessorshouldbereferencedintheproperty’ssummarydocumentation.Ingeneral,theserulesrequirethesetaccessortobereferencedwheneveritisvisibletothesamesetofcallersasthegetaccessor,orwheneveritisvisibletoexternalclassesorinheritingclasses.

Thespecificrulesfordeterminingwhethertoincludethesetaccessorintheproperty’ssummarydocumentationare:

1.Thesetaccessorhasthesameaccesslevelasthegetaccessor.Forexample:

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

protectedstringName

{

get{returnthis.name;}

set{this.name=value;}

}

Page 119: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

2.Thepropertyisonlyinternallyaccessiblewithintheassembly,andthesetaccessoralsohasinternalaccess.Forexample:

internalclassClass1

{

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

protectedstringName

{

get{returnthis.name;}

internalset{this.name=value;}

}

}

internalclassClass1

{

publicclassClass2

{

///<summary>

Page 120: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

///Getsorsetsthenameofthecustomer.

///</summary>

publicstringName

{

get{returnthis.name;}

internalset{this.name=value;}

}

}

}

3.Thepropertyisprivateoriscontainedbeneathaprivateclass,andthesetaccessorhasanyaccessmodifierotherthanprivate.Intheexamplebelow,theaccessmodifierdeclaredonthesetaccessorhasnomeaning,sincethesetaccessoriscontainedwithinaprivateclassandthuscannotbeseenbyotherclassesoutsideofClass1.Thiseffectivelygivesthesetaccessorthesameaccesslevelasthegetaccessor.

publicclassClass1

{

privateclassClass2

{

publicclassClass3

{

Page 121: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

///<summary>

///Getsorsetsthenameofthecustomer.

///</summary>

publicstringName

{

get{returnthis.name;}

internalset{this.name=value;}

}

}

}

}

4.Wheneverthesetaccessorhasprotectedorprotectedinternalaccess,itshouldbereferencedinthedocumentation.Aprotectedorprotectedinternalsetaccessorcanalwaysbeenseenbyaclassinheritingfromtheclasscontainingtheproperty.

internalclassClass1

{

publicclassClass2

{

///<summary>

Page 122: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

///Getsorsetsthenameofthecustomer.

///</summary>

internalstringName

{

get{returnthis.name;}

protectedset{this.name=value;}

}

}

privateclassClass3:Class2

{

publicClass3(stringname){this.Name=name;}

}

}

Page 123: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,updatetheproperty’ssummarytextandremovewordingwhichreferstothelimitedaccessaccessor.

Page 124: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementDocumentationMustNotBeCopiedAndPasted

CheckId SA1625

Category DocumentationRules

Page 125: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmldocumentationforaC#elementcontainstwoormoreidenticalentries,indicatingthatthedocumentationhasbeencopiedandpasted.Thiscansometimesindicateinvalidorpoorlywrittendocumentation.

Page 126: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenanelementcontainstwoormoreidenticaldocumentationtexts.Forexample:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Partofthename.</param>

///<paramname="lastName">Partofthename.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Insomecases,amethodmaycontainoneormoreparameterswhicharenotusedwithinthebodyofthemethod.Inthiscase,thedocumentationfortheparametercanbesetto"Theparameterisnotused."StyleCopwillallowmultipleparameterstocontainidenticaldocumentationaslongasthedocumentationstringis"Theparameterisnotused."

Page 127: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,editthedocumentationfortheelementandensurethateachoftheindividualdocumentationtextsareunique.Forexample:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 128: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SingleLineCommentsMustNotUseDocumentationStyleSlashes

CheckId SA1626

Category DocumentationRules

Page 129: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codecontainsasingle-linecommentwhichbeginswiththreeforwardslashesinarow.

Page 130: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainsasingle-linecommentwhichbeginswiththreeslashes.CommentsbeginningwiththreeslashesarereservedforXmldocumentationheaders.Single-linecommentsshouldbeginwithonlytwoslashes.Whencommentingoutlinesofcode,itisadvisabletobeginthecommentwithfourslashestodifferentiateitfromnormalcomments.Forexample:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Partofthename.</param>

///<paramname="lastName">Partofthename.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

Alegalcommentbeginningwithtwoslashes:

//Jointhenamestogether.

stringfullName=firstName+""+lastName;

Anillegalcommentbeginningwiththreeslashes:

///Trimthename.

fullName=fullName.Trim();

Page 131: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Alineofcommented-outcodebeginningwithfourslashes:

////fullName=asfd;

returnfullName;

}

Page 132: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removeaslashfromthebeginningofthecommentsothatitbeginswithonlytwoslashes.

Page 133: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationTextMustNotBeEmpty

CheckId SA1627

Category DocumentationRules

Page 134: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmlheaderdocumentationforaC#codeelementcontainsanemptytag.

Page 135: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenthedocumentationheaderforanelementcontainsanemptytag.Forexample:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName"></param>

///<paramname="lastName">Partofthename.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

...

}

Page 136: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,adddocumentationtextwithintheemptytag.

Page 137: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationTextMustBeginWithACapitalLetter

CheckId SA1628

Category DocumentationRules

Page 138: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsectionoftheXmlheaderdocumentationforaC#elementdoesnotbeginwithacapitalletter.

Page 139: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenpartofthedocumentationdoesnotbeginwithacapitalletter.Forexample,thesummarytextinthedocumentationbelowbeginswithalower-caseletter:

///<summary>

///joinsafirstnameandalastnametogetherintoasinglestring.

///</summary>

///<paramname="firstName">Thefirstname.</param>

///<paramname="lastName">Thelastname.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

...

}

Page 140: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatallsectionsofthedocumentationbeginwithacapitalletter.

Page 141: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationTextMustEndWithAPeriod

CheckId SA1629

Category DocumentationRules

Page 142: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsectionoftheXmlheaderdocumentationforaC#elementdoesnotendwithaperiod(alsoknownasafullstop).

Page 143: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenpartofthedocumentationdoesnotendwithaperiod.Forexample,thesummarytextinthedocumentationbelowdoesnotendwithaperiod:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring

///</summary>

///<paramname="firstName">Thefirstname.</param>

///<paramname="lastName">Thelastname.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

...

}

Page 144: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatallsectionsofthedocumentationendwithaperiod.

Page 145: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationTextMustContainWhitespace

CheckId SA1630

Category DocumentationRules

Page 146: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsectionoftheXmlheaderdocumentationforaC#elementdoesnotcontainanywhitespacebetweenwords.

Page 147: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenpartofthedocumentationdoescontainanywhitespacebetweenwords.Thiscanindicatepoorlywrittenorpoorlyformatteddocumentation.Forexample:

///<summary>

///Joinsnames

///</summary>

///<paramname="firstName">First</param>

///<paramname="lastName">Last</param>

///<returns>Name</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

...

}

Page 148: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatallsectionsofthedocumentationcontainatleastoneinstanceofwhitespacebetweenwords.

Page 149: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationTextMustMeetCharacterPercentage

CheckId SA1631

Category DocumentationRules

Page 150: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsectionoftheXmlheaderdocumentationforaC#elementdoesnotcontainenoughalphabeticcharacters.

Page 151: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenpartofthedocumentationdoescontainenoughcharacters.Thisruleiscalculatedbycountingthenumberofalphabeticcharactersandnumberswithinthedocumentationtext,andcomparingitagainstthenumberofsymbolsandothernon-alphabeticcharacters.Ifthepercentageofnon-alphabeticcharactersistoohigh,thisgenerallyindicatespoorlyformatteddocumentationwhichwillbedifficulttoread.Forexample,considerthefollowsummarydocumentation:

///<summary>

///@)$(*Aname--------

///</summary>

publicclassName

{

...

}

Page 152: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,rewritethedocumentationtextusinggrammaticallyproperlanguage,andensurethattheratioofsymbolsversuscharactersinthetextisnottoogreat.

Page 153: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationTextMustMeetMinimumCharacterLength

CheckId SA1632

Category DocumentationRules

Page 154: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseFromStyleCop4.5thisruleisdisabledbydefault.

AsectionoftheXmlheaderdocumentationforaC#elementistooshort.

Page 155: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenpartofthedocumentationistooshort.Thiscanoftenindicatethatthedocumentationisnotdescriptive.Forexample:

///<summary>

///Aname

///</summary>

publicclassName

{

...

}

Page 156: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,rewritethedocumentationtextusinggrammaticallyproperanddescriptivelanguage.Inmostcases,doingsowillcausethelengthofthedocumentationtexttobegreaterthantheminimumlengthwhichcausesthisruletofire.

Page 157: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileMustHaveHeader

CheckId SA1633

Category DocumentationRules

Page 158: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#codefileismissingastandardfileheader.

Page 159: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaC#sourcefileismissingafileheader.Thefileheadermustbeginonthefirstlineofthefile,andmustbeformattedasablockofcommentscontainingXml,asfollows:

//-----------------------------------------------------------------------

//<copyrightfile="NameOfFile.cs"company="CompanyName">

//Companycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Forexample,afilecalledWidget.csfromafictionalcompanycalledSprocketEnterprisesshouldcontainafileheadersimilartothefollowing:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="SprocketEnterprises">

//Copyright(c)SprocketEnterprises.Allrightsreserved.

//</copyright>

//-----------------------------------------------------------------------

Thedashedlinesatthetopandbottomoftheheaderarenotstrictlynecessary,sotheheadercouldbewrittenas:

//<copyrightfile="Widget.cs"company="SprocketEnterprises">

//Copyright(c)SprocketEnterprises.Allrightsreserved.

//</copyright>

Itispossibletoaddadditionaltags,althoughtheywillnotbecheckedorenforcedbyStyleCop:

Page 160: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="SprocketEnterprises">

//Copyright(c)SprocketEnterprises.Allrightsreserved.

//</copyright>

//<author>JohnDoe</author>

//-----------------------------------------------------------------------

Afilethatiscompletelyauto-generatedbyatool,andwhichshouldnotbecheckedorenforcedbyStyleCop,canincludean“auto-generated”headerratherthanthestandardfileheader.ThiswillcauseStyleCoptoignorethefile.Thistypeofheadershouldneverbeplacedontopofamanuallywrittencodefile.

//<auto-generated/>

namespaceSample.Something

{

//Thecontentsofthisfilearecompletelyauto-generatedbyatool.

}

Page 161: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addastandardfileheaderatthetopofthefile.

Page 162: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderMustShowCopyright

CheckId SA1634

Category DocumentationRules

Page 163: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefileheaderatthetopofaC#codefileismissingacopyrighttag.

Page 164: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefileheaderatthetopofaC#fileismissingacopyrighttag.Forexample:

//-----------------------------------------------------------------------

//<Tag>Afileheaderwhichdoesnotcontainacopyrighttag</Tag>

//-----------------------------------------------------------------------

Afileheadershouldincludeacopyrighttag,asfollows:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Page 165: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addastandardcopyrighttagtothefileheader.

Page 166: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderMustHaveCopyrightText

CheckId SA1635

Category DocumentationRules

Page 167: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefileheaderatthetopofaC#codefileismissingcopyrighttext.

Page 168: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefileheaderatthetopofaC#filedoesnotcontaintextwithinitscopyrighttag.Forexample:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="SprocketEnterprises">

//</copyright>

//-----------------------------------------------------------------------

Afileheadershouldincludecopyrighttext,asfollows:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="SprocketEnterprises">

//Copyright(c)SprocketEnterprises.Allrightsreserved.

//</copyright>

//-----------------------------------------------------------------------

Page 169: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addyourcompany’sstandardcopyrighttexttothecopyrighttag.

Page 170: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderCopyrightTextMustMatch

CheckId SA1636

Category DocumentationRules

Page 171: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefileheaderatthetopofaC#codefiledoesnotcontaintheappropriatecopyrighttext.

Page 172: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefileheaderatthetopofaC#filedoesnotcontainthecopyrighttextthathasbeenspecifiedfortheproject.Toenablethisrule,navigatetotheStyleCopsettingsfortheprojectandchangetotheCompanyInformationtab,asshownbelow:

Checkthecheckboxatthetopofthesettingspage,andfillintherequiredcopyrighttextforyourcompany.ClickOKtosavethesettings.Withthesesettingsinplace,everyfilewithintheprojectmustcontaintherequiredcopyrighttextwithinitsfileheadercopyrighttag,asshownintheexamplebelow:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Page 173: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addyourcompany’sstandardcopyrighttexttothefileheadercopyrighttag.

Page 174: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderMustContainFileName

CheckId SA1637

Category DocumentationRules

Page 175: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefileheaderatthetopofaC#codefileismissingthefilename.

Page 176: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefileheaderatthetopofaC#filedoesnotcontainavalidfilenametag.Forexample:

//-----------------------------------------------------------------------

//<copyrightcompany="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Afileheadershouldincludeafiletagcontainingthenameofthefile,asfollows:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Page 177: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addafiletagcontainingthenameofthefile.

Page 178: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderFileNameDocumentationMustMatchFileName

CheckId SA1638

Category DocumentationRules

Page 179: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefiletagwithinthefileheaderatthetopofaC#codefiledoesnotcontainthenameofthefile.

Page 180: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefiletagwithinthefileheaderatthetopofaC#filedoesnotcontainthenameofthefile.Forexample,consideraC#sourcefilenamedFile1.cs,withthefollowingheader:

//-----------------------------------------------------------------------

//<copyrightfile="File2.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Aviolationofthisrulewouldoccur,sincethefiletagdoesnotcontainthecorrectnameofthefile.Theheadershouldbewrittenas:

//-----------------------------------------------------------------------

//<copyrightfile="File1.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Page 181: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addthenameofthefiletothefiletag.

Page 182: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderMustHaveSummary

CheckId SA1639

Category DocumentationRules

Page 183: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefileheaderatthetopofaC#codefiledoesnotcontainafilled-insummarytag.

Page 184: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefileheaderatthetopofaC#filedoesnotcontainavalidsummarytag.Thisruleisdisabledbydefault.

Forexample:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Ifthisruleisenabled,thefileheadershouldcontainasummarytag.Forexample:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//<summary>ThisistheWidgetclass.</summary>//-----------------------------------------------------------------------

Page 185: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addandfill-inasummarytagdescribingthecontentsofthefile.

Page 186: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderMustHaveValidCompanyText

CheckId SA1640

Category DocumentationRules

Page 187: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefileheaderatthetopofaC#codefiledoesnotcontaincompanynametext.

Page 188: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefileheaderatthetopofaC#filedoesnotcontainacompanytagwithcompanynametext.Forexample:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Thecompanyattributeshouldhavetextinit.Forexample:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Page 189: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addandfill-inacompanyattributecontainingthenameofthecompany.

Page 190: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderCompanyNameTextMustMatch

CheckId SA1641

Category DocumentationRules

Page 191: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefileheaderatthetopofaC#codefiledoesnotcontaintheappropriatecompanynametext.

Page 192: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefileheaderatthetopofaC#filedoesnotcontainthecompanynametextthathasbeenspecifiedfortheproject.Toenablethisrule,navigatetotheStyleCopsettingsfortheprojectandchangetotheCompanyInformationtab,asshownbelow:

Checkthecheckboxatthetopofthesettingspage,andfillintherequiredcompanynametextforyourcompany.ClickOKtosavethesettings.Withthesesettingsinplace,everyfilewithintheprojectmustcontaintherequiredcompanynametextwithinitsfileheadercopyrighttag,asshownintheexamplebelow:

//-----------------------------------------------------------------------

//<copyrightfile="Widget.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

Page 193: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addyourcompany’sstandardcompanynametexttothefileheadercopyrighttag.

Page 194: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ConstructorSummaryDocumentationMustBeginWithStandardText

CheckId SA1642

Category DocumentationRules

Page 195: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmldocumentationheaderforaC#constructordoesnotcontaintheappropriatesummarytext.

Page 196: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenthesummarytagwithinthedocumentationheaderforaconstructordoesnotbeginwiththepropertext.

Theruleisintendedtostandardizethesummarytextforaconstructorbasedontheaccessleveloftheconstructor.Thesummaryforanon-privateinstanceconstructormustbeginwith“Initializesanewinstanceofthe{classname}class.”Forexample,thefollowingshowstheconstructorfortheCustomerclass.

///<summary>

///InitializesanewinstanceoftheCustomerclass.

///</summary>

publicCustomer()

{

}

Itispossibletoembedothertagsintothesummarytext.Forexample:

///<summary>

///Initializesanewinstanceofthe<seecref="Customer"/>class.

///</summary>

publicCustomer()

Page 197: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

}

Iftheclasscontainsgenericparameters,thesecanbeannotatedwithinthecreflinkusingeitherofthefollowingtwoformats:

///<summary>

///Initializesanewinstanceofthe<seecref="Customer`1"/>class.

///</summary>

publicCustomer()

{

}

///<summary>

///Initializesanewinstanceofthe<seecref="Customer{T}"/>class.

///</summary>

publicCustomer()

{

}

Iftheconstructorisstatic,thesummarytextshouldbeginwith“Initializesstaticmembersofthe{classname}class.”Forexample:

///<summary>

///InitializesstaticmembersoftheCustomerclass.

Page 198: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

///</summary>

publicstaticCustomer()

{

}

Privateinstanceconstructorsmustusethesummarytext“Preventsadefaultinstanceofthe{classname}classfrombeingcreated.”

///<summary>

///PreventsadefaultinstanceoftheCustomerclassfrombeingcreated.

///</summary>

privateCustomer()

{

}

Page 199: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,editthesummarytextfortheconstructorasdescribedabove.

Page 200: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DestructorSummaryDocumentationMustBeginWithStandardText

CheckId SA1643

Category DocumentationRules

Page 201: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheXmldocumentationheaderforaC#finalizerdoesnotcontaintheappropriatesummarytext.

Page 202: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenthesummarytagwithinthedocumentationheaderforafinalizerdoesnotbeginwiththepropertext.

Theruleisintendedtostandardizethesummarytextforafinalizer.Thesummaryforafinalizermustbeginwith“Finalizesaninstanceofthe{classname}class.”Forexample,thefollowingshowsthefinalizerfortheCustomerclass.

///<summary>

///FinalizesaninstanceoftheCustomerclass.

///</summary>

~Customer()

{

}

Itispossibletoembedothertagsintothesummarytext.Forexample:

///<summary>

///Finalizesaninstanceofthe<seecref="Customer"/>class.

///</summary>

~Customer()

{

Page 203: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}Iftheclasscontainsgenericparameters,thesecanbeannotatedwithinthecreflinkusingeitherofthefollowingtwoformats:

///<summary>

///Initializesanewinstanceofthe<seecref="Customer`1"/>class.

///</summary>

publicCustomer()

{

}

///<summary>

///Initializesanewinstanceofthe<seecref="Customer{T}"/>class.

///</summary>

publicCustomer()

{

}

Page 204: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,editthesummarytextforthefinalizerasdescribedabove.

Page 205: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationHeadersMustNotContainBlankLines

CheckId SA1644

Category DocumentationRules

Page 206: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsectionwithintheXmldocumentationheaderforaC#elementcontainsblanklines.

Page 207: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Aviolationofthisruleoccurswhenthedocumentationheadercontainsoneormoreblanklineswithinasectionofdocumentation.Forexample:

///<summary>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///

///Usesasimpleformofstringconcatenation.

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Ratherthaninsertingblanklinesintothedocumentation,usethe<para>tagtodenoteparagraphs.Forexample:

///<summary>

Page 208: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

///<para>

///Joinsafirstnameandalastnametogetherintoasinglestring.

///</para><para>

///Usesasimpleformofstringconcatenation.

///</para>

///</summary>

///<paramname="firstName">Thefirstnametojoin.</param>

///<paramname="lastName">Thelastnametojoin.</param>

///<returns>Thejoinednames.</returns>

publicstringJoinNames(stringfirstName,stringlastName)

{

returnfirstName+""+lastName;

}

Page 209: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklinesfromthedocumentationheader,andoptionallyreplacethemwith<para/>tags.

Page 210: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName IncludedDocumentationFileDoesNotExist

CheckId SA1645

Category DocumentationRules

Page 211: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnincludedXmldocumentationfiledoesnotexist.

Page 212: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Asanalternativetoauthoringdocumentationdirectlywithinthecodefile,itispossibletoplacedocumentationformultipleelementswithinaseparateXmlfile,andthenreferenceasectionofthatfilewithinanelement'sdocumentationheader.Thiscausesthecompilertoimportthedocumentationforthatelementfromtheincludeddocument.Forexample:

///<includefile="IncludedDocumentation.xml"path="root/EnabledMethodDocs"/>publicboolEnabled(booltrue)

{

}

Aviolationofthisruleoccurswhentheincludedfiledoesnotexistatthegivenlocation,orcannotbeloaded.

Page 213: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,correctthepathtotheincludeddocumentationfiletopointtoavalidfilelocation.

Page 214: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName IncludedDocumentationXPathDoesNotExist

CheckId SA1646

Category DocumentationRules

Page 215: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnincludedXmldocumentationlinkcontainsaninvalidpath.

Page 216: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Asanalternativetoauthoringdocumentationdirectlywithinthecodefile,itispossibletoplacedocumentationformultipleelementswithinaseparateXmlfile,andthenreferenceasectionofthatfilewithinanelement'sdocumentationheader.Thiscausesthecompilertoimportthedocumentationforthatelementfromtheincludeddocument.Forexample:

///<includefile="IncludedDocumentation.xml"path="root/EnabledMethodDocs"/>publicboolEnabled(booltrue)

{

}

Aviolationofthisruleoccurswhenthepathattributedoesnotlinktoavalidpathwithintheincludeddocumentationfile.

Page 217: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,correctthevalueofthepathattributetopointtoavalidpathwithinthefile.

Page 218: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName IncludeNodeDoesNotContainValidFileAndPath

CheckId SA1647

Category DocumentationRules

Page 219: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnincludetagwithinanXmldocumentationheaderdoesnotcontainvalidfileandpathattribute.

Page 220: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#syntaxprovidesamechanismforinsertingdocumentationforclassesandelementsdirectlyintothecode,throughtheuseofXmldocumentationheaders.Foranintroductiontotheseheadersandadescriptionoftheheadersyntax,seethefollowingarticle:http://msdn.microsoft.com/en-us/magazine/cc302121.aspx.

Asanalternativetoauthoringdocumentationdirectlywithinthecodefile,itispossibletoplacedocumentationformultipleelementswithinaseparateXmlfile,andthenreferenceasectionofthatfilewithinanelement'sdocumentationheader.Thiscausesthecompilertoimportthedocumentationforthatelementfromtheincludeddocument.Forexample:

///<includefile="IncludedDocumentation.xml"path="root/EnabledMethodDocs"/>publicboolEnabled(booltrue)

{

}

Aviolationofthisruleoccurswhentheincludetagismissingafileorpathattribute,orcontainsanimproperlyformattedfileorpathattribute.

Page 221: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorcorrectthefileandpathattributes.

Page 222: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName InheritDocMustBeUsedWithInheritingClass

CheckId SA1648

Category DocumentationRules

Page 223: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Cause<inheritdoc>hasbeenusedonanelementthatdoesntinheritfromabaseclassorimplementaninterface..

Page 224: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionVerifiesthatan'inheritdoc'tagisnotusedwhentheclassorinterfacedoesnotinheritfromabaseclassorinterface.

Aviolationofthisruleoccurswhentheelementhavingtheinheritdoctagdoesn'tinheritfromabasecaseorimplementaninterface.

Page 225: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removethe<inheritdoc>taganddocumenttheelementappropriately.

Page 226: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileHeaderFileNameDocumentationMustMatchTypeName

CheckId SA1649

Category DocumentationRules

Page 227: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThefiletagwithinthefileheaderatthetopofaC#codefiledoesnotmatchthefirsttypedeclaredinthefile.ForgenericsthataredefinedasClass1<T>thenameofthefileneedstobeClass1{T}.csandthisshouldappearintheheaderalso.Partialclassesareignored.

Page 228: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefiletagwithinthefileheaderatthetopofaC#filedoesnotcontainthenameofthefirsttypeinthefile.Forexample,consideraC#sourcefilenamedClass1.cs,withthefollowingheader:

//-----------------------------------------------------------------------

//<copyrightfile="ThisIsNotTheCorrectTypeName.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

publicclassClass1{

}

Aviolationofthisrulewouldoccur,sincethefiletagdoesnotcontainthecorrectnameofthefirsttypeinthefile.Theheadershouldbewrittenas:

//-----------------------------------------------------------------------

//<copyrightfile="Class1.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

publicclassClass1{

Page 229: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

Agenericclassshouldbewrittenas:

//-----------------------------------------------------------------------

//<copyrightfile="Class1{T}.cs"company="MyCompany">

//Customcompanycopyrighttag.

//</copyright>

//-----------------------------------------------------------------------

publicclassClass1<T>{

}

Page 230: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addthenameofthefirsttypefromthefiletothefiletag.

Page 231: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Ruleswhichenforcecodelayoutandlinespacing.

SA1500:CurlyBracketsForMultiLineStatementsMustNotShareLine

SA1501:StatementMustNotBeOnSingleLine

SA1502:ElementMustNotBeOnSingleLine

SA1503:CurlyBracketsMustNotBeOmitted

SA1504:AllAccessorMustBeMultiLineOrSingleLine

SA1505:OpeningCurlyBracketsMustNotBeFollowedByBlankLine

SA1506:ElementDocumentationHeadersMustNotBeFollowedByBlankLine

SA1507:CodeMustNotContainMultipleBlankLinesInARow

SA1508:ClosingCurlyBracketsMustNotBePrecededByBlankLine

SA1509:OpeningCurlyBracketsMustNotBePrecedededByBlankLine

SA1510:ChainedStatementBlocksMustNotBePrecededByBlankLine

SA1511:WhileDoFooterMustNotBePrecededByBlankLine

SA1512:SingleLineCommentsMustNotBeFollowedByBlankLine

SA1513:ClosingCurlyBracketMustBeFollowedByBlankLine

SA1514:ElementDocumentationHeaderMustBePrecededByBlankLine

SA1515:SingleLineCommentMustBePrecededByBlankLine

SA1516:ElementsMustBeSeparatedByBlankLine

SA1517:CodeMustNotContainBlankLinesAtStartOfFile

SA1518:CodeMustNotContainBlankLinesAtEndOfFile

Page 232: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CurlyBracketsForMultiLineStatementsMustNotShareLine

CheckId SA1500

Category LayoutRules

Page 233: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheopeningorclosingcurlybracketwithinaC#statement,element,orexpressionisnotplacedonitsownline.

Page 234: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheopeningorclosingcurlybracketwithinastatement,element,orexpressionisnotplacedonitsownline.Forexample:

publicobjectMethod()

{

lock(this){

returnthis.value;

}

}

WhenStyleCopchecksthiscode,aviolationofthisrulewilloccurbecausetheopeningcurlybracketofthelockstatementisplacedonthesamelineasthelockkeyword,ratherthanbeingplacedonitsownline,asfollows:

publicobjectMethod()

{

lock(this)

{

returnthis.value;

}

}

Aviolationwillalsooccuriftheclosingcurlybracketsharesalinewithothercode.Forexample:

publicobjectMethod()

Page 235: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

lock(this)

{

returnthis.value;}

}

Page 236: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatboththeopeningandclosingcurlybracketsareplacedontheirownline,anddonotsharethelinewithanyothercode,otherthancomments.

Page 237: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName StatementMustNotBeOnASingleLine

CheckId SA1501

Category LayoutRules

Page 238: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#statementcontainingopeningandclosingcurlybracketsiswrittencompletelyonasingleline.

Page 239: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenastatementthatiswrappedinopeningandclosingcurlybracketsiswrittenonasingleline.Forexample:

publicobjectMethod()

{

lock(this){returnthis.value;}

}

WhenStyleCopchecksthiscode,aviolationofthisrulewilloccurbecausetheentirelockstatementiswrittenononeline.Thestatementshouldbewrittenacrossmultiplelines,withtheopeningandclosingcurlybracketseachontheirownline,asfollows:

publicobjectMethod()

{

lock(this)

{

returnthis.value;

}

}

Page 240: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,rewritethestatementsothatitexpandsacrossmultiplelines.

Page 241: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementMustNotBeOnASingleLine

CheckId SA1502

Category LayoutRules

Page 242: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#elementcontainingopeningandclosingcurlybracketsiswrittencompletelyonasingleline.

Page 243: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenanelementthatiswrappedinopeningandclosingcurlybracketsiswrittenonasingleline.Forexample:

publicobjectMethod(){returnnull;}

WhenStyleCopchecksthiscode,aviolationofthisrulewilloccurbecausetheentiremethodiswrittenononeline.Themethodshouldbewrittenacrossmultiplelines,withtheopeningandclosingcurlybracketseachontheirownline,asfollows:

publicobjectMethod()

{

returnnull;

}

Asanexceptiontothisrule,accessorswithinproperties,events,orindexersareallowedtobewrittenallonasingleline,aslongastheaccessorisshort.

Page 244: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,rewritetheelementsothatitexpandsacrossmultiplelines.

Page 245: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CurlyBracketsMustNotBeOmitted

CheckId SA1503

Category LayoutRules

Page 246: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheopeningandclosingcurlybracketsforaC#statementhavebeenomitted.

Page 247: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheopeningandclosingcurlybracketsforastatementhavebeenomitted.InC#,sometypesofstatementsmayoptionallyincludecurlybrackets.Examplesincludeif,while,andforstatements.Forexample,anif-statementmaybewrittenwithoutcurlybrackets:

if(true)

returnthis.value;

AlthoughthisislegalinC#,StyleCopalwaysrequiresthecurlybracketstobepresent,toincreasethereadabilityandmaintainabilityofthecode.

Whenthecurlybracketsareomitted,itispossibletointroduceanerrorinthecodebyinsertinganadditionalstatementbeneaththeif-statement.Forexample:

if(true)

this.value=2;

returnthis.value;

Glancingatthiscode,itappearsasifboththeassignmentstatementandthereturnstatementarechildrenoftheif-statement.Infact,thisisnottrue.Onlytheassignmentstatementisachildoftheif-statement,andthereturnstatementwillalwaysexecuteregardlessoftheoutcomeoftheif-statement.

StyleCopalwaysrequirestheopeningandclosingcurlybracketstobepresent,topreventthesekindsoferrors:

if(true)

{

this.value=2;

returnthis.value;

}

Page 248: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)
Page 249: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,wrapthebodyofthestatementincurlybrackets.

Page 250: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName AllAccessorsMustBeSingleLineOrMultiLine

CheckId SA1504

Category LayoutRules

Page 251: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseWithinaC#property,indexerorevent,atleastoneofthechildaccessorsiswrittenonasingleline,andatleastoneofthechildaccessorsiswrittenacrossmultiplelines.

Page 252: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheaccessorswithinaproperty,indexeroreventarenotconsistentlywrittenonasinglelineoronmultiplelines.Thisruleisintendedtoincreasethereadabilityofthecodebyrequiringalloftheaccessorswithinanelementtobeformattedinthesameway.

Forexample,thefollowingpropertywouldgenerateaviolationofthisrule,becauseoneaccessoriswrittenonasinglelinewhiletheotheraccessorsnapsmultiplelines.

publicboolEnabled

{

get{returnthis.enabled;}

set

{

this.enabled=value;

}

}

Theviolationcanbeavoidedbyplacingbothaccessorsonasingleline,orexpandingbothaccessorsacrossmultiplelines:

publicboolEnabled

{

get{returnthis.enabled;}

set{this.enabled=value;}

Page 253: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

publicboolEnabled

{

get

{

returnthis.enabled;

}

set

{

this.enabled=value;

}

}

Page 254: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,writeeachaccessoronasinglelineiftheaccessorsareshort,orexpandbothaccessorsacrossmultiplelinesiftheaccessorsarelonger.

Page 255: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningCurlyBracketsMustNotBeFollowedByBlankLine

CheckId SA1505

Category LayoutRules

Page 256: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnopeningcurlybracketwithinaC#element,statement,orexpressionisfollowedbyablankline.

Page 257: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenanopeningcurlybracketisfollowedbyablankline.Forexample:

publicboolEnabled

{

get

{

returnthis.enabled;

}

}

Thecodeabovewouldgeneratetwoinstancesofthisviolation,sincetherearetwoplaceswhereopeningcurlybracketsarefollowedbyblanklines.

Page 258: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklinefollowingtheopeningcurlybracket.

Page 259: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementDocumentationHeadersMustNotBeFollowedByBlankLine

CheckId SA1506

Category LayoutRules

Page 260: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnelementdocumentationheaderaboveaC#elementisfollowedbyablankline.

Page 261: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhentheelementdocumentationheaderaboveanelementisfollowedbyablankline.Forexample:

///<summary>

///Getsavalueindicatingwhetherthecontrolisenabled.

///</summary>

publicboolEnabled

{

get{returnthis.enabled;}

}

Thecodeabovewouldgenerateaninstanceofthisviolation,sincethedocumentationheaderisfollowedbyablankline.

Page 262: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklinefollowingthedocumentationheader.

Page 263: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeMustNotContainMultipleBlankLinesInARow

CheckId SA1507

Category LayoutRules

Page 264: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codecontainsmultipleblanklinesinarow.

Page 265: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenthecodecontainsmorethanoneblanklineinarow.Forexample:

publicboolEnabled

{

get

{

Console.WriteLine("Gettingtheenabledflag.");

returnthis.enabled;

}

}

Thecodeabovewouldgenerateaninstanceofthisviolation,sinceitcontainsblankmultiplelinesinarow.

Page 266: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheextrablanklines.

Page 267: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingCurlyBracketsMustNotBePrecededByBlankLine

CheckId SA1508

Category LayoutRules

Page 268: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclosingcurlybracketwithinaC#element,statement,orexpressionisprecededbyablankline.

Page 269: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenaclosingcurlybracketisprecededbyablankline.Forexample:

publicboolEnabled

{

get

{

returnthis.enabled;

}

}

Thecodeabovewouldgeneratetwoinstancesofthisviolation,sincetherearetwoplaceswhereclosingcurlybracketsareprecededbyblanklines.

Page 270: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklineprecedingtheclosingcurlybracket.

Page 271: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningCurlyBracketsMustNotBePrecededByBlankLine

CheckId SA1509

Category LayoutRules

Page 272: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnopeningcurlybracketwithinaC#element,statement,orexpressionisprecededbyablankline.

Page 273: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenanopeningcurlybracketisprecededbyablankline.Forexample:

publicboolEnabled

{

get

{

returnthis.enabled;

}

}

Thecodeabovewouldgeneratetwoinstancesofthisviolation,sincetherearetwoplaceswhereopeningcurlybracketsareprecededbyblanklines.

Page 274: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklineprecedingtheopeningcurlybracket.

Page 275: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ChainedStatementBlocksMustNotBePrecededByBlankLine

CheckId SA1510

Category LayoutRules

Page 276: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseChainedC#statementsareseparatedbyablankline.

Page 277: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

SometypesofC#statementscanonlybeusedwhenchainedtothebottomofanotherstatement.Examplesincludecatchandfinallystatements,whichmustalwaysbechainedtothebottomofatry-statement.Anotherexampleisanelse-statement,whichmustalwaysbechainedtothebottomofanif-statement,ortoanotherelse-statement.Thesetypesofchainedstatementsmustnotbeseparatedbyablankline.Forexample:

try

{

this.SomeMethod();

}

catch(Exceptionex)

{

Console.WriteLine(ex.ToString());

}

Page 278: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removeanyblanklinesbetweenthechainedstatements.

Page 279: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName WhileDoFooterMustNotBePrecededByBlankLine

CheckId SA1511

Category LayoutRules

Page 280: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThewhilefooteratthebottomofado-whilestatementisseparatedfromthestatementbyablankline.

Page 281: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenthewhilekeywordatthebottomofado-whilestatementisseparatedfromthemainpartofthestatementbyoneormoreblanklines.Forexample:

do

{

Console.WriteLine("Loopforever");

}

while(true);

Page 282: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removeanyblanklinesbeforethewhilekeyword.

Page 283: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SingleLineCommentsMustNotBeFollowedByBlankLine

CheckId SA1512

Category LayoutRules

Page 284: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsingle-linecommentwithinC#codeisfollowedbyablankline.

Page 285: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenasingle-linecommentisfollowedbyablankline.Forexample:

publicboolEnabled

{

get

{

//Returnthevalueofthe'enabled'field.

returnthis.enabled;

}

}

Thecodeabovewouldgenerateaninstanceofthisviolation,sincethesingle-linecommentisfollowedbyablankline.

Itisallowedtoplaceablanklineinbetweentwoblocksofsingle-linecomments.Forexample:

publicboolEnabled

{

get

{

Page 286: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

//Thisisasamplecommentwhichdoesn'treallysayanything.

//Thisisanotherpartofthecomment.

//Thereisablanklineabovethiscommentbutthatisok.

returnthis.enabled;

}

}

Ifthecommentisbeingusedtocommentoutalineofcode,placefourforwardslashesatthebeginningofthecomment,ratherthantwo.ThiswillcauseStyleCoptoignorethisviolation.Forexample:

publicboolEnabled

{

get

{

////returnfalse;

returnthis.enabled;

}

}

Page 287: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklinefollowingthesingle-linecomment.

Page 288: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingCurlyBracketMustBeFollowedByBlankLine

CheckId SA1513

Category LayoutRules

Page 289: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclosingcurlybracketwithinaC#element,statement,orexpressionisnotfollowedbyablankline.

Page 290: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenaclosingcurlybracketisnotfollowedbyablankline.Forexample:

publicboolEnabled

{

get

{

returnthis.enabled;

}}

Thecodeabovewouldgenerateoneinstanceofthisviolation,sincethereisoneplacewhereaclosingcurlybracketisnotfollowedbyablankline.

Page 291: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensureablanklinefollowsclosingcurlybrackets.

Page 292: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementDocumentationHeadersMustBePrecededByBlankLine

CheckId SA1514

Category LayoutRules

Page 293: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnelementdocumentationheaderaboveaC#elementisnotprecededbyablankline.

Page 294: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhentheelementdocumentationheaderaboveanelementisnotprecededbyablankline.Forexample:

publicboolVisible

{

get{returnthis.visible;}

}

///<summary>

///Getsavalueindicatingwhetherthecontrolisenabled.

///</summary>

publicboolEnabled

{

get{returnthis.enabled;}

}

Thecodeabovewouldgenerateaninstanceofthisviolation,sincethedocumentationheaderisnotprecededbyablankline.

Anexceptiontothisruleoccurswhenthedocumentationheaderisthefirstitemwithinitsscope.Inthiscase,theheadershouldnotbeprecededbyablankline.Forexample:

publicclassClass1

Page 295: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

///<summary>

///Getsavalueindicatingwhetherthecontrolisenabled.

///</summary>

publicboolEnabled

{

get{returnthis.enabled;}

}

}

Inthecodeabove,theheaderisthefirstitemwithinitsscope,andthusitshouldnotbeprecededbyablankline.

Page 296: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addablanklineabovethedocumentationheader.

Page 297: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SingleLineCommentsMustBePrecededByBlankLine

CheckId SA1515

Category LayoutRules

Page 298: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsingle-linecommentwithinC#codeisnotprecededbyablankline.

Page 299: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhenasingle-linecommentisnotprecededbyablankline.Forexample:

publicboolEnabled

{

get

{

Console.WriteLine("Gettingtheenabledflag.");

//Returnthevalueofthe'enabled'field.

returnthis.enabled;

}

}

Thecodeabovewouldgenerateaninstanceofthisviolation,sincethesingle-linecommentisnotprecededbyablankline.

Anexceptiontothisruleoccurswhenthesingle-linecommentisthefirstitemwithinitsscope.Inthiscase,thecommentshouldnotbeprecededbyablankline.Forexample:

publicboolEnabled

{

get

Page 300: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

//Returnthevalueofthe'enabled'field.

returnthis.enabled;

}

}

Inthecodeabove,thecommentisthefirstitemwithinitsscope,andthusitshouldnotbeprecededbyablankline.

Ifthecommentisbeingusedtocommentoutalineofcode,beginthecommentwithfourforwardslashesratherthantwo.ThiswillcauseStyleCoptoignorethisviolation.Forexample:

publicboolEnabled

{

get

{

Console.WriteLine("Gettingtheenabledflag.");

////returnfalse;returnthis.enabled;

}

}

Page 301: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addablanklineabovethecomment.

Page 302: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementsMustBeSeparatedByBlankLine

CheckId SA1516

Category LayoutRules

Page 303: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAdjacentC#elementsarenotseparatedbyablankline.

Page 304: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethereadabilityofthecode,StyleCoprequiresblanklinesincertainsituations,andprohibitsblanklinesinothersituations.Thisresultsinaconsistentvisualpatternacrossthecode,whichcanimproverecognitionandreadabilityofunfamiliarcode.

Aviolationofthisruleoccurswhentwoadjacentelementarenotseparatedbyablankline.Forexample:

publicvoidMethod1()

{

}

publicboolProperty

{

get{returntrue;}

}

Intheexampleabove,themethodandpropertyarenotseparatedbyablankline,soaviolationofthisrulewouldoccur.

publiceventEventHandlerSomeEvent{add{//addeventsubscriberhere}

remove

Page 305: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{//removeeventsubscriberhere}

}

Intheexampleabove,theaddandremoveoftheeventneedtobeseparatedbyablanklinebecausetheaddismultiline.

Page 306: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolations>HowtoFixViolationsTofixaviolationofthisrule,addablanklinebetweentheadjacentelements.

Page 307: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeMustNotContainBlankLinesAtStartOfFile

CheckId SA1517

Category LayoutRules

Page 308: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThecodefilehasblanklinesatthestart.

Page 309: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethelayoutofthecode,StyleCoprequiresnoblanklinesatthestartoffiles.

Aviolationofthisruleoccurswhenoneormoreblanklinesareatthestartofthefile.

Page 310: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklinesfromthestartofthefile.

Page 311: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeMustNotContainBlankLinesAtEndOfFile

CheckId SA1518

Category LayoutRules

Page 312: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThecodefilehasblanklinesattheend.

Page 313: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionToimprovethelayoutofthecode,StyleCoprequiresnoblanklinesattheendoffiles.

Aviolationofthisruleoccurswhenoneormoreblanklinesareattheendofthefile.

Page 314: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheblanklinesfromtheendofthefile.

Page 315: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Ruleswhichimprovecodemaintainability.

SA1119:StatementMustNotUseUnnecessaryParenthesis

SA1400:AccessModifierMustBeDeclared

SA1401:FieldsMustBePrivate

SA1402:FileMayOnlyContainASingleClass

SA1403:FileMayOnlyContainASingleNamespace

SA1404:CodeAnalysisSuppressionMustHaveJustification

SA1405:DebugAssertMustProvideMessageText

SA1406:DebugFailMustProvideMessageText

SA1407:ArithmeticExpressionsMustDeclarePrecedence

SA1408:ConditionalExpressionsMustDeclarePrecendence

SA1409:RemoveUnnecessaryCode

SA1410:RemoveDelegateParenthesisWhenPossible

SA1411:AttributeConstructorMustNotUseUnnecessaryParenthesis

Page 316: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName StatementMustNotUseUnnecessaryParenthesis

CheckId SA1119

Category MaintainabilityRules

Page 317: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#statementcontainsparenthesiswhichareunnecessaryandshouldberemoved.

Page 318: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionItispossibleinC#toinsertparenthesisaroundvirtuallyanytypeofexpression,statement,orclause,andinmanysituationsuseofparenthesiscangreatlyimprovethereadabilityofthecode.However,excessiveuseofparenthesiscanhavetheoppositeeffect,makingitmoredifficulttoreadandmaintainthecode.

Aviolationofthisruleoccurswhenparenthesisareusedinsituationswheretheyprovidenopracticalvalue.Typically,thishappensanytimetheparenthesissurroundanexpressionwhichdoesnotstrictlyrequiretheuseofparenthesis,andtheparenthesisexpressionislocatedattherootofastatement.Forexample,thefollowinglinesofcodeallcontainunnecessaryparenthesiswhichwillresultinviolationsofthisrule:

intx=(5+b);

stringy=(this.Method()).ToString();

return(x.Value);

Ineachofthesestatements,theextraparenthesiscanberemovedwithoutsacrificingthereadabilityofthecode:

intx=5+b;

stringy=this.Method().ToString();

returnx.Value;

Page 319: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheunnecessaryparenthesis.

Page 320: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName AccessModifierMustBeDeclared

CheckId SA1400

Category MaintainabilityRules

Page 321: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheaccessmodifierforaC#elementhasnotbeenexplicitlydefined.

Page 322: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#allowselementstobedefinedwithoutanaccessmodifier.Dependinguponthetypeofelement,C#willautomaticallyassignanaccessleveltotheelementinthiscase.

Thisrulerequiresanaccessmodifiertobeexplicitlydefinedforeveryelement.Thisremovestheneedforthereadertomakeassumptionsaboutthecode,improvingthereadabilityofthecode.

Page 323: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addanaccessmodifiertothedeclarationoftheelement.

Page 324: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FieldsMustBePrivate

CheckId SA1401

Category MaintainabilityRules

Page 325: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAfieldwithinaC#classhasanaccessmodifierotherthanprivate.

Page 326: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneverafieldinaclassisgivennon-privateaccess.Formaintainabilityreasons,propertiesshouldalwaysbeusedasthemechanismforexposingfieldsoutsideofaclass,andfieldsshouldalwaysbedeclaredwithprivateaccess.Thisallowstheinternalimplementationofthepropertytochangeovertimewithoutchangingtheinterfaceoftheclass.

FieldslocatedwithinC#structsareallowedtohaveanyaccesslevel.

Page 327: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,makethefieldprivateandaddapropertytoexposethefieldoutsideoftheclass.

Page 328: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileMayOnlyContainASingleClass

CheckId SA1402

Category MaintainabilityRules

Page 329: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#codefilecontainsmorethanoneuniqueclass.

Page 330: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaC#filecontainsmorethanoneclass.Toincreaselong-termmaintainabilityofthecode-base,eachclassshouldbeplacedinitsownfile,andfilenamesshouldreflectthenameoftheclasswithinthefile.

Itispossibletoplaceothersupportingelementswithinthesamefileastheclass,suchasdelegates,enums,etc.,iftheyarerelatedtotheclass.

Itisalsopossibletoplacemultiplepartsofthesamepartialclasswithinthesamefile.

Page 331: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,moveeachclassintoitsownfile.

Page 332: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FileMayOnlyContainASingleNamespace

CheckId SA1403

Category MaintainabilityRules

Page 333: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#codefilecontainsmorethanonenamespace.

Page 334: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaC#filecontainsmorethanonenamespace.Toincreaselong-termmaintainabilityofthecode-base,eachfileshouldcontainatmostonenamespace.

Page 335: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthefileonlycontainsasinglenamespace.

Page 336: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeAnalysisSuppressionMustHaveJustification

CheckId SA1404

Category MaintainabilityRules

Page 337: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseACodeAnalysisSuppressMessageattributedoesnotincludeajustification.

Page 338: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainsaCodeAnalysisSuppressMessageattribute,butajustificationforthesuppressionhasnotbeenprovidedwithintheattribute.WheneveraCodeAnalysisruleissuppressed,ajustificationshouldbeprovided.Thiscanincreasethelong-termmaintainabilityofthecode.

[SuppressMessage("Microsoft.Performance","CA1804:RemoveUnusedLocals",Justification="Usedduringunittesting")]

publicboolEnable(){

}

Page 339: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,addaJustificationtagandjustificationtexttotheSuppressMessageattributedescribingthereasonforthesuppression.

Page 340: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DebugAssertMustProvideMessageText

CheckId SA1405

Category MaintainabilityRules

Page 341: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAcalltoDebug.AssertinC#codedoesnotincludeadescriptivemessage.

Page 342: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainsacalltoDebug.Assertwhichdoesnotprovideadescriptionfortheend-user.Forexample,thefollowingassertincludesadescriptionmessage:

Debug.Assert(value!=true,"Thevaluemustalwaysbetrue.");

Page 343: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addadescriptivemessagetotheassertwhichwillappeartotheenduserwhentheassertisfired.

Page 344: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DebugFailMustProvideMessageText

CheckId SA1406

Category MaintainabilityRules

Page 345: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAcalltoDebug.FailinC#codedoesnotincludeadescriptivemessage.

Page 346: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainsacalltoDebug.Failwhichdoesnotprovideadescriptionfortheend-user.Forexample,thefollowingcallincludesadescriptionmessage:

Debug.Fail("Thecodeshouldneverreachthispoint.");

Page 347: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,addadescriptivemessagetotheDebug.Failcallwhichwillappeartotheenduserwhentheassertisfired.

Page 348: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ArithmeticExpressionsMustDeclarePrecedence

CheckId SA1407

Category MaintainabilityRules

Page 349: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#statementcontainsacomplexarithmeticexpressionwhichomitsparenthesisaroundoperators.

Page 350: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#maintainsahierarchyofprecedenceforarithmeticoperators.ItispossibleinC#tostringmultiplearithmeticoperationstogetherinonestatementwithoutwrappinganyoftheoperationsinparenthesis,inwhichcasethecompilerwillautomaticallysettheorderandprecedenceoftheoperationsbasedonthesepre-establishedrules.Forexample:

intx=5+y*b/6%z-2;

Althoughthiscodeislegal,itisnothighlyreadableormaintainable.Inordertoachievefullunderstandingofthiscode,thedevelopermustknowandunderstandthebasicoperatorprecedencerulesinC#.

Thisruleisintendedtoincreasethereadabilityandmaintainabilityofthistypeofcode,andtoreducetheriskofintroducingbugslater,byforcingthedevelopertoinsertparenthesistoexplicitlydeclaretheoperatorprecedence.Theexamplebelowshowsmultiplearithmeticoperationssurroundedbyparenthesis:

intx=5+(y*((b/6)%z))-2;

Insertingparenthesismakesthecodemoreobviousandeasytounderstand,andremovestheneedforthereadertomakeassumptionsaboutthecode.

Page 351: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,insertparenthesiswithinthearithmeticexpressiontodeclaretheprecedenceoftheoperations.

Page 352: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ConditionalExpressionsMustDeclarePrecedence

CheckId SA1408

Category MaintainabilityRules

Page 353: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#statementcontainsacomplexconditionalexpressionwhichomitsparenthesisaroundoperators.

Page 354: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionC#maintainsahierarchyofprecedenceforconditionaloperators.ItispossibleinC#tostringmultipleconditionaloperationstogetherinonestatementwithoutwrappinganyoftheoperationsinparenthesis,inwhichcasethecompilerwillautomaticallysettheorderandprecedenceoftheoperationsbasedonthesepre-establishedrules.Forexample:

if(x||y&&z&&a||b)

{

}

Althoughthiscodeislegal,itisnothighlyreadableormaintainable.Inordertoachievefullunderstandingofthiscode,thedevelopermustknowandunderstandthebasicoperatorprecedencerulesinC#.

Thisruleisintendedtoincreasethereadabilityandmaintainabilityofthistypeofcode,andtoreducetheriskofintroducingbugslater,byforcingthedevelopertoinsertparenthesistoexplicitlydeclaretheoperatorprecedence.Forexample,adevelopercouldwritethiscodeas:

if((x||y)&&z&&(a||b))

{

}

or

if(x||(y&&z&&a)||b)

{

}

Insertingparenthesismakesthecodemoreobviousandeasytounderstand,andremovestheneedforthereadertomakeassumptionsaboutthecode.

Page 355: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsInsertparenthesiswithintheconditionalexpressiontodeclaretheprecedenceoftheoperations.

Page 356: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName RemoveUnnecessaryCode

CheckId SA1409

Category MaintainabilityRules

Page 357: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#filecontainscodewhichisunnecessaryandcanberemovedwithoutchangingtheoveralllogicofthecode.

Page 358: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthefilecontainscodewhichcanberemovedwithoutchangingtheoveralllogicofthecode.

Forexample,thefollowingtry-catchstatementcouldberemovedcompletelysincethetryandcatchblocksarebothempty.

try

{

}

catch(Exceptionex)

{

}

Thetry-finallystatementbelowdoescontaincodewithinthetryblock,butitdoesnotcontainanycatchblocks,andthefinallyblockisempty.Thus,thetry-finallyisnotperforminganyusefulfunctionandcanberemoved.

try

{

this.Method();

}

finally

{

Page 359: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

Asafinalexample,theunsafestatementbelowisempty,andthusprovidesnovalue.

unsafe

{

}

Page 360: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsThefixaviolationofthisrule,removetheunnecessarycode,orfillinthecodewithadditionalstatements.

Page 361: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName RemoveDelegateParenthesisWhenPossible

CheckId SA1410

Category MaintainabilityRules

Page 362: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAcalltoaC#anonymousmethoddoesnotcontainanymethodparameters,yetthestatementstillincludesparenthesis.

Page 363: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionWhenananonymousmethoddoesnotcontainanymethodparameters,theparenthesisaroundtheparametersareoptional.

Aviolationofthisruleoccurswhentheparenthesisarepresentonananonymousmethodcallwhichtakesnomethodparameters.Forexample:

this.Method(delegate(){return2;});

Theparenthesisareunnecessaryandshouldberemoved:

this.Method(delegate{return2;});

Page 364: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsRemovetheunnecessaryparenthesisafterthedelegatekeyword.

Page 365: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName AttributeConstructorMustNotUseUnnecessaryParenthesis

CheckId SA1411

Category MaintainabilityRules

Page 366: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTODO.

Page 367: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionTODO

Aviolationofthisruleoccurswhenunneccsaryparenthesishavebeenusedinanattributeconstructor.Forexample:

[Serializable()]Theparenthesisareunnecessaryandshouldberemoved:

[Serializable]

Page 368: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsRemovetheunnecessaryparenthesis.

Page 369: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Ruleswhichenforcenamingrequirementsformembers,types,andvariables.

SA1300:ElementMustBeginWithUpperCaseLetter

SA1301:ElementMustBeginWithLowerCaseLetter

SA1302:InterfaceNamesMustBeginWithI

SA1303:ConstFieldNamesMustBeginWithUpperCaseLetter

SA1304:NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter

SA1305:FieldNamesMustNotUseHungarianNotation

SA1306:FieldNamesMustBeginWithLowerCaseLetter

SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter

SA1308:VariableNamesMustNotBePrefixed

SA1309:FieldNamesMustNotBeginWithUnderscore

SA1310:FieldNamesMustNotContainUnderscore

Page 370: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementMustBeginWithUpperCaseLetter

CheckId SA1300

Category NamingRules

Page 371: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThenameofaC#elementdoesnotbeginwithanupper-caseletter.

Page 372: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthenamesofcertaintypesofelementsdonotbeginwithanupper-caseletter.Thefollowingtypesofelementsshoulduseanupper-caseletterasthefirstletteroftheelementname:namespaces,classes,enums,structs,delegates,events,methods,andproperties.

Inaddition,anyfieldwhichispublic,internal,ormarkedwiththeconstattributeshouldbeginwithanupper-caseletter.Non-privatereadonlyfieldsmustalsobenamedusinganupper-caseletter.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstobeginwithalower-caseletter,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 373: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,changethenameoftheelementsothatitbeginswithanupper-caseletter,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 374: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementMustBeginWithLowerCaseLetter

CheckId SA1301

Category NamingRules

Page 375: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTherearecurrentlynosituationsinwhichthisrulewillfire.

Page 376: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName InterfaceNamesMustBeginWithI

CheckId SA1302

Category NamingRules

Page 377: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThenameofaC#interfacedoesnotbeginwiththecapitalletterI.

Page 378: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthenameofaninterfacedoesnotbeginwiththecapitalletterI.InterfacenamesshouldalwaysbeginwithI.Forexample,ICustomer.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthuscannotbeginwiththeletterI,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 379: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addthecapitalletterItothefrontoftheinterfacename,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 380: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ConstFieldNamesMustBeginWithUpperCaseLetter

CheckId SA1303

Category NamingRules

Page 381: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThenameofaconstantC#fieldmustbeginwithanupper-caseletter.

Page 382: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthenameofafieldmarkedwiththeconstattributedoesnotbeginwithanupper-caseletter.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstobeginwithalower-caseletter,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 383: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,changethenameoftheconstantfieldsothatitbeginswithanupper-caseletter,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 384: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName NonPrivateReadonlyFieldsMustBeginWithUpperCaseLetter

CheckId SA1304

Category NamingRules

Page 385: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThenameofanon-privatereadonlyC#fieldmustbeingwithanupper-caseletter.

Page 386: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthenameofareadonlyfieldwhichisnotprivatedoesnotbeginwithanupper-caseletter.Non-privatereadonlyfieldsmustalwaysstartwithanupper-caseletter.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstobeginwithalower-caseletter,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 387: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,changethenameofthereadonlyfieldsothatitbeginswithanupper-caseletter,makethefieldprivate,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 388: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FieldNamesMustNotUseHungarianNotation

CheckId SA1305

Category NamingRules

Page 389: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThenameofafieldorvariableinC#usesHungariannotation.

Page 390: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenHungariannotationisusedinthenamingoffieldsandvariables.TheuseofHungariannotationhasbecomewidespreadinC++code,butthetrendinC#istouselonger,moredescriptivenamesforvariables,whicharenotbasedonthetypeofthevariablebutwhichinsteaddescribewhatthevariableisusedfor.

Inaddition,moderncodeeditorssuchasVisualStudiomakeiteasytoidentifytypeinformationforavariableorfield,typicallybyhoveringthemousecursoroverthevariablename.ThisreducestheneedforHungariannotation.

StyleCopassumesthatanyvariablenamethatbeginswithoneortwolower-caselettersfollowedbyanupper-caseletterismakinguseofHungariannotation,andwillflagaviolationofthisruleineachcase.Itispossibletodeclarecertainprefixesaslegal,inwhichcasetheywillbeignored.Forexample,avariablenamedonExecutewillappeartoStyleCoptobeusingHungariannotation,wheninrealityitisnot.Thus,theonprefixshouldbeflaggedasanallowedprefix.

Toconfigurethelistofallowedprefixes,bringuptheStyleCopsettingsforaproject,andnavigatetotheHungariantab,asshownbelow:

AddingaoneortwoletterprefixtothislistwillcauseStyleCoptoignore

Page 391: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

variablesorfieldswhichbeginwiththisprefix.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstouseHungariannotation,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 392: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheHungariannotationfromthefieldorvariablename,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 393: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FieldNamesMustBeginWithLowerCaseLetter

CheckId SA1306

Category NamingRules

Page 394: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThenameofafieldorvariableinC#doesnotbeginwithalower-caseletter.

Page 395: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthenameofafieldorvariablebeginswithanupper-caseletter.Constantsmustalwaysstartwithanuppercaseletter,whilstreadonlyfieldsmaystartwitheitheranuppercaseoralowercaseletter.Also,publicorinternalfieldsmustalwaysstartwithanuppercaseletter.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstobeginwithanupper-caseletter,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 396: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,changethenameofthefieldorvariablesothatitbeginswithalower-caseletter,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 397: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName AccessibleFieldsMustBeginWithUpperCaseLetter

CheckId SA1307

Category NamingRules

Page 398: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThenameofapublicorinternalfieldinC#doesnotbeginwithanupper-caseletter.

Page 399: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthenameofapublicorinternalfieldbeginswithalower-caseletter.Publicorinternalfieldsmustbeingwithanupper-caseletter.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstostartwithalower-caseletter,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 400: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,changethenameofthefieldsothatitbeginswithanupper-caseletter,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 401: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName VariableNamesMustNotBePrefixed

CheckId SA1308

Category NamingRules

Page 402: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAfieldnameinC#isprefixedwithm_ors_.

Page 403: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenafieldnameisprefixedbym_ors_.

Bydefault,StyleCopdisallowstheuseofunderscores,m_,etc.,tomarklocalclassfields,infavorofthe‘this.’prefix.Theadvantageofusing‘this.’isthatitappliesequallytoallelementtypesincludingmethods,properties,etc.,andnotjustfields,makingallcallstoclassmembersinstantlyrecognizable,regardlessofwhicheditorisbeingusedtoviewthecode.Anotheradvantageisthatitcreatesaquick,recognizabledifferentiationbetweeninstancemembersandstaticmembers,whichwillnotbeprefixed.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstobeginwiththeprefix,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 404: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheprefixfromthebeginningofthefieldname,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 405: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FieldNamesMustNotBeginWithUnderscore

CheckId SA1309

Category NamingRules

Page 406: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAfieldnameinC#beginswithanunderscore.

Page 407: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenafieldnamebeginswithanunderscore.

Bydefault,StyleCopdisallowstheuseofunderscores,m_,etc.,tomarklocalclassfields,infavorofthe‘this.’prefix.Theadvantageofusing‘this.’isthatitappliesequallytoallelementtypesincludingmethods,properties,etc.,andnotjustfields,makingallcallstoclassmembersinstantlyrecognizable,regardlessofwhicheditorisbeingusedtoviewthecode.Anotheradvantageisthatitcreatesaquick,recognizabledifferentiationbetweeninstancemembersandstaticmembers,whichwillnotbeprefixed.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstobeginwithanunderscore,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 408: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheunderscorefromthebeginningofthefieldname,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 409: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName FieldNamesMustNotContainUnderscore

CheckId SA1310

Category NamingRules

Page 410: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAfieldnameinC#containsanunderscore.

Page 411: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenafieldnamecontainsanunderscore.Fieldsandvariablesshouldbenamedusingdescriptive,readablewordingwhichdescribesthefunctionofthefieldorvariable.Typically,thesenameswillbewrittenusingcamelcase,andshouldnotuseunderscores.Forexample,usecustomerPostCoderatherthancustomer_post_code.

IfthefieldorvariablenameisintendedtomatchthenameofanitemassociatedwithWin32orCOM,andthusneedstoincludeunderscores,placethefieldorvariablewithinaspecialNativeMethodsclass.ANativeMethodsclassisanyclasswhichcontainsanameendinginNativeMethods,andisintendedasaplaceholderforWin32orCOMwrappers.StyleCopwillignorethisviolationiftheitemisplacedwithinaNativeMethodsclass.

Page 412: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheunderscorefromthenameofthefield,orplacetheitemwithinaNativeMethodsclassifappropriate.

Page 413: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Ruleswhichenforceastandardorderingschemeforcodecontents.

SA1200:UsingDirectivesMustBePlacedWithinNamespace

SA1201:ElementsMustAppearInTheCorrectOrder

SA1202:ElementsMustBeOrderedByAccess

SA1203:ConstantsMustAppearBeforeFields

SA1204:StaticElementsMustAppearBeforeInstanceElements

SA1205:PartialElementsMustDeclareAccess

SA1206:DeclarationKeywordsMustFollowOrder

SA1207:ProtectedMustComeBeforeInternal

SA1208:SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives

SA1209:UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives

SA1210:UsingDirectivesMustBeOrderedAlphabeticallyByNamespace

SA1211:UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName

SA1212:PropertyAccessorsMustFollowOrder

SA1213:EventAccessorsMustFollowOrder

Page 414: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName UsingDirectivesMustBePlacedWithinNamespace

CheckId SA1200

Category OrderingRules

Page 415: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#usingdirectiveisplacedoutsideofanamespaceelement.

Page 416: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenausingdirectiveorausing-aliasdirectiveisplacedoutsideofanamespaceelement,unlessthefiledoesnotcontainanynamespaceelements.

Forexample,thefollowingcodewouldresultintwoviolationsofthisrule.

usingSystem;

usingGuid=System.Guid;

namespaceMicrosoft.Sample

{

publicclassProgram

{

}

}

Thefollowingcode,however,wouldnotresultinanyviolationsofthisrule:

namespaceMicrosoft.Sample

{

usingSystem;

usingGuid=System.Guid;

publicclassProgram

Page 417: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

}

}

Therearesubtledifferencesbetweenplacingusingdirectiveswithinanamespaceelement,ratherthanoutsideofthenamespace,including:

1.Placingusing-aliasdirectiveswithinthenamespaceeliminatescompilerconfusionbetweenconflictingtypes.

2.Whenmultiplenamespacesaredefinedwithinasinglefile,placingusingdirectiveswithinthenamespaceelementsscopesreferencesandaliases.

1.EliminatingTypeConfusion

Considerthefollowingcode,whichcontainsausing-aliasdirectivedefinedoutsideofthenamespaceelement.ThecodecreatesanewclasscalledGuid,andalsodefinesausing-aliasdirectivetomapthenameGuidtothetypeSystem.Guid.Finally,thecodecreatesaninstanceofthetypeGuid:

usingGuid=System.Guid;

namespaceMicrosoft.Sample

{

publicclassGuid

{

publicGuid(strings)

{

}

}

Page 418: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

publicclassProgram

{

publicstaticvoidMain(string[]args)

{

Guidg=newGuid("hello");

}

}

}

Thiscodewillcompilecleanly,withoutanycompilererrors.However,itisunclearwhichversionoftheGuidtypeisbeingallocated.Iftheusingdirectiveismovedinsideofthenamespace,asshownbelow,acompilererrorwilloccur:

namespaceMicrosoft.Sample

{

usingGuid=System.Guid;

publicclassGuid

{

publicGuid(strings)

{

Page 419: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

}

publicclassProgram

{

publicstaticvoidMain(string[]args)

{

Guidg=newGuid("hello");

}

}

}

Thecodefailsonthefollowingcompilererror,foundonthelinecontainingGuidg=newGuid("hello");

CS0576:Namespace'Microsoft.Sample'containsadefinitionconflictingwithalias'Guid'

ThecodecreatesanaliastotheSystem.GuidtypecalledGuid,andalsocreatesitsowntypecalledGuidwithamatchingconstructorinterface.Later,thecodecreatesaninstanceofthetypeGuid.Tocreatethisinstance,thecompilermustchoosebetweenthetwodifferentdefinitionsofGuid.Whentheusing-aliasdirectiveisplacedoutsideofthenamespaceelement,thecompilerwillchoosethelocaldefinitionofGuiddefinedwithinthelocalnamespace,andcompletelyignoretheusing-aliasdirectivedefinedoutsideofthenamespace.This,unfortunately,isnotobviouswhenreadingthecode.

Page 420: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Whentheusing-aliasdirectiveispositionedwithinthenamespace,however,thecompilerhastochoosebetweentwodifferent,conflictingGuidtypesbothdefinedwithinthesamenamespace.Bothofthesetypesprovideamatchingconstructor.Thecompilerisunabletomakeadecision,soitflagsthecompilererror.

Placingtheusing-aliasdirectiveoutsideofthenamespaceisabadpracticebecauseitcanleadtoconfusioninsituationssuchasthis,whereitisnotobviouswhichversionofthetypeisactuallybeingused.Thiscanpotentiallyleadtoabugwhichmightbedifficulttodiagnose.

Placingusing-aliasdirectiveswithinthenamespaceelementeliminatesthisasasourceofbugs.

2.MultipleNamespaces

Placingmultiplenamespaceelementswithinasinglefileisgenerallyabadidea,butifandwhenthisisdone,itisagoodideatoplaceallusingdirectiveswithineachofthenamespaceelements,ratherthangloballyatthetopofthefile.Thiswillscopethenamespacestightly,andwillalsohelptoavoidthekindofbehaviordescribedabove.

Itisimportanttonotethatwhencodehasbeenwrittenwithusingdirectivesplacedoutsideofthenamespace,careshouldbetakenwhenmovingthesedirectiveswithinthenamespace,toensurethatthisisnotchangingthesemanticsofthecode.Asexplainedabove,placingusing-aliasdirectiveswithinthenamespaceelementallowsthecompilertochoosebetweenconflictingtypesinwaysthatwillnothappenwhenthedirectivesareplacedoutsideofthenamespace.

Page 421: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,moveallusingdirectivesandusing-aliasdirectiveswithinthenamespaceelement.

Page 422: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementsMustAppearInTheCorrectOrder

CheckId SA1201

Category OrderingRules

Page 423: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnelementwithinaC#codefileisoutoforderinrelationtotheotherelementsinthecode.

Page 424: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodeelementswithinafiledonotfollowastandardorderingscheme.

Tocomplywiththisrule,elementsatthefilerootlevelorwithinanamespacemustbepositionedinthefollowingorder:

ExternAliasDirectives

UsingDirectives

Namespaces

Delegates

Enums

Interfaces

Structs

Classes

Withinaclass,struct,orinterface,elementsmustbepositionedinthefollowingorder:

Fields

Constructors

Finalizers(Destructors)

Delegates

Events

Enums

Interfaces

Properties

Indexers

Methods

Structs

Classes

Page 425: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Complyingwithastandardorderingschemebasedonelementtypecanincreasethereadabilityandmaintainabilityofthefileandencouragecodereuse.

Whenimplementinganinterface,itissometimesdesirabletogroupallmembersoftheinterfacenexttooneanother.Thiswillsometimesrequireviolatingthisrule,iftheinterfacecontainselementsofdifferenttypes.Thisproblemcanbesolvedthroughtheuseofpartialclasses.

1.Addthepartialattributetotheclass,iftheclassisnotalreadypartial.

2.Addasecondpartialclasswiththesamename.Itispossibletoplacethisinthesamefile,justbelowtheoriginalclass,orwithinasecondfile.

3.Movetheinterfaceinheritanceandallmembersoftheinterfaceimplementationtothesecondpartoftheclass.

Forexample:

///<summary>

///Representsacustomerofthesystem.

///</summary>

publicpartialclassCustomer

{

//Containsthemainfunctionalityoftheclass.

}

///<content>

///ImplementstheICollectionclass.

///</content>

publicpartialclassCustomer:ICollection

Page 426: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

publicintCount

{

get{returnthis.count;}

}

publicboolIsSynchronized

{

get{returnfalse;}

}

publicobjectSyncRoot

{

get{returnnull;}

}

publicvoidCopyTo(Arrayarray,intindex)

{

thrownewNotImplementedException();

}

Page 427: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

Page 428: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,ordertheelementsinthefileintheorderdescribedabove.

Page 429: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ElementsMustBeOrderedByAccess

CheckId SA1202

Category OrderingRules

Page 430: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnelementwithinaC#codefileisoutoforderwithinregardtoaccesslevel,inrelationtootherelementsinthecode.

Page 431: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodeelementswithinafiledonotfollowastandardorderingschemebasedonaccesslevel.

Tocomplywiththisrule,adjacentelementsofthesametypemustbepositionedinthefollowingorderbyaccesslevel:

public

internal

protectedinternal

protected

private

Complyingwithastandardorderingschemebasedonaccesslevelcanincreasethereadabilityandmaintainabilityofthefileandmakeiteasiertoidentifythepublicinterfacethatisbeingexposedfromaclass.

Page 432: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,ordertheelementsinthefileintheorderdescribedabove.

Page 433: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ConstantsMustAppearBeforeFields

CheckId SA1203

Category OrderingRules

Page 434: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAconstfieldisplacedbeneathanon-constfield.

Page 435: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaconstfieldisplacedbeneathanon-constfield.Constantsmustbeplacedabovefieldstoindicatethatthetwoarefundamentallydifferenttypesofelementswithdifferentconsiderationsforthecompiler,differentnamingrequirements,etc.

Page 436: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,placeallconstsaboveallfields.

Page 437: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName StaticElementsMustAppearBeforeInstanceElements

CheckId SA1204

Category OrderingRules

Page 438: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAstaticelementispositionedbeneathaninstanceelementofthesametype.

Page 439: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenastaticelementispositionedbeneathaninstanceelementofthesametype.Allstaticelementsmustbeplacedaboveallinstanceelementsofthesametypetomakeiteasiertoseetheinterfaceexposedfromtheinstanceandstaticversionoftheclass.

Page 440: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,placeallstaticelementsaboveallinstanceelementsofthesametype.

Page 441: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PartialElementsMustDeclareAccess

CheckId SA1205

Category OrderingRules

Page 442: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThepartialelementdoesnothaveanaccessmodifierdefined.StyleCopmaynotbeabletodeterminethecorrectplacementoftheelementsinthefile.Pleasedeclareanaccessmodifierforallpartialelements.

Page 443: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthepartialelementsdoesnothaveanaccessmodifierdefined.

Page 444: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,specifyanacessmodifierforthepartialelement.

Page 445: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DeclarationKeywordsMustFollowOrder

CheckId SA1206

Category OrderingRules

Page 446: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThekeywordswithinthedeclarationofanelementdonotfollowastandardorderingscheme.

Page 447: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthekeywordswithinanelement’sdeclarationdonotfollowastandardorderingscheme.

Withinanelementdeclaration,keywordsmustappearinthefollowingorder:

Accessmodifiers

static

Allotherkeywords

Usingastandardorderingschemeforelementdeclarationkeywordscanmakethecodemorereadablebyhighlightingtheaccesslevelofeachelement.Thiscanhelppreventelementsfrombeinggivenahigheraccesslevelthanneeded.

Page 448: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowToFixViolationsTofixaninstanceofthisviolation,orderthekeywordsintheelement’sdeclarationasdescribedabove.

Page 449: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ProtectedMustComeBeforeInternal

CheckId SA1207

Category OrderingRules

Page 450: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThekeywordprotectedispositionedafterthekeywordinternalwithinthedeclarationofaprotectedinternalC#element.

Page 451: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaprotectedinternalelement’saccessmodifiersarewrittenasinternalprotected.Inreality,anelementwiththekeywordsprotectedinternalwillhavethesameaccesslevelasanelementwiththekeywordsinternalprotected.Tomakethecodeeasiertoreadandmoreconsistent,StyleCopstandardizestheorderingofthesekeywords,sothataprotectedinternalelementwillalwaysbedescribedassuch,andneverasinternalprotected.Thiscanhelptoreduceconfusionaboutwhethertheseaccesslevelsareindeedthesame.

Page 452: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,placetheprotectedkeywordbeforetheinternalkeyword.

Page 453: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives

CheckId SA1208

Category OrderingRules

Page 454: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAusingdirectivewhichdeclaresamemberoftheSystemnamespaceappearsafterausingdirectivewhichdeclaresamemberofadifferentnamespace,withinaC#codefile.

Page 455: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenausingdirectivefortheSystemnamespaceisplacedafteranon-Systemusingdirective.PlacingallSystemusingdirectivesatthetopoftheusingdirectivescanmakethecodecleanerandeasiertoread,andcanhelpmakeiteasiertoidentifythenamespacesthatarebeingusedbythecode.

Page 456: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,placetheSystemusingdirectiveaboveallusingdirectivesforothernamespaces.

Page 457: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives

CheckId SA1209

Category OrderingRules

Page 458: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAusing-aliasdirectiveispositionedbeforearegularusingdirective.

Page 459: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenausing-aliasdirectiveisplacedbeforeanormalusingdirective.Using-aliasdirectiveshavespecialbehaviorwhichcanalterthemeaningoftherestofthecodewithinthefileornamespace.Placingtheusing-aliasdirectivestogetherbelowallotherusing-directivescanmakethecodecleanerandeasiertoread,andcanhelpmakeiteasiertoidentifythetypesusedthroughoutthecode.

Page 460: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,placeallusing-aliasdirectivesbeneathallnormalusingdirectives.

Page 461: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName UsingDirectivesMustBeOrderedAlphabeticallyByNamespace

CheckId SA1210

Category OrderingRules

Page 462: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheusingdirectiveswithinaC#codefilearenotsortedalphabeticallybynamespace.

Page 463: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheusingdirectivesarenotsortedalphabeticallybynamespace.Sortingtheusingdirectivesalphabeticallymakesthecodecleanerandeasiertoread,andcanhelpmakeiteasiertoidentifythenamespacesthatarebeingusedbythecode.TheSystemnamespacesareanexceptiontothisruleandwillalwaysprecedeallothernamespaces.SeeSA1208formoredetails.

Page 464: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,ordertheusingdirectivesalphabeticallybynamespacewithalltheSystemnamespaceentriesfirst.

Page 465: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName

CheckId SA1211

Category OrderingRules

Page 466: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheusing-aliasdirectiveswithinaC#codefilearenotsortedalphabeticallybyaliasname.

Page 467: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheusing-aliasdirectivesarenotsortedalphabeticallybyaliasname.Sortingtheusing-aliasdirectivesalphabeticallycanmakethecodecleanerandeasiertoread,andcanhelpmakeiteasiertoidentifythenamespacesthatarebeingusedbythecode.

Page 468: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,ordertheusing-aliasdirectivesalphabeticallybyaliasname.

Page 469: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PropertyAccessorsMustFollowOrder

CheckId SA1212

Category OrderingRules

Page 470: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAgetaccessorappearsafterasetaccessorwithinapropertyorindexer.

Page 471: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenagetaccessorisplacedafterasetaccessorwithinapropertyorindexer.Tocomplywiththisrule,thegetaccessorshouldappearbeforethesetaccessor.

Forexample,thefollowingcodewouldraiseaninstanceofthisviolation:

publicstringName{

set{this.name=value;}

get{returnthis.name;}

}Thecodebelowwouldnotraisethisviolation:

publicstringName{get{returnthis.name;}

set{this.name=value;}

}

Page 472: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,placethegetaccessorbeforethesetaccessor.

Page 473: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName EventAccessorsMustFollowOrder

CheckId SA1213

Category OrderingRules

Page 474: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnaddaccessorappearsafteraremoveaccessorwithinanevent.

Page 475: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenanaddaccessorisplacedafteraremoveaccessorwithinanevent.Tocomplywiththisrule,theaddaccessorshouldappearbeforetheremoveaccessor.

Forexample,thefollowingcodewouldraiseaninstanceofthisviolation:

publiceventEventHandlerNameChanged{

remove{this.nameChanged-=value;}

add{this.nameChanged+=value;}

}Thecodebelowwouldnotraisethisviolation:

publiceventEventHandlerNameChanged{add{this.nameChanged+=value;}

remove{this.nameChanged-=value;}

}

Page 476: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaninstanceofthisviolation,placetheaddaccessorbeforetheremoveaccessor.

Page 477: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Ruleswhichensurethatthecodeiswell-formattedandreadable.

SA1100:DoNotPrefixCallsWithBaseUnlessLocalImplementationExists

SA1101:PrefixLocalCallsWithThis

SA1102:QueryClauseMustFollowPreviousClause

SA1103:QueryClausesMustBeOnSeparateLinesOrAllOnOneLine

SA1104:QueryClauseMustBeginOnNewLineWhenPreviousClauseSpansMultipleLines

SA1105:QueryClausesSpanningMultipleLinesMustBeginOnOwnLine

SA1106:CodeMustNotContainEmptyStatements

SA1107:CodeMustNotContainMultipleStatementsOnOneLine

SA1108:BlockStatementsMustNotContainEmbeddedComments

SA1109:BlockStatementsMustNotContainEmbeddedRegions

SA1110:OpeningParenthesisMustBeOnDeclarationLine

SA1111:ClosingParenthesisMustBeOnLineOfOpeningParenthesis

SA1112:ClosingParenthesisMustBeOnLineOfOpeningParenthesis

SA1113:CommaMustBeOnSameLineAsPreviousParameter

SA1114:ParameterListMustFollowDeclaration

SA1115:ParameterMustFollowComma

SA1116:SplitParametersMustStartOnLineAfterDeclaration

SA1117:ParametersMustBeOnSameLineOrSeparateLines

SA1118:ParameterMustNotSpanMultipleLines

SA1120:CommentsMustContainText

SA1121:UseBuiltInTypeAlias

SA1122:UseStringEmptyForEmptyStrings

SA1123:DoNotPlaceRegionsWithinElements

SA1124:DoNotUseRegions

Page 478: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

SA1125:UseShorthandForNullableTypes

Page 479: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DoNotPrefixCallsWithBaseUnlessLocalImplementationExists

CheckId SA1100

Category ReadabilityRules

Page 480: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAcalltoamemberfromaninheritedclassbeginswith‘base.’,andthelocalclassdoesnotcontainanoverrideorimplementationofthemember.

Page 481: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneverthecodecontainsacalltoamemberfromthebaseclassprefixedwith‘base.’,andthereisnolocalimplementationofthemember.Forexample:

stringname=base.JoinName("John","Doe");

Thisruleisinplacetopreventapotentialsourceofbugs.Considerabaseclasswhichcontainsthefollowingvirtualmethod:

publicvirtualstringJoinName(stringfirst,stringlast)

{

}

Anotherclassinheritsfromthisbaseclassbutdoesnotprovidealocaloverrideofthismethod.Somewherewithinthisclass,thebaseclassmethodiscalledusingbase.JoinName(...).Thisworksasexpected.Atalaterdate,someoneaddsalocaloverrideofthismethodtotheclass:

publicoverridestringJoinName(stringfirst,stringlast)

{

return“Bob”;

}

Atthispoint,thelocalcalltobase.JoinName(...)mostlikelyintroducesabugintothecode.Thiscallwillalwayscallthebaseclassmethodandwillcausethelocaloverridetobeignored.

Forthisreason,callstomembersfromabaseclassshouldnotbeginwith‘base.’,unlessalocaloverrideisimplemented,andthedeveloperwantsto

Page 482: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

specificallycallthebaseclassmember.Whenthereisnolocaloverrideofthebaseclassmember,thecallshouldbeprefixedwith'this.'ratherthan'base.'.

Page 483: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,changethe‘base.’prefixto‘this.’.

Page 484: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PrefixLocalCallsWithThis

CheckId SA1101

Category ReadabilityRules

Page 485: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAcalltoaninstancememberofthelocalclassorabaseclassisnotprefixedwith‘this.’,withinaC#codefile.

Page 486: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneverthecodecontainsacalltoaninstancememberofthelocalclassorabaseclasswhichisnotprefixedwith‘this.’.Anexceptiontothisruleoccurswhenthereisalocaloverrideofabaseclassmember,andthecodeintendstocallthebaseclassmemberdirectly,bypassingthelocaloverride.Inthiscasethecallcanbeprefixedwith‘base.’ratherthan‘this.’.

Bydefault,StyleCopdisallowstheuseofunderscoresorm_tomarklocalclassfields,infavorofthe‘this.’prefix.Theadvantageofusing‘this.’isthatitappliesequallytoallelementtypesincludingmethods,properties,etc.,andnotjustfields,makingallcallstoclassmembersinstantlyrecognizable,regardlessofwhicheditorisbeingusedtoviewthecode.Anotheradvantageisthatitcreatesaquick,recognizabledifferentiationbetweeninstancemembersandstaticmembers,whicharenotbeprefixed.

Afinaladvantageofusingthe‘this.’prefixisthattypingthis.willcauseVisualStudiotoshowtheIntelliSensepopup,makingitquickandeasyforthedevelopertochoosetheclassmembertocall.

Page 487: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,insertthe‘this.’prefixbeforethecalltotheclassmember.

Page 488: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName QueryClauseMustFollowPreviousClause

CheckId SA1102

Category ReadabilityRules

Page 489: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#queryclausedoesnotbeginonthesamelineasthepreviousclause,oronthenextline.

Page 490: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaclausewithinaqueryexpressiondoesnotbeginonthesamelineasthepreviousclause,oronthelineafterthequeryclause.Forexample:

objectx=selectainb

fromc;

Thequeryclausecancorrectlybewrittenas:

objectx=selectainbfromc;

or:

objectx=

selecta

inb

fromc;

Page 491: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethateachclauseinthequeryexpressionbeginsonthesamelineasthepreviousclause,oronthefollowingline.

Page 492: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName QueryClausesMustBeOnSeparateLinesOrAllOnOneLine

CheckId SA1103

Category ReadabilityRules

Page 493: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheclauseswithinaC#queryexpressionarenotallplacedonthesameline,andeachclauseisnotplacedonitsownline.

Page 494: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthequeryclausesarenoteitherplacedallonthesameline,oreachonitsownline.Forexample:

objectx=selectainb

fromc;

Thequeryclausecancorrectlybewrittenas:

objectx=selectainbfromc;

or:

objectx=

selecta

inb

fromc;

Page 495: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatallclausesareplacedtogetheronthesameline,oreachclausebeginsonitsownline.

Page 496: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName QueryClauseMustBeginOnNewLineWhenPreviousClauseSpansMultipleLines

CheckId SA1104

Category ReadabilityRules

Page 497: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclausewithinaC#queryexpressionbeginsonthesamelineasthepreviousclause,whenthepreviousclausespansacrossmultiplelines.

Page 498: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaqueryclausespansacrossmultiplelines,andthenextclausebeginsonthesamelineastheendofthepreviousclause.

objectx=

selecta

inb.GetCustomers(

2,“x”)fromc;

Thequeryclausecancorrectlybewrittenas:

objectx=

selecta

inb.GetCustomers(

2,“x”)

fromc;

Page 499: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,movetheclausedowntostartonthenextline.

Page 500: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName QueryClausesSpanningMultipleLinesMustBeginOnOwnLine

CheckId SA1105

Category ReadabilityRules

Page 501: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclausewithinaC#queryexpressionspansacrossmultiplelines,anddoesnotbeginonitsownline.

Page 502: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenaqueryclausespansacrossmultiplelines,butdoesnotbeginonitsownline.Forexample:

objectx=

selectainbfromc.GetCustomers(

2,“x”);

Thequeryclausecancorrectlybewrittenas:

objectx=

selecta

inbfromc.GetCustomers(

2,“x”);

Page 503: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,movetheclausedowntostartonthenextline.

Page 504: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeMustNotContainEmptyStatements

CheckId SA1106

Category ReadabilityRules

Page 505: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codecontainsanextrasemicolon.

Page 506: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainanextrasemicolon.Syntactically,thisresultsinanextra,emptystatementinthecode.

Page 507: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheunneededsemicolon.

Page 508: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeMustNotContainMultipleStatementsOnOneLine

CheckId SA1107

Category ReadabilityRules

Page 509: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codecontainsmorethanonestatementonasingleline.

Page 510: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainmorethanonestatementonthesameline.Eachstatementmustbeginonanewline.

Page 511: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,moveeachstatementtobeginonitsownline.

Page 512: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName BlockStatementsMustNotContainEmbeddedComments

CheckId SA1108

Category ReadabilityRules

Page 513: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#statementcontainsacommentbetweenthedeclarationofthestatementandtheopeningcurlybracketofthestatement.

Page 514: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainsacommentinbetweenthedeclarationandtheopeningcurlybracket.Forexample:

if(x!=y)

//Makesurexdoesnotequaly

{

}

Thecommentcanlegallybeplacedabovethestatement,orwithinthebodyofthestatement:

//Makesurexdoesnotequaly

if(x!=y)

{

}

if(x!=y)

{

//Makesurexdoesnotequaly

}

Ifthecommentisbeingusedtocommentoutalineofcode,beginthecommentwithfourforwardslashesratherthantwo:

if(x!=y)

Page 515: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

////if(x==y)

{

}

Page 516: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,movethecommentabovethestatement,withinthebodyofthestatement,orremovethecomment.

Page 517: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName BlockStatementsMustNotContainEmbeddedRegions

CheckId SA1109

Category ReadabilityRules

Page 518: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#statementcontainsaregiontagbetweenthedeclarationofthestatementandtheopeningcurlybracketofthestatement.

Page 519: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainsaregiontaginbetweenthedeclarationandtheopeningcurlybracket.Forexample:

if(x!=y)

#region

{

}

#endregion

Thiswillresultinthebodyofthestatementbeinghiddenwhentheregioniscollapsed.

Page 520: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheregionormoveitoutsideofthestatement.

Page 521: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningParenthesisMustBeOnDeclarationLine

CheckId SA1110

Category ReadabilityRules

Page 522: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheopeningparenthesisorbracketinacalltoaC#methodorindexer,orthedeclarationofamethodorindexer,isnotplacedonthesamelineasthemethodorindexername.

Page 523: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheopeningbracketofamethodorindexercallordeclarationisnotplacedonthesamelineasthemethodorindexer.Thefollowingexamplesshowcorrectplacementoftheopeningbracket:

publicstringJoinName(stringfirst,stringlast)

{

returnJoinStrings(

first,last);

}

publicintthis[intx]

{

get{returnthis.items[x];}

}

Page 524: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethattheopeningbracketisplacedonthesamelineasthenameofthemethodorindexer.

Page 525: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingParenthesisMustBeOnLineOfLastParameter

CheckId SA1111

Category ReadabilityRules

Page 526: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheclosingparenthesisorbracketinacalltoaC#methodorindexer,orthedeclarationofamethodorindexer,isnotplacedonthesamelineasthelastparameter.

Page 527: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheclosingbracketofamethodorindexercallordeclarationisnotplacedonthesamelineasthelastparameter.Thefollowingexamplesshowcorrectplacementofthebracket:

publicstringJoinName(stringfirst,stringlast)

{

stringname=JoinStrings(

first,

last);

}

publicintthis[intx]

{

get{returnthis.items[x];}

}

Page 528: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethattheclosingbracketisplacedonthesamelineasthelastparameter.

Page 529: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingParenthesisMustBeOnLineOfOpeningParenthesis

CheckId SA1112

Category ReadabilityRules

Page 530: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheclosingparenthesisorbracketinacalltoaC#methodorindexer,orthedeclarationofamethodorindexer,isnotplacedonthesamelineastheopeningbracketwhentheelementdoesnottakeanyparameters.

Page 531: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenamethodorindexerdoesnottakeanyparametersandtheclosingbracketofacallordeclarationforthemethodorindexerisnotplacedonthesamelineastheopeningbracket.Thefollowingexampleshowscorrectplacementoftheclosingparenthesis:

publicstringGetName()

{

returnthis.name.Trim();

}

Page 532: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethattheclosingbracketisplacedonthesamelineastheopeningbracket.

Page 533: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CommaMustBeOnSameLineAsPreviousParameter

CheckId SA1113

Category ReadabilityRules

Page 534: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAcommabetweentwoparametersinacalltoaC#methodorindexer,orinthedeclarationofamethodorindexer,isnotplacedonthesamelineasthepreviousparameter.

Page 535: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenacommabetweentwoparameterstoamethodorindexerisnotplacedonthesamelineasthepreviousparameter.Thefollowingexamplesshowcorrectplacementofthecomma:

publicstringJoinName(stringfirst,stringlast)

{

stringname=JoinStrings(

first,

last);

}

publicintthis[intx,

inty]

{

get{returnthis.items[x,y];}

}

Page 536: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthecommaisplacedonthesamelineasthepreviousparameter.

Page 537: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ParameterListMustFollowDeclaration

CheckId SA1114

Category ReadabilityRules

Page 538: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThestartoftheparameterlistforamethodorindexercallordeclarationdoesnotbeginonthesamelineastheopeningbracket,oronthelineaftertheopeningbracket.

Page 539: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthereareoneormoreblanklinesbetweentheopeningbracketandthestartoftheparameterlist.Forexample:

publicstringJoinName(

stringfirst,stringlast)

{

}

Theparameterlistmustbeginonthesamelineastheopeningbracket,oronthenextline.Forexample:

publicstringJoinName(stringfirst,stringlast)

{

}

publicstringJoinName(

stringfirst,stringlast)

{

}

Page 540: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethattheparameterlistbeginsonthesamelineastheopeningbracket,oronthenextline.

Page 541: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ParameterMustFollowComma

CheckId SA1115

Category ReadabilityRules

Page 542: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAparameterwithinaC#methodorindexercallordeclarationdoesnotbeginonthesamelineasthepreviousparameter,oronthenextline.

Page 543: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthereareoneormoreblanklinesbetweenaparameterandthepreviousparameter.Forexample:

publicstringJoinName(

stringfirst,

stringlast)

{

}

Theparametermustbeginonthesamelineasthepreviouscomma,oronthenextline.Forexample:

publicstringJoinName(stringfirst,stringlast)

{

}

publicstringJoinName(

stringfirst,

stringlast)

{

}

Page 544: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)
Page 545: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethattheparameterbeginsonthesamelineasthepreviouscomma,oronthenextline.

Page 546: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SplitParametersMustStartOnLineAfterDeclaration

CheckId SA1116

Category ReadabilityRules

Page 547: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheparameterstoaC#methodorindexercallordeclarationspanacrossmultiplelines,butthefirstparameterdoesnotstartonthelineaftertheopeningbracket.

Page 548: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheparameterstoamethodorindexerspanacrossmultiplelines,butthefirstparameterdoesnotstartonthelineaftertheopeningbracket.Forexample:

publicstringJoinName(stringfirst,

stringlast)

{

}

Theparametersmustbeginonthelineafterthedeclaration,whenevertheparameterspanacrossmultiplelines:

publicstringJoinName(

stringfirst,

stringlast)

{

}

Page 549: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthefirstparameterstartsonthelineaftertheopeningbracket,orplaceallparametersonthesamelineiftheparametersarenottoolong.

Page 550: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ParametersMustBeOnSameLineOrSeparateLines

CheckId SA1117

Category ReadabilityRules

Page 551: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheparameterstoaC#methodorindexercallordeclarationarenotallonthesamelineoreachonaseparateline.

Page 552: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheparameterstoamethodorindexerarenotallonthesamelineoreachonitsownline.Forexample:

publicstringJoinName(stringfirst,stringmiddle,

stringlast)

{

}

Theparameterscanallbeplacedonthesameline:

publicstringJoinName(stringfirst,stringmiddle,stringlast)

{

}

publicstringJoinName(

stringfirst,stringmiddle,stringlast)

{

}

Alternatively,eachparametercanbeplacedonitsownline:

publicstringJoinName(

stringfirst,

stringmiddle,

stringlast)

Page 553: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

}

Page 554: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,placeallparametersonthesameline,orplaceeachparameteronitsownline.

Page 555: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ParameterMustNotSpanMultipleLines

CheckId SA1118

Category ReadabilityRules

Page 556: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAparametertoaC#methodorindexer,otherthanthefirstparameter,spansacrossmultiplelines.

Page 557: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionTopreventmethodcallsfrombecomingexcessivelycomplicatedandunreadable,onlythefirstparametertoamethodorindexercallisallowedtospanacrossmultiplelines.Theexceptionisananonymousmethodpassedasaparameter,whichisalwaysallowedtospanmultiplelines.Aviolationofthisruleoccurswheneveraparameterotherthanthefirstparameterspansacrossmultiplelines,andtheparameterdoesnotcontainananonymousmethod.

Forexample,thefollowingcodewouldviolatethisrule,sincethesecondparameterspansacrossmultiplelines:

returnJoinStrings(

"John",

"Smith"+

"Doe");

Whenparametersotherthanthefirstparameterspanacrossmultiplelines,itcanbedifficulttotellhowmanyparametersarepassedtothemethod.Ingeneral,thecodebecomesdifficulttoread.

Tofixtheexampleabove,ensurethattheparametersafterthefirstparameterdonotspanacrossmultiplelines.Ifthiswillcauseaparametertobeexcessivelylong,storethevalueoftheparameterwithinatemporaryvariable.Forexample:

stringlast="Smith"+

"Doe";

returnJoinStrings(

"John",

Page 558: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

last);

Insomecases,thiswillallowthemethodtobewrittenevenmoreconcisely,suchas:

returnJoinStrings("John",last);

Page 559: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethattheparametersafterthefirstparameterdonotspanmultiplelines,unlesstheparametercontainsananonymousmethod.

Page 560: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CommentsMustContainText

CheckId SA1120

Category ReadabilityRules

Page 561: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#commentdoesnotcontainanycommenttext.

Page 562: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneverthecodecontainsaC#commentwhichdoesnotcontainanytext.

Page 563: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addtexttothecomment,orremovethecomment.

Page 564: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName UseBuiltInTypeAlias

CheckId SA1121

Category ReadabilityRules

Page 565: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThecodeusesoneofthebasicC#types,butdoesnotusethebuilt-inaliasforthetype.

Page 566: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenoneofthefollowingtypesareusedanywhereinthecode:Boolean,Byte,Char,Decimal,Double,Int16,Int32,Int64,Object,SByte,Single,String,UInt16,UInt32,UInt64.

Aviolationalsooccurswhenanyofthesetypesarerepresentedinthecodeusingthefullnamespaceforthetype:System.Boolean,System.Byte,System.Char,System.Decimal,System.Double,System.Int16,System.Int32,System.Int64,System.Object,System.SByte,System.Single,System.String,System.UInt16,System.UInt32,System.UInt64.

Ratherthanusingthetypenameorthefully-qualifiedtypename,thebuilt-inaliasesforthesetypesshouldalwaysbeused:bool,byte,char,decimal,double,short,int,long,object,sbyte,float,string,ushort,uint,ulong.

Thefollowingtablelistseachofthesetypesinallthreeformats:

TypeAlias Type FullyQualifiedType

bool Boolean System.Boolean

byte Byte System.Byte

char Char System.Char

decimal Decimal System.Decimal

double Double System.Double

short Int16 System.Int16

int Int32 System.Int32

long Int64 System.Int64

object Object System.Object

sbyte SByte System.SByte

float Single System.Single

string String System.String

ushort UInt16 System.UInt16

uint UInt32 System.UInt32

Page 567: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

ulong UInt64 System.UInt64

Page 568: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,replacethetypewiththebuilt-inaliasforthetype.

Page 569: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName UseStringEmptyForEmptyStrings

CheckId SA1122

Category ReadabilityRules

Page 570: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codeincludesanemptystring,writtenas“”.

Page 571: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthecodecontainsanemptystring.Forexample:

strings="";

Thiswillcausethecompilertoembedanemptystringintothecompiledcode.Ratherthanincludingahard-codedemptystring,usethestaticstring.Emptyproperty:

strings=string.Empty;

Page 572: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,replacethehard-codedemptystringwithstring.Empty.

Page 573: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DoNotPlaceRegionsWithinElements

CheckId SA1123

Category ReadabilityRules

Page 574: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codecontainsaregionwithinthebodyofacodeelement.

Page 575: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneveraregionisplacedwithinthebodyofacodeelement.Inmanyeditors,includingVisualStudio,theregionwillappearcollapsedbydefault,hidingthecodewithintheregion.Itisgenerallyabadpracticetohidecodewithinthebodyofanelement,asthiscanleadtobaddecisionsasthecodeismaintainedovertime.

Page 576: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheregionfromthecode.

Page 577: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DoNotUseRegions

CheckId SA1124

Category ReadabilityRules

Page 578: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codecontainsaregion.

Page 579: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneveraregionisplacedanywherewithinthecode.Inmanyeditors,includingVisualStudio,theregionwillappearcollapsedbydefault,hidingthecodewithintheregion.Itisgenerallyabadpracticetohidecode,asthiscanleadtobaddecisionsasthecodeismaintainedovertime.

Page 580: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheregionfromthecode.

Page 581: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName UseShorthandForNullableTypes

CheckId SA1125

Category ReadabilityRules

Page 582: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheNullabletypehasbeendefinednotusingtheC#shorthand.Forexample,Nullable<DateTime>hasbeenusedinsteadofthepreferredDateTime?

Page 583: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenevertheNullabletypehasbeendefinedwithoutusingtheshorthandC#style.

Page 584: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,usetheshorthandversionofthenullabletypeie.int?,DateTime?,etc.

Page 585: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

Ruleswhichenforcespacingrequirementsaroundkeywordsandsymbolsinthecode.

SA1000:KeywordsMustBeSpacedCorrectly

SA1001:CommasMustBeSpacedCorrectly

SA1002:SemicolonsMustBeSpacedCorrectly

SA1003:SymbolsMustBeSpacedCorrectly

SA1004:DocumentationLinesMustBeginWithSingleSpace

SA1005:SingleLineCommentsMustBeginWithSingeSpace

SA1006:PreprocessorKeywordsMustNotBePrecededBySpace

SA1007:OperatorKeywordMustBeFollowedBySpace

SA1008:OpeningParenthesisMustBeSpacedCorrectly

SA1009:ClosingParenthesisMustBeSpacedCorrectly

SA1010:OpeningSquareBracketsMustBeSpacedCorrectly

SA1011:ClosingSquareBracketsMustBeSpacedCorrectly

SA1012:OpeningCurlyBracketsMustBeSpacedCorrectly

SA1013:ClosingCurlyBracketsMustBeSpacedCorrectly

SA1014:OpeningGenericBracketsMustBeSpacedCorrectly

SA1015:ClosingGenericBracketsMustBeSpacedCorrectly

SA1016:OpeningAttributeBracketsMustBeSpacedCorrectly

SA1017:ClosingAttributeBracketsMustBeSpacedCorrectly

SA1018:NullableTypeSymbolsMustNotBePrecededBySpace

SA1019:MemberAccessSymbolsMustBeSpacedCorrectly

SA1020:IncrementDecrementSymbolsMustBeSpacedCorrectly

SA1021:NegativeSignsMustBeSpacedCorrectly

SA1022:PositiveSignsMustBeSpacedCorrectly

SA1023:DereferenceAndAccessOfSymbolsMustBeSpacedCorrectly

Page 586: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

SA1024:ColonsMustBeSpacedCorrectly

SA1025:CodeMustNotContainMultipleWhitespaceInARow

SA1026:CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArrayAllocation

SA1027:TabsMustNotBeUsed

Page 587: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName KeywordsMustBeSpacedCorrectly

CheckId SA1000

Category SpacingRules

Page 588: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThespacingaroundaC#keywordisincorrect.

Page 589: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundakeywordisincorrect.

ThefollowingC#keywordsmustalwaysbefollowedbyasinglespace:catch,fixed,for,foreach,from,group,if,in,into,join,let,lock,orderby,return,select,stackalloc,switch,throw,using,where,while,yield.

Thefollowingkeywordsmustnotbefollowedbyanyspace:checked,default,sizeof,typeof,unchecked.

Thenewkeywordshouldalwaysbefollowedbyaspace,unlessitisusedtocreateanewarray,inwhichcasethereshouldbenospacebetweenthenewkeywordandtheopeningarraybracket.

Page 590: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addorremoveaspaceafterthekeyword,accordingtothedescriptionabove.

Page 591: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CommasMustBeSpacedCorrectly

CheckId SA1001

Category SpacingRules

Page 592: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThespacingaroundacommaisincorrect,withinaC#codefile.

Page 593: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundacommaisincorrect.

Acommashouldalwaysbefollowedbyasinglespace,unlessitisthelastcharacterontheline,andacommashouldneverbeprecededbyanywhitespace,unlessitisthefirstcharacterontheline.

Page 594: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthecommaisfollowedbyasinglespace,andisnotprecededbyanyspace.

Page 595: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SemicolonsMustBeSpacedCorrectly

CheckId SA1002

Category SpacingRules

Page 596: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThespacingaroundasemicolonisincorrect,withinaC#codefile.

Page 597: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundasemicolonisincorrect.

Asemicolonshouldalwaysbefollowedbyasinglespace,unlessitisthelastcharacterontheline,andasemicolonshouldneverbeprecededbyanywhitespace,unlessitisthefirstcharacterontheline.

Page 598: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthesemicolonisfollowedbyasinglespace,andisnotprecededbyanyspace.

Page 599: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SymbolsMustBeSpacedCorrectly

CheckId SA1003

Category SpacingRules

Page 600: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThespacingaroundanoperatorsymbolisincorrect,withinaC#codefile.

Page 601: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundanoperatorsymbolisincorrect.

Thefollowingtypesofoperatorsymbolsmustbesurroundedbyasinglespaceoneitherside:colons,arithmeticoperators,assignmentoperators,conditionaloperators,logicaloperators,relationaloperators,shiftoperators,andlambdaoperators.Forexample:

intx=4+y;

Incontrast,unaryoperatorsmustbeprecededbyasinglespace,butmustneverbefollowedbyanyspace.Forexample:

boolx=!value;

Anexceptionoccurswheneverthesymbolisprecededorfollowedbyaparenthesisorbracket,inwhichcasethereshouldbenospacebetweenthesymbolandthebracket.Forexample:

if(!value)

{

}

Page 602: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundthesymbolfollowstheruledescribedabove.

Page 603: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DocumentationLinesMustBeginWithSingleSpace

CheckId SA1004

Category SpacingRules

Page 604: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAlinewithinadocumentationheaderaboveaC#elementdoesnotbeginwithasinglespace.

Page 605: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenalinewithinadocumentationheaderdoesnotbeginwithasinglespace.Forexample:

///<summary>

///Thesummarytext.

///</summary>

///<paramname="x">Thedocumentroot.</param>

///<paramname="y">TheXmlheadertoken.</param>

privatevoidMethod1(intx,inty)

{

}

Theheaderlinesshouldbeginwithasinglespaceafterthethreeleadingforwardslashes:

///<summary>

///Thesummarytext.

///</summary>

///<paramname="x">Thedocumentroot.</param>

///<paramname="y">TheXmlheadertoken.</param>

privatevoidMethod1(intx,inty)

{

Page 606: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

}

Page 607: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethattheheaderlinebeginswithasinglespace.

Page 608: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName SingleLineCommentsMustBeginWithSingleSpace

CheckId SA1005

Category SpacingRules

Page 609: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAsingle-linecommentwithinaC#codefiledoesnotbeginwithasinglespace.

Page 610: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenasingle-linecommentdoesnotbeginwithasinglespace.Forexample:

privatevoidMethod1()

{

//Asingle-linecomment.

//Asingle-linecomment.

}

Thecommentsshouldbeginwithasinglespaceaftertheleadingforwardslashes:

privatevoidMethod1()

{

//Asingle-linecomment.

//Asingle-linecomment.

}

Anexceptiontothisruleoccurswhenthecommentisbeingusedtocommentoutalineofcode.Inthiscase,thespacecanbeomittedifthecommentbeginswithfourforwardslashestoindicateout-commentedcode.Forexample:

privatevoidMethod1()

{

Page 611: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

////intx=2;

////returnx;

}

Page 612: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthecommentbeginswithasinglespace.Ifthecommentisbeingusedtocommentoutalineofcode,ensurethatthecommentbeginswithfourforwardslashes,inwhichcasetheleadingspacecanbeomitted.

Page 613: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PreprocessorKeywordsMustNotBePrecededBySpace

CheckId SA1006

Category SpacingRules

Page 614: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAC#preprocessor-typekeywordisprecededbyspace.

Page 615: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthepreprocessor-typekeywordinapreprocessordirectiveisprecededbyspace.Forexample:

#ifDebug

Thereshouldnotbeanywhitespacebetweentheopeninghashmarkandthepreprocessor-typekeyword:

#ifDebug

Page 616: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespacebetweentheopeninghashmarkandthepreprocessor-typekeyword.

Page 617: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OperatorKeywordMustBeFollowedBySpace

CheckId SA1007

Category SpacingRules

Page 618: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheoperatorkeywordwithinaC#operatoroverloadmethodisnotfollowedbyanywhitespace.

Page 619: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheoperatorkeywordwithinanoperatoroverloadmethodisnotfollowedbyanywhitespace.Theoperatorkeywordshouldalwaysbefollowedbyasinglespace.Forexample:

publicMyClassoperator+(MyClassa,MyClassb)

{

}

Page 620: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,addasinglespaceaftertheoperatorkeyword.

Page 621: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningParenthesisMustBeSpacedCorrectly

CheckId SA1008

Category SpacingRules

Page 622: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnopeningparenthesiswithinaC#statementisnotspacedcorrectly.

Page 623: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheopeningparenthesiswithinastatementisnotspacedcorrectly.Anopeningparenthesisshouldnotbeprecededbyanywhitespace,unlessitisthefirstcharacterontheline,oritisprecededbycertainC#keywordssuchasif,while,orfor.Inaddition,anopeningparenthesisisallowedtobeprecededbywhitespacewhenitfollowsanoperatorsymbolwithinanexpression.

Anopeningparenthesisshouldnotbefollowedbywhitespace,unlessitisthelastcharacterontheline.

Page 624: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundtheopeningparenthesisfollowstheruledescribedabove.

Page 625: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingParenthesisMustBeSpacedCorrectly

CheckId SA1009

Category SpacingRules

Page 626: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclosingparenthesiswithinaC#statementisnotspacedcorrectly.

Page 627: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhentheclosingparenthesiswithinastatementisnotspacedcorrectly.

Aclosingparenthesisshouldneverbeprecededbywhitespace.Inmostcases,aclosingparenthesisshouldbefollowedbyasinglespace,unlesstheclosingparenthesiscomesattheendofacast,ortheclosingparenthesisisfollowedbycertaintypesofoperatorsymbols,suchaspositivesigns,negativesigns,andcolons.

Iftheclosingparenthesisisfollowedbywhitespace,thenextnon-whitespacecharactermustnotbeanopeningorclosingparenthesisorsquarebracket,orasemicolonorcomma.

Page 628: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundtheclosingparenthesisfollowstheruledescribedabove.

Page 629: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningSquareBracketsMustBeSpacedCorrectly

CheckId SA1010

Category SpacingRules

Page 630: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnopeningsquarebracketwithinaC#statementisnotspacedcorrectly.

Page 631: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenanopeningsquarebracketwithinastatementisprecededorfollowedbywhitespace.

Anopeningsquarebracketmustneverbeprecededbywhitespace,unlessitisthefirstcharacterontheline,andanopeningsquaremustneverbefollowedbywhitespace,unlessitisthelastcharacterontheline.

Page 632: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespaceoneithersideoftheopeningsquarebracket.

Page 633: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingSquareBracketsMustBeSpacedCorrectly

CheckId SA1011

Category SpacingRules

Page 634: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclosingsquarebracketwithinaC#statementisnotspacedcorrectly.

Page 635: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundaclosingsquarebracketisnotcorrect.

Aclosingsquarebracketmustneverbeprecededbywhitespace,unlessitisthefirstcharacterontheline.

Aclosingsquarebracketmustbefollowedbywhitespace,unlessitisthelastcharacterontheline,itisfollowedbyaclosingbracketoranopeningparenthesis,itisfollowedbyacommaorsemicolon,oritisfollowedbycertaintypesofoperatorsymbols.

Page 636: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundtheclosingsquarebracketfollowstheruledescribedabove.

Page 637: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningCurlyBracketsMustBeSpacedCorrectly

CheckId SA1012

Category SpacingRules

Page 638: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnopeningcurlybracketwithinaC#elementisnotspacedcorrectly.

Page 639: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundanopeningcurlybracketisnotcorrect.

Anopeningcurlybracketshouldalwaysbeprecededbyasinglespace,unlessitisthefirstcharacterontheline,orunlessitisprecededbyanopeningparenthesis,inwhichcasethereshouldbenospacebetweentheparenthesisandthecurlybracket.

Anopeningcurlybracketmustalwaysbefollowedbyasinglespace,unlessitisthelastcharacterontheline.

Page 640: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundtheopeningcurlybracketfollowstheruledescribedabove.

Page 641: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingCurlyBracketsMustBeSpacedCorrectly

CheckId SA1013

Category SpacingRules

Page 642: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclosingcurlybracketwithinaC#elementisnotspacedcorrectly.

Page 643: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundaclosingcurlybracketisnotcorrect.

Aclosingcurlybracketshouldalwaysbefollowedbyasinglespace,unlessitisthelastcharacterontheline,orunlessitisfollowedbyaclosingparenthesis,acomma,orasemicolon.

Aclosingcurlybracketmustalwaysbeprecededbyasinglespace,unlessitisthefirstcharacterontheline.

Page 644: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundtheclosingcurlybracketfollowstheruledescribedabove.

Page 645: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningGenericBracketsMustBeSpacedCorrectly

CheckId SA1014

Category SpacingRules

Page 646: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnopeninggenericbracketwithinaC#elementisnotspacedcorrectly.

Page 647: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundanopeninggenericbracketisnotcorrect.

Anopeninggenericbracketshouldneverbeprecededorfollowedbywhitespace,unlessthebracketisthefirstorlastcharacterontheline.

Page 648: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespaceoneithersideoftheopeninggenericbracket.

Page 649: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingGenericBracketsMustBeSpacedCorrectly

CheckId SA1015

Category SpacingRules

Page 650: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclosinggenericbracketwithinaC#elementisnotspacedcorrectly.

Page 651: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundaclosinggenericbracketisnotcorrect.

Aclosinggenericbracketshouldneverbeprecededbywhitespace,unlessthebracketisthefirstcharacterontheline.

Page 652: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespacebeforetheclosinggenericbracket.

Page 653: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName OpeningAttributeBracketsMustBeSpacedCorrectly

CheckId SA1016

Category SpacingRules

Page 654: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnopeningattributebracketwithinaC#elementisnotspacedcorrectly.

Page 655: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundanopeningattributebracketisnotcorrect.

Anopeningattributebracketshouldneverbefollowedbywhitespace,unlessthebracketisthelastcharacterontheline.

Page 656: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespaceaftertheopeningattributebracket.

Page 657: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ClosingAttributeBracketsMustBeSpacedCorrectly

CheckId SA1017

Category SpacingRules

Page 658: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAclosingattributebracketwithinaC#elementisnotspacedcorrectly.

Page 659: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundaclosingattributebracketisnotcorrect.

Aclosingattributebracketshouldneverbeprecededbywhitespace,unlessthebracketisthefirstcharacterontheline.

Page 660: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespacebeforetheclosingattributebracket.

Page 661: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName NullableTypeSymbolsMustNotBePrecededBySpace

CheckId SA1018

Category SpacingRules

Page 662: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnullabletypesymbolwithinaC#elementisnotspacedcorrectly.

Page 663: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundanullabletypesymbolisnotcorrect.

Anullabletypesymbolshouldneverbeprecededbywhitespace,unlessthesymbolisthefirstcharacterontheline.

Page 664: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespacebeforethenullabletypesymbol.

Page 665: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName MemberAccessSymbolsMustBeSpacedCorrectly

CheckId SA1019

Category SpacingRules

Page 666: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThespacingaroundamemberaccesssymbolisincorrect,withinaC#codefile.

Page 667: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundamemberaccesssymbolisincorrect.Amemberaccesssymbolshouldnothavewhitespaceoneitherside,unlessitisthefirstcharacterontheline.

Page 668: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthememberaccesssymbolisnotsurroundedbyanywhitespace.

Page 669: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName IncrementDecrementSymbolsMustBeSpacedCorrectly

CheckId SA1020

Category SpacingRules

Page 670: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnincrementordecrementsymbolwithinaC#elementisnotspacedcorrectly.

Page 671: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundanincrementordecrementsymbolisnotcorrect.

Thereshouldbenowhitespacebetweentheincrementordecrementsymbolandtheitemthatisbeingincrementedordecremented.

Page 672: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthereisnowhitespacebetweentheincrementordecrementsymbolandtheitemthatisbeingincrementedordecremented.

Page 673: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName NegativeSignsMustBeSpacedCorrectly

CheckId SA1021

Category SpacingRules

Page 674: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnegativesignwithinaC#elementisnotspacedcorrectly.

Page 675: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundanegativesignisnotcorrect.

Anegativesignshouldalwaysbeprecededbyasinglespace,unlessitcomesafteranopeningsquarebracket,aparenthesis,oristhefirstcharacterontheline.

Anegativesignshouldneverbefollowedbywhitespace,andshouldneverbethelastcharacteronaline.

Page 676: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundthenegativesignfollowstheruledescribedabove.

Page 677: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName PositiveSignsMustBeSpacedCorrectly

CheckId SA1022

Category SpacingRules

Page 678: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseApositivesignwithinaC#elementisnotspacedcorrectly.

Page 679: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundapositivesignisnotcorrect.

Apositivesignshouldalwaysbeprecededbyasinglespace,unlessitcomesafteranopeningsquarebracket,aparenthesis,oristhefirstcharacterontheline.

Apositivesignshouldneverbefollowedbywhitespace,andshouldneverbethelastcharacteronaline.>

Page 680: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundthepositivesignfollowstheruledescribedabove.

Page 681: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName DereferenceAndAccessOfMustBeSpacedCorrectly

CheckId SA1023

Category SpacingRules

Page 682: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAdereferencesymboloranaccess-ofsymbolwithinaC#elementisnotspacedcorrectly.

Page 683: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundadereferenceoraccess-ofsymbolisnotcorrect.

Thespacingaroundthesymboldependsuponwhetherthesymbolisusedwithinatypedeclaration.Ifso,thesymbolmustalwaysbefollowedbyasinglespace,unlessitisthelastcharacterontheline,orisfollowedbyanopeningsquarebracketoraparenthesis.Inaddition,thesymbolshouldnotbeprecededbywhitespace,andshouldnotbethefirstcharacterontheline.Anexampleofaproperlyspaceddereferencesymbolusedwithinatypedeclarationis:

object*x=null;

Whenadereferenceoraccess-ofsymbolisusedoutsideofatypedeclaration,theoppositeruleapplies.Inthiscase,thesymbolmustalwaysbeprecededbyasinglespace,unlessitisthefirstcharacterontheline,orisprecededbyanopeningsquarebracketoraparenthesis.Thesymbolshouldnotbefollowedbywhitespace,andshouldnotbethelastcharacterontheline.Forexample:

y=*x;

Page 684: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundthedereferenceoraddress-ofsymbolfollowstheruledescribedabove.

Page 685: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName ColonsMustBeSpacedCorrectly

CheckId SA1024

Category SpacingRules

Page 686: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAcolonwithinaC#elementisnotspacedcorrectly.

Page 687: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswhenthespacingaroundacolonisnotcorrect.

Thespacingaroundacolondependsuponthetypeofcolonandhowitisusedwithinthecode.Acolonappearingwithinanelementdeclarationmustalwayshaveasinglespaceoneitherside,unlessitisthefirstorlastcharacterontheline.Forexampleallofthecolonsbelowfollowthisrule:

publicclassClass2<T>:Class1whereT:MyType

{

publicClass2(intx):base(x)

{

}

}

Whenthecoloncomesattheendofalabelorcasestatement,itmustalwaysbefollowedbywhitespaceorbethelastcharacterontheline,butshouldneverbeprecededbywhitespace.Forexample:

_label:

switch(x)

{

case2:

returnx;

}

Finally,whenacolonisusedwithinaconditionalstatement,itmustalwayscontainasinglespaceoneitherside,unlessthecolonisthefirstorlastcharacterontheline.Forexample:

Page 688: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

intx=y?2:3;

Page 689: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,ensurethatthespacingaroundthecolonfollowstheruledescribedabove.

Page 690: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeMustNotContainMultipleWhitespaceInARow

CheckId SA1025

Category SpacingRules

Page 691: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseThecodecontainsmultiplewhitespacecharactersinarow.

Page 692: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneverthecodecontainsmultiplewhitespacecharactersinarow,unlessthecharacterscomeatthebeginningorendofalineofcode,orcomeafteracommaorsemicolon.

Page 693: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removetheextrawhitespacecharactersareleaveonlyasinglespace.

Page 694: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName CodeMustNotContainSpaceAfterNewKeywordInImplicitlyTypedArrayAllocation

CheckId SA1026

Category SpacingRules

Page 695: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseAnimplicitlytypednewarrayallocationwithinaC#codefileisnotspacedcorrectly.

Page 696: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneverthecodecontainsanimplicitlytypednewarrayallocationwhichisnotspacedcorrectly.Withinanimplicitlytypednewarrayallocation,thereshouldnotbeanyspacebetweenthenewkeywordandtheopeningarraybracket.Forexample:

vara=new[]{1,10,100,1000};

Page 697: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removeanywhitespacebetweenthenewkeywordandtheopeningarraybracket.

Page 698: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

TypeName TabsMustNotBeUsed

CheckId SA1027

Category SpacingRules

Page 699: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

CauseTheC#codecontainsatabcharacter.

Page 700: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

RuleDescriptionAviolationofthisruleoccurswheneverthecodecontainsatabcharacter.

TabsshouldnotbeusedwithinC#code,becausethelengthofthetabcharactercanvarydependingupontheeditorbeingusedtoviewthecode.Thiscancausethespacingandindexingofthecodetovaryfromthedeveloper’soriginalintention,andcaninsomecasesmakethecodedifficulttoread.

Forthesereasons,tabsshouldnotbeused,andeachlevelofindentationshouldconsistoffourspaces.Thiswillensurethatthecodelooksthesamenomatterwhicheditorisbeingusedtoviewthecode.

Page 701: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

HowtoFixViolationsTofixaviolationofthisrule,removethetabcharacterfromthecode.

Page 702: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

StartingwithStyleCop4.3.2,itispossibletosuppressthereportingofruleviolationsbyaddingsuppressionattributeswithinthesourcecode.ThesyntaxforthesesuppressionsissimilartothatforVisualStudioCodeAnalysis,orFxCop.FormoreinformationaboutCodeAnalysissuppressions,seethefollowingarticle:InSourceSuppressionsOverview.

StyleCoprulesuppressionsareregisteredincodeusingtheSuppressMessageattribute.TheSuppressMessageattributeisaconditionalattribute,whichisincludedintheILmetadataofyourmanagedcodeassemblyonlyiftheCODE_ANALYSIScompilationsymbolisdefinedatcompiletime.

Werecommendusingin-sourcesuppressionsondebugbuilds,inordertoeliminatethepossibilityofmistakenlyshippingthein-sourcesuppressionmetadataandcompromisingexecutionorperformancebecauseofthemetadatabloat.

TheSuppressMessageattributehasthefollowingformat:

[SuppressMessage("RuleCategory,"RuleId","Justification")]

Where:

RuleCategory-TheStyleCoprulenamespaceinwhichtheruleisdefined.Forexample,StyleCop.CSharp.DocumentationRulesRuleId-Theidentifierfortherule,usingtheformatshortname:longname.Forexample,SA1600:ElementsMustBeDocumentedJustification-Thetextthatisusedtodocumentthereasonforsuppressingthemessage.

TheSuppressMessageattributealsotakesthefollowingoptionalparameters.TheseparametersarecompletelyignoredbyStyleCopanddonotneedtobefilledinforStyleCopsuppressions.

MessageIdScopeTarget

SuppressMessageUsage

StyleCopviolationsaresuppressedattheleveltowhichaninstanceofthe

Page 703: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

SuppressMessageattributeisapplied.Thepurposeofthisistotightlycouplethesuppressioninformationtothecodewheretheviolationoccurs.

Forexample,aStyleCopSuppressMessageattributeplacedonaclasswillsuppresstheruleforallcontentsoftheclass.Thesameattributeplacedonamethodwillonlysuppresstherulewithinthemethod.

GlobalSuppressions

StyleCopdoesnotsupportthenotionofglobalsuppressionsorfile-levelsuppressions.Suppressionsmustbeplacedonacodeelement.

Examples:

ThefollowingcodesuppressestheElementsMustBeDocumentedruleforthegivenclassandallitscontents:

[SuppressMessage("StyleCop.CSharp.DocumentationRules","SA1600:ElementsMustBeDocumented")]

publicclassMyUndocumentedClass

{

publicvoidMyUndocumentedMethod

{

}

}BeginningwithStyleCop4.4.0,itisalsopossibletosuppressalloftheruleswithinarulenamespace,usingasinglesuppressionattribute.ThisisindicatedbyreplacingtheruleCheckIDandrulenamewithasingleasterisk.ThefollowingcodeexamplesuppressesallofStyleCop'sdefaultdocumentationruleswithintheinnerclass.Inthiscase,StyleCopwouldstillflagaviolationindicatingthattheouterclassismissingdocumentation,butitwouldignorealldocumentationrulesfortheinnerclassanditscontents.

publicclassOuterClass

Page 704: The StyleCop tool provides warnings ... - Documentation & Help · Documentation Rules (SA1600-) Rules which verify the content and formatting of code documentation. Layout Rules (SA1500-)

{

[SuppressMessage("StyleCop.CSharp.DocumentationRules","*")]

publicclassInnerClass

{

publicvoidMyUndocumentedMethod

{

}

}}