.
NET Programming
Chapter 1: C# Programming languages
C# fundamentals
1.INTRODUCTION TO C#
What is C# ?
- C# (C-Sharp) is a programming language developed 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# 12,
was released in November 2023.
- C# is used to develop web applications, desktop applications, mobile
apps, games and much more.
- A modern, object-oriented programming language developed by
Microsoft.
- Part of the .NET ecosystem.
- Combines the best of C++ and Java.
2. INTEGRATED DEVELOPMENT ENVIRONMENT (IDE)
3. SYNTAX
The Basic C# file when create a console project that called
“Program.cs”
3. SYNTAX
Example 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 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.
- Line 7: Another thing that always appear in a C# program is the Main method. Any code
inside its curly brackets {} will be executed. You don't have to understand the keywords
before and after Main. You will get to know them bit by bit while reading this tutorial.
- 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.
4. COMMENT
How can we comment code in the C# programming language ?
5. VARIABLES
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
Syntax
type variableName = value;
string subjectName = “EIU .Net Programming”;
int myNum = 99;
5. VARIABLES
Constants
- Cannot declare a constant variable without assigning the value
- Eead-only
- Cannot overwrite existing values
5. VARIABLES
How to use variables
5. VARIABLES
Identifiers and clean variables
6. DATA TYPES
7. TYPE CASTING
Type casting is when you assign a value of one data type to another type.
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
8. USER INPUT
8. USER INPUT
Handle error for user Input
9. OPERATORS
Operators are used to perform operations on variables and values.
9. OPERATORS
Assignment Operators
- Assignment operators are used to assign values to variables.
9. OPERATORS
Comparison Operators
- Comparison operators are used to compare two values (or variables). This is
important in programming, because it helps us to find answers and make
decisions.
9. OPERATORS
Logical Operators
- Logical operators are used to determine the logic between variables or values
10. STRING
A string variable contains a collection of characters surrounded by
double quotes
A string in C# is actually an object, which contain properties and
methods that can perform certain operations on strings.
Length:
Uppercase or lowercase:
10. STRING
String Concatenation
The “+” operator can be used between strings to combine them.
This is called concatenation:
String Interpolation
10. STRING
Special Characters
The strings must be written within quotes, C# will misunderstand
this string, and generate an error:
C# has a bool data type, which can take the values true or false
10. BOOLEANS
C# has a bool data type, which can take the values true or false.
C# has a bool data type, which can take the values true or false
10. BOOLEANS
Boolean Expression
- A Boolean expression returns a boolean value: True or False, by
comparing values/variables.
- Useful to build logic, and find answers.
10. BOOLEANS
Boolean Expression
More Example:
11. If ... Else
Conditions
C# supports the usual logical conditions from mathematics:
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
• Equal to a == b
• Not Equal to: a != b
C# has the following conditional statements:
• Use if to specify a block of code to be executed, if a specified condition is true
• Use else to specify a block of code to be executed, if the same condition is
false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed
11. If ... Else
The If Statements
11. If ... Else
The Else Statement
11. If ... Else
The Else If Statement
11. If ... Else
Short Hand If...Else
variable = (condition) ? expressionTrue : expressionFalse;
12. Switch
Use the switch statement to select one of many code blocks to be executed.
switch(expression)
{
case 1:
// code block
break;
case 2:
// code block
break;
default:
// code block
break;
}
12. Switch
Example:
13. LOOP
While Loop
Syntax:
while (condition)
{
// code block to be executed
}
13. LOOP
For Loop
Syntax:
for (statement 1; statement 2; statement 3)
{
// code block to be executed
}
13. LOOP
Foreach Loop
Syntax:
foreach (type variableName in arrayName)
{
// code block to be executed
}
13. LOOP
Break and Continue
- BREAK: The break statement can also be used to jump out of a loop.
- CONTINUE: The continue statement breaks one iteration (in the loop),
if a specified condition occurs, and continues with the next iteration in
the loop.
14. Arrays
Arrays are used to store multiple values in a single variable, instead of
declaring separate variables for each value
Access the Elements of an Array:
Change an Array Element:
Array Length:
14. Arrays
Other Ways to Create an Array
14. Arrays
Loop Through Arrays
15. Methods
class Program
{
static void MyMethod()
{
// code to be executed
}
}
MyMethod(): is the name of the method
static means that the method belongs to the Program class and not an
object of the Program class.
void means that this method does not have a return value. You can
choose another type like int, string, array, object,….
15. Methods
Call a Method
15. Methods
Method Parameters
15. Methods
Default Parameter Value
15. Methods
Return Values
15. Methods
Named Arguments
Object-oriented programming with C#
1. What is OOP?
- OOP stands for Object-Oriented Programming.
- Object-oriented programming has several advantages over procedural programming:
• OOP is faster and easier to execute
• OOP provides a clear structure for the programs
• OOP makes the code easier to maintain, modify and debug
• OOP makes it possible to create full reusable applications with less code and shorter
development time
- Classes and objects are the two main aspects of object-oriented programming.
- A class is a template for objects, and an object is an instance of a class
2. Classes and Objects
To create a class, use the class keyword:
Create an Object: Create an object called "myObj" and use it to print the value of color:
3. Class Members
Fields and methods inside classes are often referred to as "Class Members":
3. Class Members
Fields
Variables inside a class are called fields, you can access them by creating an object of the
class, and by using the dot syntax (.).
The following example will create an object of the Car class, with the name myObj. Then we
print the value of the fields color and maxSpeed:
3. Class Members
Object Methods
Methods normally belong to a class, and they define how an object of a class behaves.
Just like with fields, you can access methods with the dot syntax. However, note that the
method must be public. And remember that we use the name of the method followed by two
parentheses () and a semicolon ; to call (execute) the method:
4. Constructors
A constructor is a special method that is used to initialize objects. The advantage of a
constructor, is that it is called when an object of a class is created. It can be used to
set initial values for fields:
4. Constructors
Constructor Parameters
Constructors can also take parameters, which is used to initialize fields.
The following example adds a string modelName parameter to the constructor. Inside the
constructor we set model to modelName (model=modelName). When we call the
constructor, we pass a parameter to the constructor ("Mustang"), which will set the value of
model to "Mustang":
5. Access Modifiers
public string color;
The public keyword is an access modifier, which is used to set the access level/visibility for
classes, fields, methods and properties.
C# has the following access modifiers:
5. Access Modifiers
Private Modifier
If you declare a field with a private access modifier, it can only be accessed within the same
class.
If you try to access it outside the class, an error will occur.
5. Access Modifiers
WHY ACCESS MODIFIERS?
To control the visibility of class members (the security level of each individual class and
class member).
To achieve "Encapsulation" - which is the process of making sure that "sensitive" data is
hidden from users. This is done by declaring fields as private.
6. Properties (Get and Set)
Properties and Encapsulation
Encapsulation mean is to make sure that "sensitive" data is hidden from users. To achieve
this, you must:
• Declare fields/variables as private
• Provide public get and set methods, through properties, to access and update the value
of a private field
6. Properties (Get and Set)
Properties
- Private variables can only be accessed within the same class (an outside class has no
access to it)
- A property is like a combination of a variable and a method, and it has two methods: a get
and a set method:
6. Properties (Get and Set)
Automatic Properties (Short Hand)
C# also provides a way to use short-hand / automatic properties, where you do not have to
define the field for the property, and you only have to write get; and set; inside the property.
The result is the
same; the only
difference is
less code.
7. Inheritance
Inheritance (Derived and Base Class)
In C#, it is possible to inherit fields and methods from one class to another. We group
the "inheritance concept" into two categories:
Derived Class (child) - the class that inherits from another class
Base Class (parent) - the class being inherited from
To inherit from a class, use the “ : ” symbol.
7. Inheritance
Example:
8. Polymorphism
Polymorphism and Overriding Methods
Polymorphism means "many forms", and it occurs when we have many classes that are
related to each other by inheritance.
8. Polymorphism
C# provides an option to override the base class method, by adding the virtual keyword to
the method inside the base class, and by using the override keyword for each derived class
methods
9. Abstract
The abstract keyword is used for classes and methods:
Abstract class: is a restricted class that cannot be used to create objects (to access it, it
must be inherited from another class).
Abstract method: can only be used in an abstract class, and it does not have a body. The
body is provided by the derived class (inherited from).
An abstract class can have both abstract and regular methods:
9. Abstract
Example:
9. Interface
Another way to achieve abstraction in C#, is with interfaces.
An interface is a completely "abstract class", which can only contain abstract
methods and properties (with empty bodies):
9. Interface
Example in the real project:
9. Interface
Example in the real project:
Start your future at EIU
Thank You