Advanced Programming
Introduction to C#
Engr. Muhammad Ahsan Raees
Today Lecture
 Syntax
 Comments
 Variable
 Data Types
 Operators
C# Syntax
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
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!");
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
Declaring (Creating) Variables
 A data type 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
Example
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
Implicit Casting
Explicit Casting
Get User Input
 In C# Console.ReadLine() to get user input
 Example
C# Operators-Arithmetic Operators
Operator Name 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--
Example
Assignment
Operator
Operator Example Same As
= 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
Comparison Operator
Operator Name Example
== 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
Example
C# Strings
 Strings are used for storing text.
 A string variable contains a
collection of characters surrounded
by double quotes
C# String Concatenation
 The + operator can be used
between strings to combine them.
This is called concatenation
C# String Concatenation
 You can also use the
string.Concat() method to
concatenate two strings
String Interpolation
 Another option 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

Advanced Programming: Introduction to C#

  • 1.
    Advanced Programming Introduction toC# Engr. Muhammad Ahsan Raees
  • 2.
    Today Lecture  Syntax Comments  Variable  Data Types  Operators
  • 3.
    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
  • 6.
  • 7.
     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
  • 8.
  • 9.
    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
  • 10.
  • 11.
  • 12.
    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--
  • 14.
  • 15.
    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
  • 17.
  • 18.
    C# Strings  Stringsare used for storing text.  A string variable contains a collection of characters surrounded by double quotes
  • 19.
    C# String Concatenation The + operator can be used between strings to combine them. This is called concatenation
  • 20.
    C# String Concatenation You can also use the string.Concat() method to concatenate two strings
  • 21.
    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