up:: [[Godot MOC]], [[Computing MOC]] tags:: #note/develop #on/computing/programming/languages # C\# C# is a general-purpose high-level programming language developed by Microsoft. It supports multiple [[Programming Paradigms|programming paradigms]]. The language was designed by [[Anders Hejlsberg]] from Microsoft in 2000 and was approved as an international standard by [[Ecma]] in 2002 and [[ISO]]/[[IEC]] in 2003. ## Syntax ### Comments Comments are denoted by `//`, just like in many other languages. Comments span from the `//` to the end of the line. ### Variable Declarations and Definitions To declare a variable, write its type followed by its name. For example: ```csharp int age; ``` To initialize a variable during declaration, use the assignment operator: ```csharp int age = 20; ``` ## Data Types ### Characters and Strings In C#, characters and strings are separate types (just like in C). String literals are denoted with double quotes, while characters are denoted with single quotes. #### Verbatim Strings Verbatim strings are denoted with an `@` at the start. This allows the literal to keep all whitespace and characters, and removes the need to escape backslashes. They can also span multiple lines. They are similar to docstrings in Python. An example of a verbatim string: ```csharp string str = @"This is a string\ with a backslash"; ``` #### String Interpolation String interpolation is performed by prefixing the string with `
and using curly braces to insert data: ```csharp string firstName = "Julianne"; string message = quot;Hello {firstName}!"; ``` ### Numeric Types There are multiple numeric types, including `int`, `float`, `double`, and `decimal`. When defining a `float`, you must append an `f` to the end of the value. For example, `float a = 10.0f`. The same goes for the `decimal` type but with an `m` instead of an `f`. Below is a table describing the precision of the various floating-point types. | Type | Precision | | --------- | ------------ | | `float` | 6-9 digits | | `double` | 15-17 digits | | `decimal` | 28-29 digits | ### Booleans Booleans in C# operate the same way as in other languages. In this case, they are written in lowercase. ## Input and Output ### Writing to Standard Output To write to standard output, use the `Console.Write()` and `Console.WriteLine()` methods. `Console.WriteLine()` appends a newline at the end of its output, whereas `Console.Write()` does not. ## Code Style ### Names #### Variables Variable names should be written in camelCase. Avoid using shorthand notations or abbreviations; instead, opt for longer, more descriptive names that make the purpose of the variable clear. ## References Wikipedia contributors. “C Sharp (Programming Language).” In _Wikipedia_. Wikipedia, The Free Encyclopedia., February 20, 2024. [https://en.wikipedia.org/w/index.php?title=C_Sharp_(programming_language)&oldid=1209083255](https://en.wikipedia.org/w/index.php?title=C_Sharp_(programming_language)&oldid=1209083255). Wagner, Bill. “C# Docs - Get Started, Tutorials, Reference.” Accessed March 15, 2024. [https://learn.microsoft.com/en-us/dotnet/csharp/](https://learn.microsoft.com/en-us/dotnet/csharp/).