{"id":20444968,"url":"https://github.com/celeroncoder/csharp-cheatsheet","last_synced_at":"2026-03-07T12:36:22.929Z","repository":{"id":107874755,"uuid":"344441118","full_name":"celeroncoder/CSharp-CheatSheet","owner":"celeroncoder","description":null,"archived":false,"fork":false,"pushed_at":"2021-03-06T20:31:41.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-05T08:36:48.224Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/celeroncoder.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-03-04T10:50:31.000Z","updated_at":"2021-03-06T20:31:43.000Z","dependencies_parsed_at":null,"dependency_job_id":"a6f81fb4-33bc-438d-91c7-f6f496bef905","html_url":"https://github.com/celeroncoder/CSharp-CheatSheet","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/celeroncoder/CSharp-CheatSheet","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celeroncoder%2FCSharp-CheatSheet","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celeroncoder%2FCSharp-CheatSheet/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celeroncoder%2FCSharp-CheatSheet/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celeroncoder%2FCSharp-CheatSheet/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/celeroncoder","download_url":"https://codeload.github.com/celeroncoder/CSharp-CheatSheet/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/celeroncoder%2FCSharp-CheatSheet/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30213236,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T12:15:00.571Z","status":"ssl_error","status_checked_at":"2026-03-07T12:15:00.217Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-11-15T10:10:23.291Z","updated_at":"2026-03-07T12:36:22.878Z","avatar_url":"https://github.com/celeroncoder.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# C# CheatSheet\n\n## Create a new Console .NET Application\n\n- **change to the dir** where you want the application.\n\n- Run the command in the terminal :-\n\n  ```\n  $dotnet new console\n  ```\n\n- To **run** the console app,\n  ```\n  dotnet run\n  ```\n\n## How C# execute a program?\n\n- In the **.cs** file the compiler finds the the line\n\n  ```\n  static void Main(string[] args)\n  ```\n\n- In the _main block of code_ it will execute **line-by-line**.\n\n- In the end it will **automatically terminate the program**.\n\n## Variables\n\nVariables in C# are _the container to store data_ in the _program_.\n\n### Defining variables in C#\n\n- First, define the **type of data to be stored in the variable**.\n- **Name** the variable.\n\n- Now you can initialize the var in the same line,\n\n  ```\n  string name = \"john doe\";\n  ```\n\n- Or, you can first **declare the var** and then set the value at some other line.\n  ```\n  string name;\n  name = \"john doe\";\n  ```\n\n## Data types in C#\n\n### Text\n\n- **string** = _plain text_\n- **char** = _a single character_ or basically a _string with a single character_\n\n#### handy stuff with Strings\n\n- **\\n** = _new line character_\n\n  ```\n  Console.WriteLine(\"First\\nSecond\");\n  ```\n\n- **\\\\** = to store or print **preserved characters**\n\n  ```\n  Console.WriteLine(\"Giraffe \\\" Academy\");\n  ```\n\n- **String Concatenation** = to append any other string to a strings\n\n  ```\n  string phrase = \"First\" + \"Second\";\n  ```\n\n- **String Interpolation** = _using variables in the string without concatenation_\n  ```\n  Console.WriteLine($\"this is a var in the string { phrase } and { var }\");\n  ```\n\n#### String Methods\n\n- **Length** = to find out how many chars are there in a string\n\n  ```\n  string phrase = \"This is a phrase.\";\n  Console.WriteLine(phrase.Length);\n  ```\n\n- **Changing Case** = changing the _case of all the char in a string_ to lower or upper case.\n\n  ```\n  Console.WriteLine(stringVar.ToUpper());\n  Console.WriteLine(stringVar.ToLower());\n  ```\n\n- **Contain** = to find that the _string contains a subset of string_, returns a boolean value.\n\n  ```\n  Console.WriteLine(phrase.Contains(\"phrase\"))\n  ```\n\n- **Indexing Char of String** = to access _a single char in the string_ by putting in the index of the character\n\n  ```\n  Console.WriteLine(phrase[0]); // returns the first char of the string phrase.\n  ```\n\n- **IndexOf** = tells us _if the string contains a string and the index of it_ if **not present returns -1**\n\n  ```\n  Console.WriteLine(phrase.IndexOf(\"phrase\")); // returns the index of the first letter of the string.\n  ```\n\n- **Substring** = returns the _substring from the index and how many chars to grab given as params_\n  ```\n  Console.WriteLine(phrase.Substring(8, 9)) // starts from 8 and returns the 9 char after that.\n  ```\n\n### Numbers\n\n- **int** = _integer_\n\n#### Decimal Numbers\n\n- **float** = _less precise_\n  ```\n  float gpa = 3.1;\n  ```\n- **double** = _medium precise_\n  ```\n  double marks = 3.2;\n  ```\n- **decimal** = _most precise_\n  ```\n  decimal money = 34.35;\n  ```\n- If a var is declared as a float, double, decimal it **must contain decimal point**, to define a single number you can use like,\n  ```\n  double gpa = 3.0;\n  ```\n\n#### Finding the remainder\n\n- To find the remainder of the division operation we use **modulus operator (%)**\n  ```\n  Console.WriteLine(5 % 5); // returns 0 as reminder is zero!\n  ```\n\n#### Order of Mathematical Operations\n\n- By _default_ there is a **priority system** like multiplication goes first.\n\n- To **change** the _order of operation_ use **parentheses** as the _operation in the parentheses will execute first_.\n\n- An operation between _two integers will return an integer_.\n\n  ```\n  Console.WriteLine(5 / 2); // returns 2 not 2.5\n  ```\n\n- Operations _between and integer or a decimal or a decimal and a decimal will return a decimal_.\n  ```\n  Console.WriteLine(5 / 2.0 ); // returns 2.5\n  Console.WriteLine(21.0 / 2) // returns a decimal.\n  ```\n\n#### The Math Library\n\n##### In order to get access to higher order mathematical operations we can use the Math library **inbuilt** in C# and calling Methods, some of them are-\n\n- **Abs** - It returns _the absolute value_ of a var or some math number.\n\n  ```\n  Console.WriteLine(Math.Abs(-40)); // returns 40\n  ```\n\n- **Pow** - Takes two number and _returns the first to the power of the other number_. (also, _works with decimal Numbers_).\n\n  ```\n  Console.WriteLine(Math.Pow(3, 2)); // returns 9 as 3 raised to the power of 2 is 9\n  ```\n\n- **Sqrt** - Returns the _square root of the number given_.\n\n  ```\n  Console.WriteLine(Math.Sqrt(36)); // returns 6\n  ```\n\n- **Max** \u0026 **Min** - returns _the greatest(**Max**) or the smallest(**min**) number that is passed in it_.\n\n  ```\n  Console.WriteLine(Math.Max(4, 90)); // returns 90 as 90 \u003e 4\n  ```\n\n- **Round** - returns the _rounded off number_.\n  ```\n  Console.WriteLine(Math.Round(4.3)); // returns 4\n  ```\n\n### Boolean\n\n- **bool** = only have **true** or **false**\n  ```\n  bool isEmployed = false;\n  ```\n\n### Arrays\n\n- It is a data-structure that allows us to store _multiple values in a single container_.\n\n#### Creating an array\n\n- First declare the _type of data_ it is going to hold.\n\n- Then, put an _open and close square brackets_ to let C# know that this going to be an array.\n\n- Give a _name to the array_.\n\n- There are multiple ways you can declare an array the simplest one is to use _open and close curly braces_.\n\n  ```\n  int [] arrayOfNum = { 4, 4, 4, 456, 454, 854 };\n  ```\n\n- If we don't have values to pass on to the array to _declare an empty array we can use **the constructor function of array** like in the following example by passing how many elements the array will hold beforehand_.\n  ```\n  string[] arrayOfNumbers = new String[45]; // 45 is the number of elements the array can hold.\n  ```\n\n#### Indexing an array\n\n- To gain access to _individual element of an array_ we use _indexing_\n\n  ```\n  Console.WriteLine(myArray[0]); // returns the first element\n  ```\n\n- the **indexing starts with 0**.\n\n### Constant\n\n- **not a var type** just using data without any declaration like\n  ```\n  Console.WriteLine(\"string\");\n  Console.WriteLine(true);\n  ```\n\n## User Input\n\n### Getting user input in a ConsoleApp\n\n- **Console.ReadLine** - Get the _user input **string** into the console_.\n\n  ```\n  string userInput = Console.ReadLine();\n  Console.WriteLine(userInput);\n  ```\n\n- anything passed in the ReadLine method is a _type of string_.\n\n- **Converting** _string to a number_ using **Convert** library's **ToInt32** method, it has to be a number.\n\n  ```\n  int num = Convert.ToInt32(\"34\");\n  ```\n\n- **Converting** _string to a decimal_ using Convert Library **ToDouble method**.\n  ```\n  double number = Convert.ToDouble(\"4.56\"); // also takes int as an argument\n  ```\n\n## Logging\n\n- **Console.Write** - writes the line given in the parentheses but don't changes the line.\n\n- **Console.WiteLine** - writes the line passed into the parentheses and _takes or use the whole line_ and takes to a _new line_.\n\n## .gitignore\n\n- to _create a .gitignore for .NET application go to the terminal in the project dir and type the command_\n  ```\n  dotnet new gitignore\n  ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceleroncoder%2Fcsharp-cheatsheet","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fceleroncoder%2Fcsharp-cheatsheet","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fceleroncoder%2Fcsharp-cheatsheet/lists"}