C# Fundamental

33
1 Programming Fundamentals I Course Code 109 Sri Lanka Institute of Information Technology Jagath Wickramarathne [email protected]

Transcript of C# Fundamental

Page 1: C# Fundamental

1

Programming

Fundamentals I Course Code 109

Sri Lanka Institute of Information Technology

Jagath [email protected]

Page 2: C# Fundamental

Sri Lanka Institute of Information technology

208 January 2010

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 2008.

Page 3: C# Fundamental

Sri Lanka Institute of Information technology

308 January 2010

How user interacts with Computer

Application Software

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

Page 4: C# Fundamental

Sri Lanka Institute of Information technology

408 January 2010

Software . . .?

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

Page 5: C# Fundamental

Sri Lanka Institute of Information technology

508 January 2010

Why Computer Programs?A computer program is a sequence of instructions that enable a computer to accomplish a particular task.

Page 6: C# Fundamental

Sri Lanka Institute of Information technology

608 January 2010

Computer Programming LanguagesA 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/

Page 7: C# Fundamental

Sri Lanka Institute of Information technology

708 January 2010

How you can select a Computer Programming Language

How you can select a Computer Programming Language

Page 8: C# Fundamental

Sri Lanka Institute of Information technology

808 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.

Page 9: C# Fundamental

Sri Lanka Institute of Information technology

908 January 2010

NET supports multiple programming languages.

• Literally dozens of languages target the .NET CLR as a platform.

PF IPF II ITA

Page 10: C# Fundamental

Sri Lanka Institute of Information technology

1008 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

Page 11: C# Fundamental

Sri Lanka Institute of Information technology

1108 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 (don’t have to delete objects)– No pointers– Only booleans allowed in if statements– Everything inherits from System.Object– Array bounds checking at runtime

Page 12: C# Fundamental

Sri Lanka Institute of Information technology

1208 January 2010

Which Computers Can Run C#?

Any that can run the • .NET framework.

• On Linux under MonoProject & IDE.

Page 13: C# Fundamental

Sri Lanka Institute of Information technology

1308 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 and Install Microsoft's Visual C# 2005 Express Edition.

Page 14: C# Fundamental

Sri Lanka Institute of Information technology

1408 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”

Page 15: C# Fundamental

Sri Lanka Institute of Information technology

1508 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 program)

Page 16: C# Fundamental

Sri Lanka Institute of Information technology

1608 January 2010

Steps involved in C# program development

Page 17: C# Fundamental

Sri Lanka Institute of Information technology

1708 January 2010

C# Compilation Process

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

Page 18: C# Fundamental

Sri Lanka Institute of Information technology

1808 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.

Page 19: C# Fundamental

Sri Lanka Institute of Information technology

1908 January 2010

Writing a Simple C# Program

Editor / IDE Project Type

Notepad Console Application

Visual Studio 2008 Windows Forms Application

Page 20: C# Fundamental

Sri Lanka Institute of Information technology

2008 January 2010

C# - Console Applicationsusing 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

Page 21: C# Fundamental

Sri Lanka Institute of Information technology

2108 January 2010

Page 22: C# Fundamental

Sri Lanka Institute of Information technology

2208 January 2010

Demo One

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

Page 23: C# Fundamental

Sri Lanka Institute of Information technology

2308 January 2010

•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

C# - Windows Applicationsusing Visual Studio 2008

Page 24: C# Fundamental

Sri Lanka Institute of Information technology

2408 January 2010

Demo Two

• How to create a C# Windows Forms application using Visual Studio 2008.– Solution

• Project– .EXE

Page 25: C# Fundamental

Sri Lanka Institute of Information technology

2508 January 2010

Demo Three

Navigating through Design and Code modes.

Different windows / Features in VS2008 IDE.

Page 26: C# Fundamental

Sri Lanka Institute of Information technology

2608 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.

Page 27: C# Fundamental

Sri Lanka Institute of Information technology

2708 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.

• XML documentation commenting feature that is good for both reading code and providing external documentation.

Page 28: C# Fundamental

Sri Lanka Institute of Information technology

2808 January 2010

• 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){

// other code}

Page 29: C# Fundamental

Sri Lanka Institute of Information technology

2908 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 can’t be used for naming your variables, types, and so on.

Page 30: C# Fundamental

Sri Lanka Institute of Information technology

3008 January 2010

Reserved words in the C# language

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

Page 31: C# Fundamental

Sri Lanka Institute of Information technology

3108 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.

Page 32: C# Fundamental

Sri Lanka Institute of Information technology

3208 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.

Page 33: C# Fundamental

33

Reference

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