Content

  • C# Type Casting
  • C# User Input
  • C# Operators
  • C# Math
  • C# Strings
  • C# Booleans

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: Implicit casting is done automatically when passing a smaller size type to a larger size type
  • Explicit Casting: Explicit casting must be done manually by placing the type in parentheses in front of the value:

Example :

int myInt = 9;
double myDouble = myInt;       // Automatic casting: int to double
            
Console.WriteLine(myInt);      // Outputs 9
Console.WriteLine(myDouble);   // Outputs 9
        
double myDouble = 9.78;
int myInt = (int) myDouble;    // Manual casting: double to int
            
Console.WriteLine(myDouble);   // Outputs 9.78
Console.WriteLine(myInt);      // Outputs 9

It is also possible to convert data types explicitly by using built-in methods, such as Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32 (int) etc.


C# User Input

We can use Console.ReadLine() to get user input.

// Type your username and press enter
Console.WriteLine("Enter username:");
    
// Create a string variable and get user input from the keyboard and store it in the variable
string userName = Console.ReadLine();
    
// Print the value of the variable (userName), which will display the input value
Console.WriteLine("Username is: " + userName);

C# Operators

Operators are used to perform operations on variables and values.

Arithmetic Operators

Arithmetic operators are used to perform common mathematical operations:

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

Assignment Operators

Assignment operators are used to assign values to variables.

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

Comparison Operators

Comparison operators are used to compare two values:

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

Logical Operators

Logical operators are used to determine the logic between variables or values

Operator Name Description Example
&&  Logical and Returns true if both statements are true x < 5 &&  x < 10
||  Logical or Returns true if one of the statements is true x < 5 || x < 4
! Logical not Reverse the result, returns false if the result is true !(x < 5 && x < 10)

C# Math

The C# Math class has many methods that allows you to perform mathematical tasks on numbers.

  • The Math.Max(x,y) method can be used to find the highest value of x and y
  • The Math.Min(x,y) method can be used to find the lowest value of of x and y
  • The Math.Sqrt(x) method returns the square root of x
  • The Math.Abs(x) method returns the absolute (positive) value of x
  • Math.Round() rounds a number to the nearest whole number

C# Strings

A string variable contains a collection of characters surrounded by double quotes

  • String Length: the length of a string can be found with the Length property
  • ToUpper() and ToLower() methods returns the string converted to uppercase or lowercase
  • String Concatenation: The + operator can be used between strings to combine them. This is called concatenation

C# Booleans

We use boolean expressions, for conditional testing

int x = 10;
int y = 9;
Console.WriteLine(x > y); // returns True, because 10 is higher than 9