Write Line
The Console.WriteLine()
method is used to print text to the console. It can also be used to print other data types and values stored in variables.
Variables and Types
In C#, a variable is a way to store data in the computer’s memory to be used later in the program. C# is a type-safe language, meaning that when variables are declared it is necessary to define their data type.
Declaring the types of variables allows the compiler to stop the program from being run when variables are used incorrectly, i.e, an int
being used when a string
is needed or vice versa.
Finding Square Root in C
In C#, Math.Sqrt() is a Math class method which is used to calulate the square root of the specified value.
Arithmetic Operators
Arithmetic operators are used to modify numerical values:
+
addition operator-
subtraction operator*
multiplication operator/
division operator%
modulo operator (returns the remainder)
Unary Operator
Operators can be combined to create shorter statements and quickly modify existing variables. Two common examples:
++
operator increments a value.--
operator decrements a value.
Math.Pow() in C
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.
.toUpper() in C
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.
IndexOf() in C
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.
Bracket Notation
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.
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 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.
String Concatenation in C
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.
.ToLower() in C
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
The string class has a Length property, which returns the number of characters in the string.
String Interpolation in C
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.
String New-Line
The character combination \n
represents a newline character when inside a C# string
.
For example passing "Hello\nWorld"
to Console.WriteLine()
would print Hello
and World
on separate lines in the console.
Read Line
The Console.ReadLine()
method is used to get user input. The user input can be stored in a variable. This method can also be used to prompt the user to press enter on the keyboard.
Comments
Comments are bits of text that are not executed. These lines can be used to leave notes and increase the readability of the program.
- Single line comments are created with two forward slashes
//
. - Multi-line comments start with
/*
and end with*/
. They are useful for commenting out large blocks of code.