0% found this document useful (0 votes)
2 views

Learn C#_ Learn C#_ Data Types and Variables Cheatsheet _ Codecademy

This document is a cheatsheet for learning C# data types and variables, covering various string methods such as .ToUpper(), IndexOf(), and Substring(). It also explains string concatenation, interpolation, escape characters, and the use of Console methods for user input and output. Additionally, it includes information on arithmetic operators, unary operators, and mathematical functions like Math.Sqrt() and Math.Pow().

Uploaded by

Michael Okocha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Learn C#_ Learn C#_ Data Types and Variables Cheatsheet _ Codecademy

This document is a cheatsheet for learning C# data types and variables, covering various string methods such as .ToUpper(), IndexOf(), and Substring(). It also explains string concatenation, interpolation, escape characters, and the use of Console methods for user input and output. Additionally, it includes information on arithmetic operators, unary operators, and mathematical functions like Math.Sqrt() and Math.Pow().

Uploaded by

Michael Okocha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

10-02-2024 16:03 Learn C#: Learn C#: Data Types and Variables Cheatsheet | Codecademy

Cheatsheets / Learn C#

Learn C#: Data Types and Variables

.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();

//upperstr2 contains "THIS IS C# PROGRAM


XSDD_$#%"

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');

Console.WriteLine("The Index Value of


character 's' is " + index1);
//The Index Value of character 's' is 5

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];

//last contains letter s


char last = value[value.Length - 1];

Escape Character Sequences in C#

In C#, an escape sequence refers to a combination of


characters beginning with a back slash \ followed by
letters or digits. It’s used to make sure that the program
reads certain characters as part of a string. For
example, it can be used to include quotation marks
within a string that you would like to print to console.
Escape sequences can do other things using specific
characters. \n is used to create a new line.

Substring() in C#

In C#, Substring() is a string method used to retrieve string myString = "Divyesh";


part of a string while keeping the original data intact.
string test1 = myString.Substring(2);
The substring that you retrieve can be stored in a
variable for use elsewhere in your program.

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#

Concatenation is the process of appending one string // Declare strings


to the end of another string. The simplest method of
string firstName = "Divyesh";
adding two strings in C# is using the + operator.
string lastName = "Goardnan";

// Concatenate two string variables


string name = firstName + " " + lastName;
Console.WriteLine(name);
//Ths code will output Divyesh Goardnan

.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();

//variable lower contains "this is a


mixed case string."

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#

String interpolation provides a more readable and int id = 100


convenient syntax to create formatted strings. It allows
us to insert variable values and expressions in the
middle of a string so that we don’t have to worry about // We can use an expression with a string
punctuation or spaces. interpolation.
string multipliedNumber = $"The
multiplied ID is {id * 10}.";

Console.WriteLine(multipliedNumber);
// This code would output "The multiplied
ID is 1000."

String New-Line

The character combination \n represents a newline Console.WriteLine("Hello\nWorld");


character when inside a C# string .
For example passing "Hello\nWorld" to
Console.WriteLine() would print Hello and // The console output will look like:
World on separate lines in the console. // Hello
// World

Variables and Types

A variable is a way to store data in the computer’s string foo = "Hello";


memory to be used later in the program. C# is a type-
string bar = "How are you?";
safe language, meaning that when variables are
declared it is necessary to define their data type. int x = 5;
Declaring the types of variables allows the compiler to
stop the program from being run when variables are
Console.WriteLine(foo);
used incorrectly, i.e, an int being used when a string
is needed or vice versa. // Prints: Hello

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()

The Console.WriteLine() method is used to print Console.WriteLine("Hello, world!");


text to the console. It can also be used to print other
data types and values stored in variables.
// Prints: Hello, world!

Arithmetic Operators

Arithmetic operators are used to modify numerical int result;


values:
+ addition operator
- subtraction operator result = 10 + 5; // 15
* multiplication operator
/ division operator result = 10 - 5; // 5
% modulo operator (returns the remainder)

result = 10 * 5; // 50

result = 10 / 5; // 2

result = 10 % 5; // 0

Math.Sqrt()

Math.Sqrt() is a Math class method which is used double x = 81;


to calculate the square root of the specified value.

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

Operators can be combined to create shorter int a = 10;


statements and quickly modify existing variables. Two
a++;
common examples:
++ operator increments a value.
-- operator decrements a value. Console.WriteLine(a);
// Prints: 11

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

You might also like