Anda di halaman 1dari 88

IT111P

Ms. Sahcheeh
INTRODUCTION TO
C#
PROGRAMMING

TOPICS:
O Types of applications developed with C#
O Exploring the first C# program
O Elements of a C# program
O Writing statements inside the Method Body
O Writing your first C# program
Introduction:
Differentiate C++ from C#
Distinguish the different types of
applications that can be created with C#
programming language.
Identify and examine the basic elements of
a C# program.
Interpret a program written in C#
programming language
Differentiate C++ from C#
O C has been around since the early 70's.
(Kerringhan and Ritchie)
O C++ was designed to fill the need for code-
reusability, necessary for large scale software
development. It introduced Classes and Object
Oriented Programming. It can do anything C can
do.So essentially, C is small and fast, and C++ is
modular and scalable. Also, it is worth
mentioning that C++ is still evolving. (Stroustrup)

There were many IDE for both languages, most famous at the
time were from Borland, named as Turbo C and Turbo C++.
Differentiate C++ from C#
O C# is another adventure of Microsoft which uses .NET
framework. This is a new language which appears to me more of
a newer version of JAVA (both languages use a similar syntax and
program architecture) as it incorporates more feature of a
secureJAVA then the loose barrelled C.
IDE for C# is called Visual C# carrying on the nomenclature of
Visualfor Microsoft.

Later, with the advent of Windows based Operating System and introduction
of Microsoft Foundation Classes, Microsoft introduces its own
versions of IDE for C and C++. Microsoft named them Visual Cand
Visual C++. Note that these two are not separate languages, but just new
IDEs, though there were certain syntax restrictions put in by Microsoft, which
makes them a little different from their ANSI counterparts
THE TRANSITION
C#
C#
O Pronounced c sharp
O A programming language that can be used
to give instructions to a computer. The
instructions can be written from a text editor
such as Notepad.
O Another way is to use a programming
environment that is equipped with many
tools that make it easy to work on projects,
to create the necessary files, and to
distribute a completed application.
C#
C# is a universal language that is used on
many operating systems, including Microsoft
Windows. C# is one of the languages used in
the Microsoft .NET Framework.
The Microsoft .NET Framework is a library of
objects that create or draw things on the
computer
C#
OOP / Object Oriented Programming The concept
behind OOP is that applications can be organized
around objects rather than processes.

(Visual Studio).NET Framework - an environment in
which programs run and was designed to be a new
programming paradigm. It provides a platform for
developing and running a code that is easy to use.

The .NET Framework can be a platform for a lot of
programming languages like Visual Basic, C++, and
J#; however, C# IS THE LANGUAGE CREATED FOR
.NET
APPLICATIONS
DEVELOPED
WITH C#
APPLICATIONS DEVELOPED
WITH C#
To launch Microsoft Visual Studio 2010, you can click Start ->
(All) Programs -> Microsoft Visual Studio 2010.
Starting Microsoft Visual C#
To launch Microsoft Visual Studio 2010, you can click Start ->
(All) Programs -> Microsoft Visual Studio 2010.
Starting Microsoft Visual C#
Introduction to C# Projects
O A computer project is a group of files and
objects that contain the necessary
instructions for the intended application to
produced the desired results.
O C# project is made of one or more files that
contain the instructions of the project. A
simple C# project can contain one file that
holds all the necessary instructions.
C# could be:
OConsole Application
OWindows Appilication
Console Applications
O The C# language is used to create
applications that display on a black window
referred to as the DOS prompt or DOS
window.
Application: Starting a Project
1. Start Microsoft Visual C# 2010 Express
or Microsoft Visual Studio 2010
2. To create a new application, on the main
menu, click File -> New Project...
3. In the middle list, click Console
Application

BASIC ELEMENTS OF A C# PROGRAM
Using
Directives
comments
Namespace
Class
Main
Method
Method Body -
Statements

1. COMMENTS
Writing a comment is like making notes for
yourself or for readers of your program.
Not considered as instructions
Have no effect on the running of the
program
2 MAJOR FUNCTIONS
1. Makes code more readable
2. Internally document what program
statements are doing
using Directives and
Namespaces
2. USING DIRECTIVES
This section lists the
namespaces that the
application will be using
frequently,
It saves the programmer
from specifying a fully
qualified name every time
that a method that is
contained within is used.
3. NAMESPACES
Namespace is a grouping of related types
contained in an assembly.
For example, the System.Drawing
namespace consists of classes, methods
that are grouped together to achieve similar
tasks.
You can define your own namespace and
indicate that these are names associated
with your particular application.

using Directives and Namespaces
O A namespace is a way of grouping classes and
structs together in a way that limits their scope and
avoids name conflicts with other classes and structs.
O When you create a program in Visual C# Express, a
namespace is automatically created for you. To use
classes from other namespaces in your program, you
must specify them with a using Directive. The most
commonly used.NET Framework namespaces are
listed by default when you create a new application.
If you use classes from other namespaces in the
class library, you must add a using directive for that
namespace to the source file
O When the Code Editor detects that you have declared
a class or struct that it cannot find in the
namespaces listed in the current using directives, it
will suggest namespaces that contain the class or
struct.

4. Class Definition
Everything in C# is designed around a class.
A class is the building block of an object
oriented program.
C# doesnt allow anything to be defined outside
a class.
Every program must have at least one class.
There are predefined classes in the .NET
Framework, example is the Drawing class which
is an abstract class that describes a 2-D
drawing.
This particular class contains its own set of
methods and properties.
Classes
O The C# language uses classes to package
code: all executable C# code must be
contained in a class.
5. MAIN() METHOD
It is the entry point for all applications.
This is where the program begins execution.
Can be placed anywhere inside a class
definition.

A method is a collection of one or more
statements combined to perform an action.
AKA: functions, subroutines, etc.


Main()
O The C# program must contain a Main
method, in which control starts and ends.
The Main method is where you create
objects and execute other methods. The
Main method is a static method that resides
inside a class or a struct. In the "Hello
World!" example, it resides inside the
Program class.


6. METHOD Body
STATEMENTS
Everything inside your method defines it.
The Console class contains the standard
input and output methods for console
applications:
O Console.Write()
O Console.WriteLine()
O Console.Read()
O Console.Readline()
ESCAPE SEQUENCES
Backslash (\) is called the escape character.
Combine it with one or more escape character
and it becomes a escape sequence.


Escape Sequence Character Description
\n Next Line (Enter Key)
\t Tab
\ Displays a double quote
\ Displays a single quote
\\ Displays a backslash
\r Current Lines Beginning
\a Short Beep
*SIMPLE OOP SIMULATION
O Namespace: A category or brand of cars. Note that the
brand really doesn't have to dictate how the car is built.
You can't say a Honda always have four doors, or that it
always has 4wd. Such specifics is up to the class to
dictate.
O Class: Blueprint for how to build a specific car.
O Object: An actual car (instance) created from the car
blueprint (the class)
O Method: Something a user of the car can make it do.
Start(), IncreaseThrottle(), Break(), OpenDoor() etc.
O Property: Attributes, information and building blocks
which the car contains. E.g. Total running miles, color,
steering wheel dimension, stereo system etc etc.

Console.Write()
Console.WriteLine()
Write text to the console window, also
called the command line window
CONSOLE INPUT/OUTPUT
Find out the difference between
Console.Write and Console.WriteLine
O The Console.Write method requires the
System namespace to be included at the top
to get the fully qualified name.
O The Write method does not append a
newline to the end of the Console. If you are
printing a string that already has a newline,
you can use Console.Write for an entire line.
Also, when you call WriteLine with no
parameters, it writes a newline only.
Output :
One Two Three
Four
O C# console programs generally use the
input/output services provided by .NET Framework
Console class. The statement
Console.WriteLine("Hello, World!"); uses the
WriteLine method. It displays its string parameter
on the command-line window followed by a new
line.
O Other Console methods are used for different
input and output operations. The Console class is
a member of the System namespace. If the using
System; statement was not included at the
beginning of the program, you would have to
specify the System classes like this:

If you need to display several items,
use {0} to represent the first item, {1}
the second item, and so on, like this:
Note for VS.NET Users: The screen will
run and close quickly when launching
this program from Visual Studio .NET. To
prevent this, add the following code as
the last line in the Main method:
// keep screen from going away
// when run from VS.NET
Console.ReadLine();
DATA TYPES
and
EXPRESSIONS
IDENTIFIERS
Names of elements that appear in a
program, such as data items.
Some are predefined, some user-defined.
Some predefined identifiers include:
System, Console, Main, Writeline
There are several rules for creating user-
defined identifiers.
RULES FOR USER-DEFINED
IDENTIFIERS
1. Keywords are not allowed.

To start, there are words, called keywords,
that you must not use because the language
itself uses them. These keywords are:
There are other names that are not considered
Keywords but should be avoided because they may
cause a conflict in your code. They are referred to as
contextual keywords and they are:

RULES FOR USER-DEFINED
IDENTIFIERS
2. Name can consist of one letter (A, B, C, D,
E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U,
V, W, X, Y, Z, a, b, c, d, e, f, g, h, i, j, k, l, m,
n, o, p, q, r, s, t, u, v, w, x, y, or z).
3. The C# language allows that a name be
made of an underscore only _. Although
this is allowed, you should refrain from
using the underscore as the only character
of a name. It makes the code difficult to
read

RULES FOR USER-DEFINED IDENTIFIERS
4. Except for the underscore, special symbols are
not used anywhere in a name. Therefore, don't
use |, !, ", /, $, %, ?, &, *, (, ), +, #, \, @, <, >, [, ], ;,
{, }, or the comma inside any name
5. If the name is made of more than one character, it
must start with either a letter or an underscore
6. After the first letter or underscore, the name can
have a combination of letters, digits (0, 1, 2, 3, 4,
5, 6, 7, 8, or 9), and/or underscores
7. The name cannot have a space
8. C# is case-sensitive. This means that the names
Case, case, and CASE are completely different.
For example, main is always written Main.
9. Be descriptive.

Besides these rules, you can also create
your own rules but that follow the
above restrictions. For example:
O It the name of a variable is in one word,
many people use lowercase letters
O If the name is a combination of words, many
people use the camel notation in which the
first word is in lowercase and the first letter
of each subsequent word is in uppercase

VARIABLES
To manipulate data, characters and numbers
must be stored in RAM.
Variables are the programming elements that
facilitate this storage.
A variable represents an area in the computer
memory where a value of a particular data type
can be stored.

SYNTAX:
type identifer;
LITERAL VALUES
Literals are the numbers, characters, and
combination of characters used in a
program.
Their values stay constant.
These are the values we assign to variables.
VALUE TYPES
The value types are often called fundamental or
primitive data types of the language.
They are the common types needed by most
applications.

Integral Data Types
Floating Point Types
Decimal Type
Boolean Type

DECIMAL TYPE
New to the modern programming
languages.
Appropriate for storing monetary data items
because it allows both integral and
fractional portion.
Provides greater precision.

decimal is the keyword used.
BOOLEAN
Basically true or false, on/off logic
It only accepts true or false, not 1 or 0

bool is the keyword used.
DECLARING STRINGS
string is a build in reference type in C# (The
other one is object)
The string type represents a string of
Unicode characters.

The keyword string is used to declare
strings.
MAKING DATA CONSTANT
Adding the keyword const to a declaration
makes it constant.
const forces the functionality of not allowing
the value to be changed.
ASSIGNMENT STATEMENTS
The following can be assigned to a variable
using the assignment operator =:
1. Another variable
2. Compatible literal value
3. Mathematical Equation
4. Call to a method that returns a compatible
value
Basic Arithmetic operators
+ (Addition)
- (Subtraction)
* (Multiplication)
/ (Division)
% (Modulus Division)
DECLARING
VARIABLES
VAR
O To declare a variable that can hold large
values, you can use the var keyword and
initialize the variable with the desired value.
Here is an example:


Signed Integers
O To use a variable that would hold quite large
numbers, besides the var keyword, you can
declare it using the int keyword. A variable
declared as int can store values between
2,147,483,648 and 2,147,484,647
negative or positive, that can fit in 32 bits.


If you declare an integer variable using the var
keyword and initialize it with a value lower than
2,147,484,647, the compiler concludes that the
memory needed to store that variable is 32 bits:

When initializing an integral variable, instead of a
decimal number, you can also initialize it with a
hexadecimal value whose decimal equivalent is less
than 2,147,484,647. Here is an example:


Unsigned Integers
O If the variable must hold only positive
natural numbers, you can declared it using
the uint keyword, which represents
an unsigned integer. The uint keyword is
used to identify a 32-bit positive integer
whose value would range from 0 to
4,294,967,295. Here are examples:


System.Console.ReadKey();
O Obtains the next character or function key
pressed by the user. The pressed key is
displayed in the console window.

ADD two numbers

Operators and
Operands
Curly Brackets { }
O Curly brackets are probably the most used and
the most tolerant operators of C#.
Fundamentally, curly brackets are used to create
a section of code. As such they are required to
delimit the bodies of namespaces, classes,
structures, exceptions, etc. They are also
optionally used in conditional statements. Curly
brackets are also used to create variable scope.

Parentheses ( )
O Parentheses are used to isolate a group of
items that must be considered as belonging
to one entity. For example, parentheses are
used to differentiate a method such as Main
from a regular variable.
Semi-Colon ;
O The semi-colon is used to indicate the end of
an expression or a declaration.
Comma ,
O The comma is used to separate variables
used in a group. For example, a comma can
be used to delimit the names of variables
that are declared with the same data type.
Assignment =
O When you declare a variable, a memory space is
reserved for it. That memory space may be
empty until you fill it with a value. To "put" a
value in the memory space allocated to a
variable, you can use the assignment operator
represented as =. Based on this, the assignment
operation gives a value to a variable.
Single-Quote '
O The single quote is used to include one character to
initialize, or assign a symbol to, a variable declared
as char. A single-quote is usually combined with
another and a character is included between them.
O You can include only one character between single-
quotes except if the combination of symbols can be
evaluated to one character. This is the case for
escape sequences.
Square Brackets [ ]
O Square brackets are mostly used to control
the dimension or index of an array.
Double-Quotes "
O The double-quote " is used to delimit a
string. Like the single-quote, the double-
quote is usually combined with another.
Between the combination of double-quotes,
you can include an empty space, a
character, a word, or a group of words,
making it a string.
End

Anda mungkin juga menyukai