Key Concepts

Review core concepts you need to learn to master this subject

Print Text to Console in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, Console.WriteLine() is used to print text to the console. It can also be used to print other data types and values stored in variables.

Getting User Input in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, the Console.ReadLine() method is used to get user input. The user input can be stored in a variable for later use. Console.ReadLine() can also be used to prompt the user to press enter on the keyboard.

C# Arithmetic Operators

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

C# arithmetic operators are used to perform arithmetic operations such as addition, subtraction, multiplication, division, and modulo.

Addition: The + operator adds two operands.

Subtraction: The - operator subtracts two operands.

Multiplication: The * operator multiplies two operands.

Division: The / operator divides the first operand by the second.

Modulo: The % operator returns the remainder when the first operand is divided by the second.

C# Unary Operator

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, operators can be combined to create shorter statements and quickly modify existing variables. These unary operators operate on a single operand.

Two common examples:

Increment: The ++ operator is used to increment the value of an integer.

Decrement: The -- operator is used to decrement the value of an integer.

Finding Square Root in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, Math.Sqrt() is a Math class method which is used to calulate the square root of the specified value.

Math.Pow() in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, Math.Pow() is a Math class method that is used to raise a number to a specified power. It returns a number of double type.

String Concatenation in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

Concatenation is the process of appending one string to the end of another string. The simplest method of adding two strings in C# is using the + operator.

Escape Character Sequences in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

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.

String Interpolation in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

String interpolation provides a more readable and 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 punctuation or spaces.

.toUpper() in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, .ToUpper() is a string method that converts every character in a string to uppercase. If a character does not have an uppercase equivalent, it remains unchanged. For example, special symbols remain unchanged.

.ToLower() in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, .ToLower() is a string method that converts every character to lowercase. If a character does not have a lowercase equivalent, it remains unchanged. For example, special symbols remain unchanged.

String Length in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

The string class has a Length property, which returns the number of characters in the string.

IndexOf() in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, the IndexOf() method is a string method used to find the index position of a specified character in a string. The method returns -1 if the character isn’t found.

Substring() in C#

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C#, Substring() is a string method used to retrieve part of a string while keeping the original data intact. The substring that you retrieve can be stored in a variable for use elsewhere in your program.

Bracket Notation

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

Strings contain characters. One way these char values can be accessed is with bracket notation. We can even 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 desired character between the brackets. For example, 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.

C# Comments

int value = 10; // Variable Console.WriteLine(value); Console.WriteLine(50);

In C# you can comment out lines of code so that they are not executed. These lines can be used to leave notes and increase the readability of the program. There are two types of comments you can make use of:

Single Line Comment: A single line comment is created with double slash //. Any text or code following the double slash on the same line is ignored during compilation.

Multi-Line Comment: A multi-line comment starts with symbol /* and with end symbol */. Any information between the two symbols is treated as a comment. It is useful for commenting out large blocks of code.

Chevron Left Icon
Data Types and Variables
Lesson 1 of 3
Chevron Right Icon
  1. 1
    When we write programs, we’re telling the computer how to process pieces of information, like the numbers in a calculation, or printing text to the screen. So how does a computer distinguish betwee…
  2. 2
    Data types tell us a few things about a piece of data, like: - How it can be stored - What operations we can perform with it - Different methods it can be used with Data types are present in al…
  3. 3
    When we use data in our programs, it’s good practice to save them in a variable. A variable is basically like a box in our computer memory where we can store values used in our code. In C#, data…
  4. 4
    When you program, you’ll come across a lot of errors. And that’s ok! And when you’re first starting to learn a strongly-typed language, they can be pretty common. So, what happens if you forget t…
  5. 5
    Because variables have to be strictly typed, there may be some circumstances where we want to change the type of data a variable is storing. This strategy is known as data type conversion. For …
  6. 6
    Congratulations! In this lesson, you learned about how data types and variables work in C#. Topics covered include: - C# built-in data types, including int, double, char, string, and bool - How to…
  1. 1
    No matter what you want to make in C#, you’ll need numbers! Art: what are the dimensions of your canvas? Games: how fast can your player move? Business: how much does your product …
  2. 2
    In C#, there are several ways of representing numerical data. Your usage of each will depend on your application. When choosing a data type, think about the following questions: Do I need a whole …
  3. 3
    So what can we do with numerical data? A first step is to write expressions using arithmetic operators. Arithmetic operators include: - addition + - subtraction - - multiplication * - division …
  4. 4
    Often we need to update a variable in our program. We can do so by modifying that variable using an arithmetic expression, then re-saving it to the same variable name: int apple = 0; apple = apple…
  5. 5
    One arithmetic operator that we haven’t covered yet and may be less familiar is a modulo. A modulo returns a remainder, what is left over when we divide a number by another number. 4 % 3 = 1 …
  6. 6
    So how do we do more advanced mathematical operations? For example, how would we perform a square root on a number if the program doesn’t recognize a square root symbol? There are several built-in…
  7. 7
    We introduced you to a few common built-in methods, but there are many more out there! Now’s a good time to practice your documentation search skills. Take a moment to look up the following built-…
  8. 8
    Great job! You just learned about numerical data types and how to work with numerical data in a few different ways: - Use arithmetic operators to write expressions. - Combine operators together to …
  1. 1
    Working with text is a fundamental part of writing programs. Whether it’s to provide instructions to a user, gathering data like a name or address, or writing a new form of poetry, text enables us …
  2. 2
    A string is a group of characters surrounded by quotation marks, like “https://codecademy.com” or “To be or not to be.” A string is just a collection of a smaller data type, char, which is a si…
  3. 3
    Often, we want to combine strings together, or combine strings with a value that we’ve saved to a variable. A common way to do is by using string concatenation. String concatenation is when we …
  4. 4
    While string concatenation is easy to read, it can get annoying to write. Another option is string interpolation. String interpolation was introduced in C# 6 and it enables us to insert our varia…
  5. 5
    In addition to containing the value of a piece of text, strings also contain information about themselves. It can be useful to know these properties when working with strings. There are several bui…
  6. 6
    We can also use built-in .NET methods to grab parts of strings or specific characters in a string. #### Substring .Substring() grabs part of a string using the specified character position and co…
  7. 7
    There are also built-in .NET methods that we can use to manipulate text data. Using these methods on a string doesn’t change the string itself, but creates an entirely new one. #### ToUpper, ToLow…
  8. 8
    Great job! You just learned about how to work with textual data in a few different ways: - How to save char and string values to a variable. - Use the addition symbol (+) to concatenate strings. -…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo