Ch 3
Ch 3
Object-Oriented Fundamentals in C#
3.1 Language fundamentals
• C# is a simple, modern, general-purpose, object-oriented
programming language developed by Microsoft together with the
.NET platform.
• C# is extremely powerful programming language and used in
millions of applications around the world.
• There is highly diverse software developed with C# and on
the .NET platform: office applications, web applications,
websites, desktop applications, mobile applications, games and
many others.
Reasons make C# a widely used
professional language:
•It is a modern, general-purpose programming language
•It is object oriented.
•It is component oriented.
•It is easy to learn.
•It produces efficient programs.
•It can be compiled on a variety of computer platforms.
•It is a part of .Net Framework.
C# - Program Structure
•A C# program consists of //Namespaces
using System;
the following parts: namespace ConsoleApplication1
{
• Namespace declaration class Program
{ static int sum; //Class attribute
• A class //Main method
static void Main(string[] args)
• Class methods (user defined) {
sum=new Program().UserInput(4,5);
• Class attributes Console.WriteLine(sum);
Console.ReadKey();
• A Main method }
//User defined method
• Statements and Expressions public int UserInput(int a, int b) {
return a+b;
• Comments }
}
}
C# - Important points
• It is worth to note the following points:
• C# is case sensitive.
• All statements and expression must end with a semicolon (;).
• The program execution starts at the Main method.
• Unlike Java, program file name could be different from the class
name.
3.1.1 Variables, Data Types and Operators in C#
A. Variable
A variable is a container/storage of information, which
can change its value during execution.
It provides means for:
• Storing information;
• Retrieving the stored information;
• Modifying the stored information.
Variables are characterized by:
Type (of the information preserved in them), for example int;
Name (identifier), for example age;
Value (stored information), for example 25.
Variable Naming Rules
Variable names can contain the letters a-z, A-Z, the digits 0-9 as well as the character '_'.
Variable names cannot start with a digit.
keywords of the C# language can’t be used as a variable names
For example: base, char, default, int, object, this, null and many others cannot be used as
variable names.
Declaring & Initializing Variables
Declaration: preparing a variable for storage
The data type of variable is implicitly recognized with the type of value that is assigned to it
E.g. var x=10; //x is of type int var s=“Hello”; //s is of type string
var d=12.5f; //d is of type float var y; //y is invalid
B. Data types
Data types: are sets of values that have similar characteristics.
Data types are characterized by:
• Introduced in C# 2.0 which allows storing the Null value under the Value
type
• By default Null value can be stored only under reference type but not value
types
• The advantage of storing Null values in value type provides improved
interaction while working with databases.
• Database allows storing of Null values both under value types and
reference types.
• To declare a Null value, the type name suffix with a question mark
Example
string name=null; //valid
int x=null; //invalid
int? x=null; //valid
C. Operators
Operators in C# can be separated in several different categories:
Arithmetic operators – they are used to perform simple mathematical operations.
-, +, *, /, %, ++, --
Assignment operators – allow assigning values to variables. =, +=, -=, *=, /=, %=
Comparison operators – allow comparison of two literals and/or variables. ==,!=,
>, <, >=, <=
Logical operators – operators that work with Boolean data types and Boolean
expressions. &&, ||, !, ^
Binary operators – used to perform operations on the binary representation of
numerical data. &, |, ^, ~, <<, >>
Type conversion operators – allow conversion of data from one type to another.
(type), as, is, typeof, sizeof
Conditional operator – allows to perform operations based on the evaluation of an
expression. (expression)?operation if true?operation if false;
3.1.2 Control Flow
1. Conditional statements 2. Loop statements
if, while loop
if...else, do…while loop
if...else if and for loop
Nested if Statement Nested loop
Switch statement for each loop
3. Jump statements
Continue, Go to,
Return and break
1. Conditional statements
i. "if “Statement
if (Boolean expression)
{
Body of the conditional statement;
}
The expression in the brackets which follows the keyword if must return the
Boolean value true or false.
If the value is true, then the body of a conditional statement is executed.
If the result is false, then the operators in the body will be skipped.
ii. if...else iii. if...else if
Statement Statement
if (Boolean expression) if (Boolean expression)
{
{
Body of the conditional statement;
Body of the conditional statement;
}
} else if (Boolean expression)
else {
{ Body of the else statement;
Body of the else statement; }
Else
}
{
Body of the else statement;
}
iv. Nested "if" Statement
An if...else statement can exist within another if...else statement
Syntax:
if (boolean-expression)
{
if (nested-expression-1)
{
// code to be executed
}
else
{
// code to be executed
}
}
v. Switch statement
switch (variable_to_be_checked)
{
case value_1: Switch-case chooses which part of the
statements; programming code to execute based
break; on the calculated value of a certain
case value_2: expression
statements;
break;
// …
// …
default:
statements;
break;
}
2. Loop statements
Enable us repeated execution of a sequence of operations
a. While Loop
• The body of the while loop may not be executed even once if
in the beginning the condition of the cycle returns false
while (condition)
{
loop body;
}
b. Do-While Loop
•Syntax of a parameter:
([ref/out]<type> <varName> [=value] )
• Classes Vs components
• Operations on array
• Properties and indexers
• Constructor types and destructor
• Delegates
• Multi-threading