0% found this document useful (0 votes)
50 views24 pages

Web Technology: Semester-Spring 2020

The document discusses various C# programming concepts including type casting, operators, user input, and mathematical functions. It explains the differences between implicit and explicit type casting in C# and provides examples of arithmetic, assignment, comparison, and logical operators. The document also demonstrates how to get user input, convert data types, and use mathematical methods like Max, Min, Sqrt, and Abs.

Uploaded by

Rabia Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views24 pages

Web Technology: Semester-Spring 2020

The document discusses various C# programming concepts including type casting, operators, user input, and mathematical functions. It explains the differences between implicit and explicit type casting in C# and provides examples of arithmetic, assignment, comparison, and logical operators. The document also demonstrates how to get user input, convert data types, and use mathematical methods like Max, Min, Sqrt, and Abs.

Uploaded by

Rabia Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Web Technology

Semester- Spring 2020


Prepare by:

Rabia Aslam Khan


Contents

 Type Casting
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

 Implicit casting is done automatically when passing a smaller size type to a larger size
type:
Example:
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9
Explicit Casting

Explicit casting must be done manually by placing the type in parentheses in front of the
value:
Example
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int

Console.WriteLine(myDouble); // Outputs 9.78


Console.WriteLine(myInt); // Outputs 9
Type Conversion Methods

 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) and Convert.ToInt64 (long):
Example:
int myInt = 10;
double myDouble = 5.25;
bool myBool = true;
Console.WriteLine(Convert.ToString(myInt)); // convert int to string
Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int
Console.WriteLine(Convert.ToString(myBool)); // convert bool to string
Why Conversion?

 Many times, there's no need for type conversion. But sometimes you have to.
C# User Input

 You have already learned that Console.WriteLine() is used to output (print) values. Now
we will use Console.ReadLine() to get user input.
 In the following example, the user can input his or hers username, which is stored in the
variable userName. Then we print the value of userName:
Example
Console.WriteLine("Enter username:");
string userName = Console.ReadLine();
Console.WriteLine("Username is: " + userName);
User Input and Numbers

 The Console.ReadLine() method returns a string. Therefore, you cannot get information
from another data type, such as int. The following program will cause an error:
Example
Console.WriteLine("Enter your age:");
int age = Console.ReadLine();
Console.WriteLine("Your age is: " + age
 Error: cannot implicitly convert type 'string' to 'int'.
Resolving Error

Example
Console.WriteLine("Enter your age:");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is: " + age);
C# Operators

 Operators are used to perform operations on variables and values.


 In the example below, we use the + operator to add together two values:
Example
int x = 100 + 50;
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--
C# Assignment Operators

 Assignment operators are used to assign values to variables.


 In the example below, we use the assignment operator (=) to assign the value 10 to a
variable called x:
Example
int x = 10;
 The addition assignment operator (+=) adds a value to a variable:
Example
int x = 10;
x += 5;
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
C# Comparison Operators

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
C# Logical Operators

Operator Name Description Example


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

 The C# Math class has many methods that allows you to perform mathematical tasks on
numbers.
Math.Max(x,y)

 The Math.Max(x,y) method can be used to find the highest value of x and y:
Example
Math.Max(5, 10);
Math.Min(x,y)

 The Math.Min(x,y) method can be used to find the lowest value of of x and y:
Example
Math.Min(5, 10);
Math.Sqrt(x)

 The Math.Sqrt(x) method returns the square root of x:


Example
Math.Sqrt(64);
Math.Abs(x)

 The Math.Abs(x) method returns the absolute (positive) value of x:


Example
Math.Abs(-4.7);
Math.Round()

 Math.Round() rounds a number to the nearest whole number:


Example
Math.Round(9.99);
C# String
Any Question

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy