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

Chapter 1 - Getting Started

The document provides an introduction to event driven programming in C#. It discusses basics like namespaces, main methods, data types, operators, methods, loops and arrays. It also covers topics like implicit and explicit conversions, static vs instance methods, and different parameter types in methods.

Uploaded by

dagne
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)
44 views

Chapter 1 - Getting Started

The document provides an introduction to event driven programming in C#. It discusses basics like namespaces, main methods, data types, operators, methods, loops and arrays. It also covers topics like implicit and explicit conversions, static vs instance methods, and different parameter types in methods.

Uploaded by

dagne
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/ 44

Event driving programming

Event driving programming(C#) by Dagne W. 1


Chapter One

Getting Started

Event driving programming(C#) by Dagne W. 2


Introduction
Introduction to C#
Objective
 Understand Basic Structure of C# program
 Understand what is a Namespace
 Understand Purpose of the main method

Event driving programming(C#) by Dagne W. 3


What is C#
 C#(C sharp) is a strongly typed object oriented
programming language .
 It is an open source programming language, that is
simple, modern and flexible.
 C# created by anders heijlsberg from Microsoft
company in 2000
 Microsoft company created c# from Microsoft
window development.

Event driving programming(C#) by Dagne W. 4


What is C#
 What is C#
C# is pronounced as "C-Sharp". It is an object-oriented
programming language provided by Microsoft that runs on
.Net Framework.
 By the help of C# programming language, we can develop
different types of secured and robust applications:
 Window applications
 Web applications
 Distributed applications
 Web service applications
 Game Applications
 Database applications etc.

Event driving programming(C#) by Dagne W. 5


C# Features

Event driving programming(C#) by Dagne W. 6


Sample Program

Event driving programming(C#) by Dagne W. 7


Using System declaration

Why Namespace? : A namespace is used to


organize your code and is collection of classes,
interfaces, structs, enums and delegates.
They also provide assistance in avoiding name
clashes.
Main method is the startup your application.

Event driving programming(C#) by Dagne W. 8


Reading and writing to Console
Reading from the console using redline()
and read()
Writing to the console using write() and
writeline()

Event driving programming(C#) by Dagne W. 9


Reading and writing .

Event driving programming(C#) by Dagne W. 10


Built in type in C#..
 Boolean types-only true or false
 Integral types-sbyte,byte, ushort, short, int, uint, long,
ulong, char
 Floating types-float and double
 Decimal types
 String types
Escape sequences in C#
\a, \b,\r, \f, \t, \\, \’, \” …

Event driving programming(C#) by Dagne W. 11


Built-in Type in C#..
 Verbatim literal- is a string with an @ symbol prefix, as in @
“Hello”.
Make escape sequence translate as normal printable characters
to enhance readability
Example:
 Without Verbatim literal:
“ C:\\Pragim\\DotNet\\Training\\Csharp”-less readable
 With Verbatim literal:
@“ C:\Pragim\DotNet\Training\Csharp”- Better readable

Event driving programming(C#) by Dagne W. 12


Operators in C#…
 Common Operators in C#

 Outline
 Assignment operator =
 Arithmetic operators like +,-,*,/,%
 Comparison operators like ==, !=,>,>=,<,<=
 Conditional operators like &&, ||
 Ternary operator ?:
 Null Coalescing operator ??

Event driving programming(C#) by Dagne W. 13


Common Operators…

Event driving programming(C#) by Dagne W. 14


Data type conversion in C#…
 Implicit conversion is done by the compiler when there is
no loss of information if the conversion is done
 If there is no possibility of throwing exceptions during
the conversion
 Example: converting an int to a float will not loose any
data and no exception will be thrown, hence an implicit
conversion can be done.
 Explicit conversion: when converting a float to an int, we
loose the fractional part and also a possibility of overflow
exception.
 Hence, in this case an explicit conversion is required.
 For explicit conversion we can use cast operator or the
convert class in C#.

Event driving programming(C#) by Dagne W. 15


Data Type conversion in C#…
 Difference between Parse and TryParse
 If the number is in a string format you have two options-
Parse() and TryParse()
 Parse() method throws an exception if it cannot parse the
value
 TryParse() returns a bool indicating whether it succeeded
or failed. Use Parse() if you are sure the value will be
valid, otherwise use TryParse()

Event driving programming(C#) by Dagne W. 16


Comments in C#
 Single line comments -//
 Multi line comments -/* */
 XML documentation comments -///
 Comments are used to document what the program does
and what specific blocks or lines of code do.
 C# compiler ignores comments.
 To Comment and Uncomment, there are 2 ways
1. Use the designer
2. Keyboard shortcut: Ctrl + K, Ctrl + C and Ctrl+K , Ctrl+U,
Note: Don’t try to comment every line of code. Use comments
only for blocks or lines of code that are difficult to understand

Event driving programming(C#) by Dagne W. 17


C# Statement…
 Statement

• If Statement
• If else statement
• Difference between && and &
• Difference between || and |

Event driving programming(C#) by Dagne W. 18


C# Statements…
 The && and & and ||and |symbols are used to test multiple conditions
in a single if statement.

 The difference is that the double && checks the next condition if the
first condition is true.

 Where as the single & checks all conditions even if the first condition
is false.

 And if it’s double || it checks the next conditions if the previous


conditions is false.

 In single | it checks all conditions at all.

 Therefore I recommend to use double && and || for more efficiency

Event driving programming(C#) by Dagne W. 19


If else and Switch statement
Java If statement
}

Event driving programming(C#) by Dagne W. 20


Switch statement…
break statement: if a break statement is used
inside in a switch statement the control will
leave the switch statement.
goto-statement: you can either jump to other
case statement or to a specific label.
Warning: using goto is bad programming style. We
can should avoid goto by all means.

Event driving programming(C#) by Dagne W. 21


Loop in C#
While loop : checks the condition first and if the
condition is true, statements with in the loop are
executed.
 This condition is repeated as long as the condition
evaluates to true.
Note: do not forget to update the variable
participating in the condition so the loop can end, at
some point.

Event driving programming(C#) by Dagne W. 22


While loop …

Event driving programming(C#) by Dagne W. 23


do while loop
1. a do while loop checks its condition at the end of the loop.
2. This means that the do loop guaranteed to execute at least
one time.

3. do loops are used to present a menu to the user. Example:

Event driving programming(C#) by Dagne W. 24


While ,do while and for loop…
 While loop checks the condition at the beginning, where as
do while loop checks the condition at the end of the loop.
 do loop is guaranteed to execute at least once, where as
while loop is not.
 A for loop is very similar to while loop. In a while we do the
initialization at one place, condition check at another
place, and modifying the variable at another place, where
as for loop has all of these at one place.

Event driving programming(C#) by Dagne W. 25


foreach loop…
A foreach loop is used to iterate through the items in a
collection. For example, foreach loop can be used with
arrays or collections such as ArrayList, HashTable and
Generics.

Event driving programming(C#) by Dagne W. 26


Array in C#…
 An array is a collection of similar data types
 Advantages: Arrays are strongly typed.
 Disadvantages: Arrays cannot grow in size once initialized. Have to rely
on integral indices to store or retrieve items from the array. (Example:
Array out of Index exception).

Event driving programming(C#) by Dagne W. 27


Methods in C#

 Methods are also called as functions. These terms are


used interchangeably
 Methods are extremely use full because they allow you
to define your logic once and use it at many places.

Event driving programming(C#) by Dagne W. 28


Methods in C#...
Methods make the maintenance of your application
easier.
1. Return type can be any valid data type or void
2. Method name can be any meaningful name(except
those reserved words)
3. Parameters are optional

Event driving programming(C#) by Dagne W. 29


Methods in C#…

Event driving programming(C#) by Dagne W. 30


Static Vs Instance methods
 When method declaration includes a static modifier, that
method said to be static method.
 When no static modifier is present, the method is said to
be an instance method.
 Static method is invoked using the class name, where as
instance method is invoked using an instance object of
the class.
 However if a method is static, there are no instance of that
method, and you can invoke only that one definition of
that method.

Event driving programming(C#) by Dagne W. 31


Type of methods parameters
The 4 types of method parameters
 Value parameters
 Reference parameters
 Out parameters
 Parameter arrays
Method parameters vs Method arguments

Event driving programming(C#) by Dagne W. 32


Type of Methods parameters ...

Event driving programming(C#) by Dagne W. 33


Type of method parameters…

Event driving programming(C#) by Dagne W. 34


Type of method parameters…
 To allow a method to return multiple values use the out keyword.
Example: Calculate method returns the value of both Sum and Product
to the

Event driving programming(C#) by Dagne W. 35


Type of methods parameters…
 Parameter array the params keyword lets you to specify a
method parameter that takes a variable number of
arguments

Event driving programming(C#) by Dagne W. 36


Type of method parameters…
 Value parameters: creates a copy of the parameter passed, so
modifications does not affect each other.
 Reference parameters: the ref method parameter keyword on a
method parameter causes a method to refer the same variable that
was passed into the method, so modifications does affect each
other.
 Out parameters: use when a method want to return more than
one value.
 Parameter arrays: the params keyword lets you specify a method
parameter that takes a variable number of arguments.

Event driving programming(C#) by Dagne W. 37


Class in C#...
 A class is consists of data and behavior.
 Class data is represented by it’s fields and behavior is
represented by its methods.
 The purpose of a class constructor is to initialize class
fields.
 A class constructor is automatically called when an
instance of a class is created.
 Constructors don’t have return values and always have the
same name as the class.
 Constructor are not mandatory. If we don’t provide a
constructor, a default parameter less constructor is
automatically provided.
 Constructors canEvent be overloaded
driving programming(C#) byby the
Dagne W. number and type of 38
Destructor
 Destructors have the same name as the class ~symbol in
front of them.
 They don’t take any parameters and don’t return a
value.
 Destructors are places where you could put a code to
release any resources your class was holding during its
life time.
 They are normally called the C# garbage collector decides
to clean your object from memory.

Event driving programming(C#) by Dagne W. 39


Destructor Example …

Event driving programming(C#) by Dagne W. 40


Class Example

Event driving programming(C#) by Dagne W. 41


Static and instance class members
 Static and Instance class members
Static class members
Instance class members
Difference between instance and static
members
An example when you make certain
members make static

Event driving programming(C#) by Dagne W. 42


Static and instance class members

Event driving programming(C#) by Dagne W. 43


.

Thank you !!!

Event driving programming(C#) by Dagne W. 44

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy