C# Syntax
using System;
namespaceHelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
4.
Comments
Single-line Comments
Single-line comments start with two forward slashes (//).
// This is a comment
Console.WriteLine("Hello World!");
Multiline Comments
Multi-line comments start with /* and ends with */.
/* The code below will print the words Hello World
to the screen, and it is amazing */
Console.WriteLine("Hello World!");
5.
Variable
In C#,there are different types of variables (defined with different
keywords), for example:
int - stores integers (whole numbers), without decimals, such as 123 or -123
double - stores floating point numbers, with decimals, such as 19.99 or -19.99
char - stores single characters, such as 'a' or 'B'. Char values are surrounded by
single quotes
string - stores text, such as "Hello World". String values are surrounded by
double quotes
bool - stores values with two states: true or false
A datatype specifies the size
and type of variable values.
It is important to use the
correct data type for the
corresponding variable; to
avoid errors, to save time and
memory, but it will also make
your code more maintainable
and readable. The most
common data types are:
Data Types
Data Type Size Description
int 4 bytes Stores whole numbers from -
2,147,483,648 to 2,147,483,647
long 8 bytes Stores whole numbers from -
9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers.
Sufficient for storing 6 to 7
decimal digits
double 8 bytes Stores fractional numbers.
Sufficient for storing 15 decimal
digits
bool 1 byte Stores true or false values
char 2 bytes Stores a single character/letter,
surrounded by single quotes
string 2 bytes
per
character
Stores a sequence of
characters, surrounded by
double quotes
C# Type Casting
Type casting is when you assign a value of one data type to another type.
In C#, there are two types of casting:
Implicit Casting (automatically) - converting a smaller type to a larger type
size
char -> int -> long -> float -> double
Explicit Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char
Get User Input
In C# Console.ReadLine() to get user input
Example
13.
C# Operators-Arithmetic Operators
OperatorName Description Example
+ Addition Adds together two values x + y
- Subtraction Subtracts one value from another x - y
* Multiplication Multiplies two values x * y
/ Division Divides one value by another x / y
% Modulus Returns the division remainder x % y
++ Increment Increases the value of a variable
by 1
x++
-- Decrement Decreases the value of a variable
by 1
x--
Assignment
Operator
Operator Example SameAs
= x = 5 x = 5
+= x += 3 x = x + 3
-= x -= 3 x = x - 3
*= x *= 3 x = x * 3
/= x /= 3 x = x / 3
%= x %= 3 x = x % 3
&= x &= 3 x = x & 3
|= x |= 3 x = x | 3
^= x ^= 3 x = x ^ 3
>>= x >>= 3 x = x >> 3
<<= x <<= 3 x = x << 3
16.
Comparison Operator
Operator NameExample
== Equal to x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y
String Interpolation
Anotheroption of string
concatenation, is string
interpolation, which substitutes
values of variables into
placeholders in a string. Note that
you do not have to worry about
spaces, like with concatenation