Web Technology: Semester-Spring 2020
Web Technology: Semester-Spring 2020
Introduction to C#
Introduction
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#?
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.");
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
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
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