Data Types and Variables
Data Types and Variables
Cheatsheets / Learn C#
.toUpper() in C#
IndexOf() in C#
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 1/6
19/01/2024, 13:12 Learn C#: Learn C#: Data Types and Variables Cheatsheet | Codecademy
Bracket Notation
Strings contain characters. One way these char values // Get values from this string.
can be accessed is with bracket notation. We can even
string value = "Dot Net Perls";
store these chars in separate variables.
We access a specific character by using the square
brackets on the string, putting the index position of the //variable first contains letter D
desired character between the brackets. For example,
char first = value[0];
to get the first character, you can specify
variable[0] . To get the last character, you can
subtract one from the length of the string. //Second contains letter o
char second = value[1];
Substring() in C#
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 2/6
19/01/2024, 13:12 Learn C#: Learn C#: Data Types and Variables Cheatsheet | Codecademy
String Concatenation in C#
.ToLower() in C#
In C#, .ToLower() is a string method that string mixedCase = "This is a MIXED case
converts every character to lowercase. If a character
string.";
does not have a lowercase equivalent, it remains
unchanged. For example, special symbols remain
unchanged. // Call ToLower instance method, which
returns a new copy.
string lower = mixedCase.ToLower();
String Length in C#
The string class has a Length property, which returns string a = "One example";
the number of characters in the string.
Console.WriteLine("LENGTH: " + a.Length);
// This code outputs 11
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 3/6
19/01/2024, 13:12 Learn C#: Learn C#: Data Types and Variables Cheatsheet | Codecademy
String Interpolation in C#
Console.WriteLine(multipliedNumber);
// This code would output "The multiplied
ID is 1000."
String New-Line
Console.ReadLine()
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 4/6
19/01/2024, 13:12 Learn C#: Learn C#: Data Types and Variables Cheatsheet | Codecademy
Comments
Comments are bits of text that are not executed. These // This is a single line comment
lines can be used to leave notes and increase the
readability of the program.
Single line comments are created with two /* This is a multi-line comment
forward slashes // . and continues until the end
Multi-line comments start with /* and end
of comment symbol is reached */
with */ . They are useful for commenting out
large blocks of code.
Console.WriteLine()
Arithmetic Operators
result = 10 * 5; // 50
result = 10 / 5; // 2
result = 10 % 5; // 0
Math.Sqrt()
Console.Write(Math.Sqrt(x));
// Prints: 9
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 5/6
19/01/2024, 13:12 Learn C#: Learn C#: Data Types and Variables Cheatsheet | Codecademy
Unary Operator
Math.Pow()
Math.Pow() is a Math class method that is used double pow_ab = Math.Pow(6, 2);
to raise a number to a specified power. It returns a
number of double type.
Console.WriteLine(pow_ab);
// Prints: 36
Print Share
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 6/6