You are on page 1of 33

Programming

Fundamentals I
Course Code 109
Sri Lanka Institute of Information Technology
Jagath Wickramarathne
Jagath.w@sliit.lk

Lecture 2 - Learning C#
Basics
Introducing the .NET Platform.

Steps involved in C# program


development.
Computer Programming Languages.
What Is .NET?
Writing a Simple C# Program.
Commenting Code.
Identifiers and Keywords.
Convention and Style.
Getting Started with C# and Visual Studio
Sri Lanka Institute of
2
08 January 2010
Information
technology
2008.

How user interacts with


Computer
App
li

cati
o

n
Soft
w ar
e

http://www.nickhalstead.com/wp-content/computer-user.jpg

Sri Lanka Institute of


Information technology

08 January 2010

Software . . .?

http://patentu.blogspot.com/2007/03/what-is-software.html

Sri Lanka Institute of


Information technology

08 January 2010

Why Computer Programs?

uter program is a sequence of instructions that enable a computer to accomplish a particul

Sri Lanka Institute of


Information technology

08 January 2010

Computer Programming
Languages
A programming language is a machine-readable artificial
language
designed to express computations that can be performed by a
machine, particularly a computer.
Programming languages can be used to create
programs that specify the behavior of a machine,
to express algorithms precisely, or as a mode of human
communication.

http://en.wikipedia.org/wiki/Programming_language

http://www.levenez.com/lang/

http://www.engin.umd.umich.edu/CIS/course.des/cis400/

Sri Lanka Institute of


Information technology

08 January 2010

How you can select a


Computer Programming
Language

How you can select a Computer Prog


ramming Language

Sri Lanka Institute of


Information technology

08 January 2010

What Is .NET?
Microsoft .NET, is a platform for developing
software.
You are building applications on:
Microsoft .NET (pronounced Dot Net).
You learn about the parts of .NET,

including the Common Language Runtime


(CLR),
the .NET Framework Class Library, and how
.NET supports multiple languages.

Sri Lanka Institute of


Information technology

08 January 2010

NET supports multiple programming


languages.

Literally dozens of languages target the .NET CLR as a


platform.

PF I
PF
II

Sri Lanka Institute of


Information technology

ITA

08 January 2010

What Can a C# Program


Do?
Typical programming tasks includes
putting data into a database or
pulling it out, displaying high speed
graphics in a game or video,
controlling electronic devices
attached to the PC or even playing
music and/or sound effects.
You can even write software to
generate music or help you compose.

http://cplus.about.com/od/introductiontoprogramming/a/cshbeginners.htm

Sri Lanka Institute of


Information technology

10

08 January 2010

Is C# The Best Programming


Language?
Some computer languages were
written for a specific purpose.
Java was originally devised to control toasters.
C for Operating Systems Programming.
Pascal to teach good programming techniques.

But C# is a general purpose


language with features to make
programs more robust.
Fully OOP

Automatic garbage collection (dont have to delete objects)


No pointers
Only booleans allowed in if statements
Everything inherits from System.Object

Institute
Sri Lanka
Arrayof bounds
Information technology

checking
at runtime
11

08 January 2010

Which Computers Can Run


C#?
Any that can run the
.NET framework.
On Linux under MonoProject & IDE.

Sri Lanka Institute of


Information technology

12

08 January 2010

How Do I Get Started With


C#?

.NET framework.

First you need a Editor.

Then a C# compiler.

There are a number of commercial and free ones available.


The list below has instructions for downloading and installing two
free compilers.
Both are completely free and include an IDE to make life easier
for you to edit, compile and debug your applications.
Download and Install Borland's Turbo C# Explorer.

Download
Sri Lanka Institute
of
Information technology

and Install Microsoft's Visual C# 2005 Express Edition.


13

08 January 2010

Could I Get A Programming


Job?
There are an increasing number of C# jobs
out there and it has the backing of Microsoft
so is likely to be around for a considerable
length of time.
The top three most popular programming
languages according to the quarterly
Tiobe.com survey, are Java, C and C#.
Eg. Search Google

most popular programming languages


C# + Salary

Sri Lanka Institute of


Information technology

14

08 January 2010

How to write Computer


Programs
There are four general phases
Specify the problem,
Analyze and break down into a
series of steps towards solution,
(i.e., design an algorithm),
Write the code,
Compile and run (i.e., test the
Sri Lanka Institute of
15
08 January 2010
Informationprogram)
technology

Steps involved in C# program


development

Sri Lanka Institute of


Information technology

16

08 January 2010

C# Compilation Process

http://www.mastercsharp.com/article.aspx?ArticleID=90&&TopicID=4

Sri Lanka Institute of


Information technology

17

08 January 2010

Where Do I Go Now?
First you have to learn to program in
C#.
Text Book
MSDN
Internet
E-books
Join The Forums
http://dotnetforum.lk/

Etc.
Sri Lanka Institute of
Information technology

18

08 January 2010

Writing a Simple C#
Program
Editor / IDE

Project Type

Notepad

Console Application

Visual Studio 2008

Windows Forms
Application

08 January 2010

Sri Lanka Institute of Information


technology

19

C# - Console Applications
using Visual Studio 2008 Command Prompt
Step I
Open a notepad.

Step II
Write the following code and save as FirstProgram.cs.

Step III
Open Visual Studio 2008 Command Prompt.
Change the current directory to the place where you
save FirstProgram.cs.

Step IV
Type csc FirstProgram1.cs compile command.

Step IV
Run FirstProgram1.exe
Sri Lanka Institute of
Information technology

20

08 January 2010

Sri Lanka Institute of


Information technology

21

08 January 2010

Demo One
How to create a C# console
application using Visual Studio 2008
Command Prompt.

Sri Lanka Institute of


Information technology

22

08 January 2010

C# - Windows Applications
using Visual Studio 2008
Step I
Run Microsoft Visual studio 2008.
Step II
Create new project Windows Forms
Application
Step III
Design Interface / Form
Step IV
Compile
Step V
Run
Sri Lanka Institute of
Information technology

23

08 January 2010

Demo Two
How to create a C# Windows Forms
application using Visual Studio 2008.
Solution
Project
.EXE

Sri Lanka Institute of


Information technology

24

08 January 2010

Demo Three

Navigating through Design and Code


modes.

Different windows / Features in


VS2008 IDE.

Sri Lanka Institute of


Information technology

25

08 January 2010

C# - Convention and Style

It is common practice in C# to use whitespace and indentation to


facilitate easier reading of code.

The names should describe what the program does.

A program may have any amount of white-space between language


elements.

Identifiers are structured, using either Pascal casing or camel


casing.

In Pascal casing the first letter of each word in a name is capitalized


HelloWorld, DotProduct, and AmortizationSchedule.
This is normally used in all instances except for parameters (passed to
methods), private fields (class member variables), and local variables
(method variables).

With camel casing, the first letter of the first word is lowercase, and
subsequent words are capitalized.
bookTitle, employeeName, and totalCompensation.
Parameters, private fields, and local variables use camel casing.

Sri Lanka Institute of


Information technology

26

08 January 2010

Commenting Code

Part of writing good code is ensuring it is properly documented.

These commenting techniques prove useful any time you want to comment
out a block of incomplete code to get a good compile or any other time you
want to leave a block of code in place temporarily.

The compiler ignores comments when reading the source code.

There are three types of commenting syntax in C#:


single line
multiline
XML

The multiline and single-line comments are great for helping yourself or other
programmers understand your code.

Sri Lanka
XMLInstitute
documentation
commenting feature that is good for both reading code
of
08 January 2010
and providing
external 27
documentation.
Information
technology

Single-Line Comments

They begin with the double forward slash marker,

//The single-line comment

Multiline Comments

Multiline comment delimiters are the begin comment /* and end comment */ markers.
Anything between these two markers is considered a comment.

/*
* File Name: Program.cs
* Author: Nishani Ranpatabendi
*/

XML Documentation Comments

XML documentation comment supports tools that extract comments into an external XML document.
This XML can be consumed by tools or run through XSLT style sheets to produce readable documentation.
XML documentation comments start with a triple slash, ///, on each line.

///
///
///
///

<summary>
first method executed in application
</summary>
<param name=args>command-line options</param>

static void Main(string[] args)


{
Sri Lanka Institute
//of other code
28
Information technology
}

08 January 2010

Identifiers and Keywords


Identifiers and keywords are important because
you need to know how to name your variables,
custom types, methods, and so on.
Identifiers are names of the types and variables in
your program.
Keywords are reserved words in the C# language.
The difference between identifiers and keywords
is that keywords are reserved for C# language
syntax and cant be used for naming your
variables, types, and so on.
Sri Lanka Institute of
Information technology

29

08 January 2010

Reserved words in the C#


language

You can prefix keywords with the @ character and use it as an


identifier.
Sri Lanka Institute of
Information technology

30

08 January 2010

Case Sensitive Languages


C# programming language is case sensitive.
If the language considers two identifiers to be
different if they consist of the same characters in
the same order except for variations in upper
and lower case.

Name name NAME would be three


different identifiers in a language which is
case sensitive.
C# is case sensitive.
Sri Lanka Institute of
Information technology

31

08 January 2010

Summery
Introducing the .NET Platform.
Steps involved in C# program
development.
Computer Programming Languages.
What Is .NET?
Writing a Simple C# Program.
Commenting Code.
Identifiers and Keywords.
Convention and Style.
Getting Started with C# and Visual Studio
2008.
Sri Lanka Institute of
Information technology

32

08 January 2010

Reference
http://msdn.microsoft.com/en-us/vcsharp/bb798022.aspx

33

You might also like