Learn C#_ Learn C#_ Data Types and Variables Cheatsheet _ Codecademy
Learn C#_ Learn C#_ Data Types and Variables Cheatsheet _ Codecademy
Cheatsheets / Learn C#
.toUpper() in C#
In C#, .ToUpper() is a string method that converts string str2 = "This is C# Program
every character in a string to uppercase. If a character
xsdd_$#%";
does not have an uppercase equivalent, it remains
unchanged. For example, special symbols remain
unchanged. // string converted to Upper case
string upperstr2 = str2.ToUpper();
IndexOf() in C#
In C#, the IndexOf() method is a string method used string str = "Divyesh";
to find the index position of a specified character in a
string. The method returns -1 if the character isn’t
found. // Finding the index of character
// which is present in string and
// this will show the value 5
int index1 = str.IndexOf('s');
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 1/6
10-02-2024 16:03 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
10-02-2024 16:03 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 converts string mixedCase = "This is a MIXED case
every character to lowercase. If a character does not
string.";
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
10-02-2024 16:03 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()
The Console.ReadLine() method is used to get user Console.WriteLine("Enter your name: ");
input. The user input can be stored in a variable. This
method can also be used to prompt the user to press
name = Console.ReadLine();
enter on the keyboard.
https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-module-ii/cheatsheet 4/6
10-02-2024 16:03 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
10-02-2024 16:03 Learn C#: Learn C#: Data Types and Variables Cheatsheet | Codecademy
Unary Operator
Math.Pow()
Math.Pow() is a Math class method that is used to double pow_ab = Math.Pow(6, 2);
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