Web Technology
Semester- Spring 2020
Prepare by:
Rabia Aslam Khan
Contents
Introduction to C#
Introduction
It is an object-oriented programming language created by Microsoft that runs on the .NET
Framework.
C# has roots from the C family, and the language is close to other popular languages like
C++ and Java.
The first version was released in year 2002. The latest version, C# 8, was released in
September 2019.
Cont.
C# is used for:
Mobile applications
Desktop applications
Web applications
Web services
Web sites
Games
VR
Database applications
And much, much more!
Why Use C#?
It is one of the most popular programming language in the world
It is easy to learn and simple to use
It has a huge community support
C# is an object oriented language which gives a clear structure to programs and allows
code to be reused, lowering development costs.
As C# is close to C, C++ and Java, it makes it easy for programmers to switch to C# or
vice versa
Sample CS Code
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
Program Explained
Line 1: using System means that we can use classes from the System namespace.
Line 2: A blank line. C# ignores white space. However, multiple lines makes the code
more readable.
Line 3: namespace is a used to organize your code, and it is a container for classes and
other namespaces.
Line 4: The curly braces {} marks the beginning and the end of a block of code.
Line 5: class is a container for data and methods, which brings functionality to your
program. Every line of code that runs in C# must be inside a class. In our example, we
named the class Program.
Cont.
Line 9: Console is a class of the System namespace, which has a WriteLine() method that
is used to output/print text. In our example it will output "Hello World!".
If you omit the using System line, you would have to write System.Console.WriteLine()
to print/output text.
WriteLine or Write
The most common method to output something in C# is WriteLine(), but you can also use
Write().
The difference is that WriteLine() prints the output on a new line each time, while Write()
prints on the same line
Example
Console.WriteLine("Hello World!");
Console.WriteLine(“This line will be printed on a new line.");
Console.Write("Hello World! ");
Console.Write(“This line will be printed on the same line.");
C# Comments
Comments can be used to explain C# code, and to make it more readable. It can also be
used to prevent execution when testing alternative code.
Single-line comments start with two forward slashes (//).
Any text between // and the end of the line is ignored by C# (will not be executed).
This example uses a single-line comment before a line of code:
C# Multi-line Comments
Multi-line comments start with /* and ends with */.
Any text between /* and */ will be ignored by C#.
C# Variables
Variables are containers for storing data values.
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
To create a variable, you must specify the type and assign it a value:
Syntax
type variableName = value;
Where type is a C# type (such as int or string), and variableName is the name of the
variable (such as x or name). The equal sign is used to assign values to the variable.
Example
Create a variable called name of type string and assign it the value "John":
string name = "John";
Console.WriteLine(name);
Create a variable called myNum of type int and assign it the value 15:
int myNum = 15;
Console.WriteLine(myNum);
Constants
you can add the const keyword if you don't want others (or yourself) to overwrite existing
values (this will declare the variable as "constant", which means unchangeable and read-
only)
Example
const int myNum = 15;
myNum = 20; // error
The const keyword is useful when you want a variable to always store the same value, so
that others won't mess up your code.
You cannot declare a constant variable without assigning the value. If you do, an error will
occur: A const field requires a value to be provided.
Display Variables
The WriteLine() method is often used to display variable values to the console window.
To combine both text and a variable, use the + character:
Example
string name = "John";
Console.WriteLine("Hello " + name);
C# Identifiers
All C# variables must be identified with unique names.
These unique names are called identifiers.
Identifiers can be short names (like x and y) or more descriptive names (age, sum, totalVolume).
Note: It is recommended to use descriptive names in order to create understandable and
maintainable code:
Example
// Good
int minutesPerHour = 60;
// OK, but not so easy to understand what m actually is
int m = 60;
Rules for naming variable s
The general rules for constructing names for variables (unique identifiers) are:
Names can contain letters, digits and the underscore character (_)
Names must begin with a letter
Names should start with a lowercase letter and it cannot contain whitespace
Names are case sensitive ("myVar" and "myvar" are different variables)
Reserved words (like C# keywords, such as int or double) cannot be used as names
C# Data Types
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.
C# 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 bit Stores true or false values
char 2 bytes Stores a single character/letter, surrounded by single quotes
string 2 bytes per Stores a sequence of characters, surrounded by double quotes
character
Any Question