Chapter One-Introduction To C#
Chapter One-Introduction To C#
Chapter One-Introduction To C#
College of Informatics
Department of computer science
Window Programming(CoSc4151)
Chapter 1: Introduction to C#
Introduction to C#
Prepared by: kibrom
Haftu(MSc)
Kombolcha
Table of Contents
Introduction to C#
C# Implementation
Microsoft.NET Platform and Its Architecture
Console application development
What is Computer Programming?
Programming language
C# features:
Extremely powerful
Easy to learn
Object-oriented
What You Need to Program using C#?
C# can be implemented as :
Visual Programming
Event-Driven Programming
Object-Oriented Programming
Internet and Web Programming
Visual Programming
In addition to writing program statements to build
portions of apps using Visual Studio’s graphical user
interface (GUI) able conveniently drag and drop
predefined objects
Example buttons and textboxes into place on your
screen, and label and resize them.
Event-Driven Programming
Can write programs that respond to user-initiated
events such as:
mouse clicks, keystrokes, timer expirations and
touches and finger swipes—gestures that are
widely used on smart phones and tablets.
Object-Oriented Programming
C# can model a programming where programs
are organized around objects and data rather
than action and logic.
Internet and Web Programming
Today’s apps can be written with the aim of
communicating among the world’s computers.
this is the focus of Microsoft’s .NET strategy.
you’ll build web-based apps with C# and
Microsoft’s ASP.NET technology.
What is .NET Framework?
What is .NET Framework?
Console applications
integration.
communication.
Windows Form
it contains the graphical representation of any window
displayed in the application.
ADO.NET
an acronym for Advanced Data Objects.
allows connection to data sources for retrieving,
manipulating and updating data from database by
providing access to data sources like SQL server ,XML
etc.
LINQ – language integration Query
a Microsoft programming model and methodology that
essentially adds formal query capabilities into Microsoft .
NET-based programming languages.
it imparts data querying capabilities to .Net languages using a
syntax which is similar to the traditional query language SQL
ASP.NET
a technology for developing, deploying, and running Web
applications by having access to classes in the .NET
Framework using HTML, CSS and JavaScript.
What is Visual Studio?
Visual Studio
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.
C# Programming Concepts
Operators
What Is a Data Type?
The if Statement
Implicit and Explicit
The if-else Statement
Type Conversions
Nested if Statements
What Is a Variable?
Loops The switch-case Statement
Variables and Data Types
Variable name
Data type int count = 5;
Variable value
What Is a Data Type?
A data type:
Is a domain of values of similar characteristics
Defines the type of information stored in the computer
memory (in a variable)
Examples:
Positive integers: 1, 2, 3, …
Alphabetical characters: a, b, c, …
Days of week: Monday, Tuesday, …
Data Type Characteristics
A data type has:
Name (C# keyword or .NET type)
Size (how much memory is used)
Default value
Example:
Integer numbers in C#
Name: int
Size: 32 bits (4 bytes)
Default value: 0
What are Integer Types?
Integer types:
Represent whole numbers
May be signed or unsigned
Have range of values, depending on the size of
memory used
The default value of integer types is:
0 – for integer types (short mean 32 bit), except
0L – for the long type(64 bit representation)
What are Floating-Point Types
Floating-point types:
Represent real numbers
May be signed or unsigned
Have range of values and different precision depending on the
used memory
Can behave abnormally in the calculations
Floating-point types are:
float: 32-bits, precision of 7 digits
double: 64-bits, precision of 15-16 digits
The default value of floating-point types:
Is 0.0F for the float type
The Boolean Data Type
The Boolean data type:
Is declared by the bool keyword
int a = 1;
int b = 2;
Console.WriteLine(greaterAB); // False
Console.WriteLine(equalA1); // True
The Character Data Type
The character data type:
Represents symbolic information
Is declared by the char keyword
Gives each symbol a corresponding integer code
Has a '\0' default value
Takes 16 bits of memory (from U+0000 to U+FFFF)
The String Data Type
The string data type:
Represents a sequence of characters
Is declared by the string keyword
Has a default value null (no value)
Strings are enclosed in quotes:
string s = "Microsoft .NET Framework";
Strings can be concatenated
Using the + operator
Saying Hello – Example
Concatenating the two names of a person to obtain his
full name:
string firstName = "Ivan";
string lastName = "Ivanov";
Console.WriteLine("Hello, {0}!\n", firstName);
long l = 5;
int i = (int) l;
Type Conversions – Example
Example of implicit and explicit conversions:
float heightInMeters = 1.74f; // Explicit conversion
double maxHeight = heightInMeters; // Implicit
Example:
int height = 200;
Identifiers
Identifiers may consist of:
Letters (Unicode)
Digits [0-9]
Underscore "_"
Identifiers
Can begin only with a letter or an underscore
Cannot be a C# keyword
Identifiers
Should have a descriptive name
It is recommended to use only Latin letters
Should be neither too long nor too short
Note:
In C# small letters are considered different than the capital letters (case
Identifiers – Examples
Examples of correct identifiers:
int New = 2; // Here N is capital
int _2Pac; // This identifiers begins with _
string поздрав = "Hello"; // Unicode symbols used
// The following is more appropriate:
string greeting = "Hello";
int n = 100; // Undescriptive
int numberOfClients = 100; // Descriptive
// Overdescriptive identifier:
int numberOfPrivateClientOfTheFirm = 100;
Examples of
int new; // incorrect identifiers:
new is a keyword
int 2Pac; // Cannot begin with a digit
Assigning Values
Assigning of values to variables
Is achieved by the = operator
The = operator has
Variable identifier on the left
Value of the corresponding data type on the right
Could be used in a cascade calling, where assigning is
done from right to left
Assigning Values – Examples
Assigning Values – Examples
int firstValue = 5;
int secondValue;
int thirdValue;
Example:
De Morgan laws
!!A A
!(A || B) !A && !B
!(A && B) !A || !B
if and if-else
if (condition)
{
statements;
}
Condition and Statement
The condition can be:
Boolean variable
Boolean logical expression
Comparison expression
The condition cannot be integer variable (like in C /
C++)
The statement can be:
Single statement ending with a semicolon
Block enclosed in braces
How It Works?
The condition is evaluated
If it is true, the statement is executed
If it is false, the statement is skipped
The if Statement – Example
static void Main()
{
Console.WriteLine("Enter two numbers.");
if (number % 2 == 0)
{
Console.WriteLine("This number is even.");
}
else
{
Console.WriteLine("This number is odd.");
}
Nested if Statements
if and if-else statements can be nested, i.e.
used inside another if or else statement
Every else corresponds to its
closest preceding if
Nested if
if (first == second)
Statements – Example
{
Console.WriteLine( "These two numbers are equal.");
}
else
{
if (first > second)
{
Console.WriteLine("The first number is bigger.");
}
else
{
Console.WriteLine("The second is bigger.");
}
}
The switch-case
Making Several Comparisons at Once
Selects for execution a statement from a list
depending on the value of the switch expression
switch (day)
{
case 1: Console.WriteLine("Monday"); break;
case 2: Console.WriteLine("Tuesday"); break;
case 3: Console.WriteLine("Wednesday"); break;
case 4: Console.WriteLine("Thursday"); break;
case 5: Console.WriteLine("Friday"); break;
case 6: Console.WriteLine("Saturday"); break;
case 7: Console.WriteLine("Sunday"); break;
default: Console.WriteLine("Error!"); break;
}
How switch-case Works?
1. The expression is evaluated
2. When one of the constants specified in a case
label is equal to the expression
The statement that corresponds to that case is
executed
3. If no case is equal to the expression
If there is default case, it is executed
Otherwise the control is transferred to the end point
of the switch statement
Loops
Execute Blocks of Code Multiple Times
What is a Loop?
Loops in C#
• while loops
• do … while loops
• for loops
• foreach loops
Nested loops
What Is Loop?
A loop is a control statement that allows repeating
execution of a block of statements
May execute a code block fixed number of times
May execute a code block while given condition holds
May execute a code block for each member of a
collection
Loops that never end are called an infinite loops
Using while(…) Loop
Repeating a Statement While Given Condition
Holds
The simplest and most frequently used loop
while (condition)
{
statements;
}
do
{
factorial *= n;
n--;
}
while (n > 0);
int n = int.Parse(Console.ReadLine());
int m = int.Parse(Console.ReadLine());
for (int number = n; number <= m; number++)
{
bool prime = true;
int divider = 2;
int maxDivider = Math.Sqrt(num);
while (divider <= maxDivider)
{
if (number % divider == 0)
{
prime = false;
break;
}
divider++;
}
if (prime)
{
Console.Write("{0} ", number);
}
}
Questions?