0% found this document useful (0 votes)
20 views

C# Lecture 1

Uploaded by

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

C# Lecture 1

Uploaded by

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

Duhok Polytechnic University

Shekhan Technical Institute


Information Technology Department

Fundamentals of
Programming by C#

Gheyath M. Othman 2020-2021


Outlines
• What is C#
• C# Applications
• Why C# is used
• C# Program Structure
• C# Output (WriteLine)
• C# Comments
• C# Variables, Constants
• C# Keywords, Identifiers
What is C#
• C# is pronounced "C-Sharp or C-Hash".
• 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.
C# Applications
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
C# General Syntax
using System; Import libraries

namespace test NameSpace: test


{
class Program className:
{ Program
static void Main(string[] args) Main Method
{
C# statements… C# statements
}
}
} End of the
NameSpace
C# General Syntax
• using System means that we can use classes from the System namespace
• namespace is a used to organize your code, and it is a container for classes
and other namespaces.
• The curly braces {} marks the beginning and the end of a block of code
• 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
• Another thing that always appear in a C# program, is the Main method. Any
code inside its curly brackets {} will be executed.
First Example
using System;

namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!"); Print Hello World!
}
}
}

Output:
General Notes
• C# is Case sensitive . a is differ from A (caps letter different from small letter)
• All statements and expressions must end with a semicolon (;)
• The program execution start at the Main method.
• Unlike java , program file name could be different from the class name.
• Console.ReadKey() can be added to the end. This makes the program wait
for a key press and it prevents the screen from running and closing quickly
when the program is launched from Visual Studio .NET.
WriteLine or Write in C#
• 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("I will print on a new line.");
Console.Write("Hello World! ");
Console.Write("I will print on the same line.");

Result:
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 (//).
// This is a comment
Console.WriteLine("Hello World!"); // This is another comment

• 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!");
C# Variables
• Variables are containers for storing data values.
• In C#, there are different types of variables (defined with different keywords)
• Example of basic value types:

• 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
C# 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(1): Create a variable called name of type string and assign it the value
“Kurdistan":

string name = "Kurdistan";


Console.WriteLine(name);
C# Variables
Example(2): Create a variable called age of type int and assign it the value 25:
int age = 25;
Console.WriteLine(age);

Also, we can declare a variable and assigning a value later for example:
int age; // declaration
age = 25; // initialization
Console.WriteLine(age);

Note
If you assign a new value to an existing variable, it will overwrite the previous value:
int age = 25;
age = 20; // age is now 20
Console.WriteLine(age);

Note
Variable must be assigned a value before using it, otherwise, C# will give an error.
int age;
int j = age; //compile-time error: Use of unassigned local variable 'age'
C# Variables
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): For Example:

const int age = 25;


age = 30; // error cannot be changed

Note: 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.

And other Types simple examples

int myAge = 15;


double myDoubleNum = 5.99D;
char myLetter = 'D';
bool myBool = true;
string myText = "Hello";
How to Display Variables
• The method WriteLine() is often used to display variable values to the console
window.

Concatenation:
• To combine both text and a variable, use the + character: for example
string name = "Aram";
Console.WriteLine("Hello " + name);

• You can also use the + character to add a variable to another variable:
string firstName = "Salar";
string lastName = "Raman";
string fullName = firstName + lastName;
Console.WriteLine(fullName);
How to Display Variables
• For numeric values, the + character works as a mathematical operator (notice
that we use int (integer) variables here) : For example

int x = 10;
int y = 5;
15
Console.WriteLine(x + y); // Print the value of x + y

• To declare more than one variable of the same type, use a comma-separated
list:
int x = 5, y = 6, z = 50;
Console.WriteLine(x + y + z);
C# Keywords
• Keywords are reserved words predefined to the C# compiler.
• These keywords cannot be used as identifiers. However, if you want to use
these keywords as identifiers, you may prefix them with the @ character.

Note: This is some keywords of the reserved words


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

General Rules:
• 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

You might also like