C# Lecture 1
C# Lecture 1
Fundamentals of
Programming by C#
• 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
{
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
/* 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":
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:
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.
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.
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