Movatterモバイル変換


[0]ホーム

URL:


styleguide

C# at Google Style Guide

This style guide is for C# code developed internally at Google, and is thedefault style for C# code at Google. It makes stylistic choices that conform toother languages at Google, such as Google C++ style and Google Java style.

Formatting guidelines

Naming rules

Naming rules followMicrosoft’s C# naming guidelines.Where Microsoft’s naming guidelines are unspecified (e.g. private and localvariables), rules are taken from theCoreFX C# coding guidelines

Rule summary:

Code

Files

Organization

Whitespace rules

Developed from Google Java style.

Example

usingSystem;// `using` goes at the top, outside the// namespace.namespaceMyNamespace{// Namespaces are PascalCase.// Indent after namespace.publicinterfaceIMyInterface{// Interfaces start with 'I'publicintCalculate(floatvalue,floatexp);// Methods are PascalCase// ...and space after comma.}publicenumMyEnum{// Enumerations are PascalCase.Yes,// Enumerators are PascalCase.No,}publicclassMyClass{// Classes are PascalCase.publicintFoo=0;// Public member variables are// PascalCase.publicboolNoCounting=false;// Field initializers are encouraged.privateclassResults{publicintNumNegativeResults=0;publicintNumPositiveResults=0;}privateResults_results;// Private member variables are// _camelCase.publicstaticintNumTimesCalled=0;privateconstint_bar=100;// const does not affect naming// convention.privateint[]_someTable={// Container initializers use a 22,3,4,// space indent.}publicMyClass(){_results=newResults{NumNegativeResults=1,// Object initializers use a 2 spaceNumPositiveResults=1,// indent.};}publicintCalculateValue(intmulNumber){// No line break before opening brace.varresultValue=Foo*mulNumber;// Local variables are camelCase.NumTimesCalled++;Foo+=_bar;if(!NoCounting){// No space after unary operator and// space after 'if'.if(resultValue<0){// Braces used even when optional and// spaces around comparison operator._results.NumNegativeResults++;}elseif(resultValue>0){// No newline between brace and else._results.NumPositiveResults++;}}returnresultValue;}publicvoidExpressionBodies(){// For simple lambdas, fit on one line if possible, no brackets or braces required.Func<int,int>increment=x=>x+1;// Closing brace aligns with first character on line that includes the opening brace.Func<int,int,long>difference1=(x,y)=>{longdiff=(long)x-y;returndiff>=0?diff:-diff;};// If defining after a continuation line break, indent the whole body.Func<int,int,long>difference2=(x,y)=>{longdiff=(long)x-y;returndiff>=0?diff:-diff;};// Inline lambda arguments also follow these rules. Prefer a leading newline before// groups of arguments if they include lambdas.CallWithDelegate((x,y)=>{longdiff=(long)x-y;returndiff>=0?diff:-diff;});}voidDoNothing(){}// Empty blocks may be concise.// If possible, wrap arguments by aligning newlines with the first argument.voidAVeryLongFunctionNameThatCausesLineWrappingProblems(intlongArgumentName,intp1,intp2){}// If aligning argument lines with the first argument doesn't fit, or is difficult to// read, wrap all arguments on new lines with a 4 space indent.voidAnotherLongFunctionNameThatCausesLineWrappingProblems(intlongArgumentName,intlongArgumentName2,intlongArgumentName3){}voidCallingLongFunctionName(){intveryLongArgumentName=1234;intshortArg=1;// If possible, wrap arguments by aligning newlines with the first argument.AnotherLongFunctionNameThatCausesLineWrappingProblems(shortArg,shortArg,veryLongArgumentName);// If aligning argument lines with the first argument doesn't fit, or is difficult to// read, wrap all arguments on new lines with a 4 space indent.AnotherLongFunctionNameThatCausesLineWrappingProblems(veryLongArgumentName,veryLongArgumentName,veryLongArgumentName);}}}

C# coding guidelines

Constants

IEnumerable vs IList vs IReadOnlyList

Generators vs containers

Property styles

Expression body syntax

For example:

intSomeProperty=>_someProperty

Structs and classes:

Lambdas vs named methods

Field initializers

Extension methods

ref and out

LINQ

Array vs List

Folders and file locations

Use of tuple as a return type

String interpolation vsString.Format() vsString.Concat vsoperator+

using

Object Initializer syntax

For example:

varx=newSomeClass{Property1=value1,Property2=value2,};

Namespace naming

Default values/null returns for structs

Removing from containers while iterating

C# (like many other languages) does not provide an obvious mechanism forremoving items from containers while iterating. There are a couple of options:

Calling delegates

Thevar keyword

Attributes

Argument Naming

Derived from the Google C++ style guide.

When the meaning of a function argument is nonobvious, consider one of thefollowing remedies:

Consider the following example:

// Bad - what are these arguments?DecimalNumberproduct=CalculateProduct(values,7,false,null);

versus:

// GoodProductOptionsoptions=newProductOptions();options.PrecisionDecimals=7;options.UseCache=CacheUsage.DontUseCache;DecimalNumberproduct=CalculateProduct(values,options,completionDelegate:null);
This site is open source.Improve this page.

[8]ページ先頭

©2009-2025 Movatter.jp