{"id":21602415,"url":"https://github.com/a-patel/csharp-style-guide","last_synced_at":"2025-03-18T13:24:15.623Z","repository":{"id":109325420,"uuid":"148442756","full_name":"a-patel/csharp-style-guide","owner":"a-patel","description":"C# Style Guide for Tutorials","archived":false,"fork":false,"pushed_at":"2018-09-12T16:38:05.000Z","size":26,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-24T18:35:14.729Z","etag":null,"topics":["coding-standards","csharp","microsoft","style-guide","styleguide"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/a-patel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-12T07:52:14.000Z","updated_at":"2020-01-23T12:55:54.000Z","dependencies_parsed_at":"2023-07-26T22:01:16.497Z","dependency_job_id":null,"html_url":"https://github.com/a-patel/csharp-style-guide","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-patel%2Fcsharp-style-guide","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-patel%2Fcsharp-style-guide/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-patel%2Fcsharp-style-guide/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/a-patel%2Fcsharp-style-guide/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/a-patel","download_url":"https://codeload.github.com/a-patel/csharp-style-guide/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244228829,"owners_count":20419507,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["coding-standards","csharp","microsoft","style-guide","styleguide"],"created_at":"2024-11-24T19:13:21.351Z","updated_at":"2025-03-18T13:24:15.599Z","avatar_url":"https://github.com/a-patel.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# C# Style Guide\nC# Style Guide for Tutorials\n\n---\nThe code is read much more often than it is written.\n\n\n### The Zen of any programming language\n\n* Beautiful is better than ugly.\n* Explicit is better than implicit.\n* Simple is better than complex.\n* Complex is better than complicated.\n* Flat is better than nested.\n* Sparse is better than dense.\n* Readability counts.\n* Special cases aren't special enough to break the rules.\n* Although practicality beats purity.\n* Errors should never pass silently.\n* Unless explicitly silenced.\n* In the face of ambiguity, refuse the temptation to guess.\n* There should be one-- and preferably only one --obvious way to do it.\n* Although that way may not be obvious at first unless you're Dutch.\n* Now is better than never.\n* Although never is often better than *right* now.\n* If the implementation is hard to explain, it's a bad idea.\n* If the implementation is easy to explain, it may be a good idea.\n* Namespaces are one honking great idea -- let's do more of those!\n\n\nYou also see \"Readability Counts\" in the above listed points, which should be your main concern while writing code: other programmers should understand and should be able to contribute to your code so that it can solve the task at hand.\n\n\n## Style Guiding Principles\n\n* Be consistent.\n* Don't rewrite existing code to follow this guide.\n* Don't violate a guideline without a good reason.\n* A reason is good when you can convince a teammate, not just when you like it.\n* Assume your reader knows C# and English.\n* Prefer clarity to 'performance'.\n* Prefer clarity to .NET dogma.\n* Write comments that people want to read, with correct spelling and grammar.\n\n\n## The Rundown\n\n* Indent with tabs.\n* Max line length is 100 columns.\n* Use spaces and empty lines precisely.\n* Braces generally go on their own lines.\n* Never put a space before `[`.\n* Always put a space before `{`.\n* Always put a space before `(` except for method invocations or when following another `(`.\n\n\n\n## Table of Contents\n\n- [Nomenclature](#nomenclature)\n  + [Namespaces](#namespaces)\n  + [Classes \u0026 Interfaces](#classes--interfaces)\n  + [Methods](#methods)\n  + [Fields](#fields)\n  + [Parameters](#parameters--parameters)\n  + [Delegates](#delegates--delegates)\n  + [Events](#events--events)\n  + [Misc](#misc)\n- [Declarations](#declarations)\n  + [Access Level Modifiers](#access-level-modifiers)\n  + [Fields \u0026 Variables](#fields--variables)\n  + [Classes](#classes)\n  + [Interfaces](#interfaces)\n- [Spacing](#spacing)\n  + [Indentation](#indentation)\n  + [Line Length](#line-length)\n  + [Vertical Spacing](#vertical-spacing)\n- [Brace Style](#brace-style)\n- [Switch Statements](#switch-statements)\n- [Language](#language)\n- [Copyright Statement](#copyright-statement)\n- [Smiley Face](#smiley-face)\n- [Credit](#credits)\n\n\n## Nomenclature\n\nOn the whole, naming should follow C# standards.\n\n### Namespaces\n\nNamespaces are all **PascalCase**, multiple words concatenated together, without hypens ( - ) or underscores ( \\_ ):\n\n**BAD**:\n\n```csharp\ncom.raywenderlich.fpsgame.hud.healthbar\n```\n\n**GOOD**:\n\n```csharp\nRayWenderlich.FpsGame.Hud.Healthbar\n```\n\n### Classes \u0026 Interfaces\n\nWritten in **PascalCase**. For example `RadialSlider`. \n\n### Methods\n\nMethods are written in **PascalCase**. For example `DoSomething()`. \n\n### Fields\n\nAll non-static fields are written **camelCase**. Per Unity convention, this includes **public fields** as well.\n\nFor example:\n\n```csharp\npublic class MyClass \n{\n    public int publicField;\n    int packagePrivate;\n    private int myPrivate;\n    protected int myProtected;\n}\n```\n\n**BAD:**\n\n```csharp\nprivate int _myPrivateVariable\n```\n\n**GOOD:**\n\n```csharp\nprivate int myPrivateVariable\n```\n\nStatic fields are the exception and should be written in **PascalCase**:\n\n```csharp\npublic static int TheAnswer = 42;\n```\n\n### Parameters\n\nParameters are written in **camelCase**.\n\n**BAD:**\n\n```csharp\nvoid DoSomething(Vector3 Location)\n```\n**GOOD:**\n\n```csharp\nvoid DoSomething(Vector3 location)\n```\n\nSingle character values are to be avoided except for temporary looping variables.\n\n### Delegates\n\nDelegates are written in **PascalCase**.\n\nWhen declaring delegates, DO add the suffix **EventHandler** to names of delegates that are used in events. \n\n**BAD:**\n\n```csharp\npublic delegate void Click()\n```\n**GOOD:**\n\n```csharp\npublic delegate void ClickEventHandler()\n```  \n\nDO add the suffix **Callback** to names of delegates other than those used as event handlers.\n\n**BAD:**\n\n```csharp\npublic delegate void Render()\n```\n**GOOD:**\n\n```csharp\npublic delegate void RenderCallback()\n```  \n\n### Events\n\nPrefix event methods with the prefix **On**.\n\n**BAD:**\n\n```csharp\npublic static event CloseCallback Close;\n```  \n\n**GOOD:**\n\n```csharp\npublic static event CloseCallback OnClose;\n```\n\n### Misc\n\nIn code, acronyms should be treated as words. For example:\n\n**BAD:**\n\n```csharp\nXMLHTTPRequest\nString URL\nfindPostByID\n```  \n\n**GOOD:**\n\n```csharp\nXmlHttpRequest\nString url\nfindPostById\n```\n\n## Declarations\n\n### Access Level Modifiers\n\nAccess level modifiers should be explicitly defined for classes, methods and member variables.\n\n### Fields \u0026 Variables\n\nPrefer single declaration per line.\n\n**BAD:**\n\n```csharp\nstring username, twitterHandle;\n```\n\n**GOOD:**\n\n```csharp\nstring username;\nstring twitterHandle;\n```\n\n### Classes\n\nExactly one class per source file, although inner classes are encouraged where scoping appropriate.\n\n### Interfaces\n\nAll interfaces should be prefaced with the letter **I**. \n\n**BAD:**\n\n```csharp\nRadialSlider\n```\n\n**GOOD:**\n\n```csharp\nIRadialSlider\n```\n\n## Spacing\n\nSpacing is especially important in raywenderlich.com code, as code needs to be easily readable as part of the tutorial. \n\n### Indentation\n\nIndentation should be done using **spaces** — never tabs.  \n\n#### Blocks\n\nIndentation for blocks uses **4 spaces** for optimal readability:\n\n**BAD:**\n\n```csharp\nfor (int i = 0; i \u003c 10; i++) \n{\n  Debug.Log(\"index=\" + i);\n}\n```\n\n**GOOD:**\n\n```csharp\nfor (int i = 0; i \u003c 10; i++) \n{\n    Debug.Log(\"index=\" + i);\n}\n```\n\n#### Line Wraps\n\nIndentation for line wraps should use **4 spaces** (not the default 8):\n\n**BAD:**\n\n```csharp\nCoolUiWidget widget =\n        someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line);\n```\n\n**GOOD:**\n\n```csharp\nCoolUiWidget widget =\n    someIncrediblyLongExpression(that, reallyWouldNotFit, on, aSingle, line);\n```\n\n### Line Length\n\nLines should be no longer than **100** characters long.\n\n### Vertical Spacing\n\nThere should be exactly one blank line between methods to aid in visual clarity \nand organization. Whitespace within methods should separate functionality, but \nhaving too many sections in a method often means you should refactor into\nseveral methods.\n\n\n## Brace Style\n\nAll braces get their own line as it is a C# convention:\n\n**BAD:**\n\n```csharp\nclass MyClass {\n    void DoSomething() {\n        if (someTest) {\n          // ...\n        } else {\n          // ...\n        }\n    }\n}\n```\n\n**GOOD:**\n\n```csharp\nclass MyClass\n{\n    void DoSomething()\n    {\n        if (someTest)\n        {\n          // ...\n        }\n        else\n        {\n          // ...\n        }\n    }\n}\n```\n\nConditional statements are always required to be enclosed with braces,\nirrespective of the number of lines required.\n\n**BAD:**\n\n```csharp\nif (someTest)\n    doSomething();  \n\nif (someTest) doSomethingElse();\n```\n\n**GOOD:**\n\n```csharp\nif (someTest) \n{\n    DoSomething();\n}  \n\nif (someTest)\n{\n    DoSomethingElse();\n}\n```\n## Switch Statements\n\nSwitch-statements come with `default` case by default (heh). If the `default` case is never reached, be sure to remove it.\n\n**DEFAULT GETS USED:**  \n  \n```csharp\nswitch (variable) \n{\n    case 1:\n        break;\n    case 2:\n        break;\n    default:\n        break;\n}\n```\n\n**DEFAULT DOESN'T GET USED:**  \n  \n```csharp\nswitch (variable) \n{\n    case 1:\n        break;\n    case 2:\n        break;\n}\n```\n\n## Language\n\nUse US English spelling.\n\n**BAD:**\n\n```csharp\nstring colour = \"red\";\n```\n\n**GOOD:**\n\n```csharp\nstring color = \"red\";\n```\n\n\n\n\nIndentation\nMaximum Line Length\nLine Wraps\nBlank Lines\nWhitespaces in Expressions and Statements\nImports/usings/regions\nComments\nNaming Conventions\n\n\n\nlinks\nhttps://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/index\nhttps://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions\nhttps://www.dofactory.com/reference/csharp-coding-standards\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa-patel%2Fcsharp-style-guide","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fa-patel%2Fcsharp-style-guide","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fa-patel%2Fcsharp-style-guide/lists"}