creating and running your first c# program · 2010-10-01 · environment for execution of .net...

34
Creating and Running Your First C# Program

Upload: others

Post on 13-Mar-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Creating and Running Your First C# Program

Page 2: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Text Book : C# Programming From Problem Analysis to Program design, Barbara Doyle

Grading :

Homeworks 20% ◦ Lecture Presentation 20%

◦ Final : % 20

◦ Project : 40%

◦ Bonus : 10% for full attends in the class

2

Page 3: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

1. What is Computer Programming?

2. Your First C# Program

3. What is .NET Framework?

4. What is Visual Studio?

5. What is MSDN Library?

3

Page 4: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project
Page 5: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Computer programming: creating a sequence of instructions to enable the computer to do something

5

Definition by Google

Page 6: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Define a task/problem

Plan your solution ◦ Find suitable algorithm to solve it

◦ Find suitable data structures to use

Write code

Fix program error (bugs)

Make your customer happy

6

= Specification

= Design

= Implementation

= Testing & Debugging

= Deployment

Page 7: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project
Page 8: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Sample C# program:

using System; class HelloCSharp { static void Main() { Console.WriteLine("Hello, C#"); } }

8

Page 9: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

9

using System; class HelloCSharp { static void Main() { Console.WriteLine("Hello, C#"); } }

Include the standard

namespace "System"

Define a class called

"HelloCSharp"

Define the Main() method – the program entry

point

Print a text on the console by calling the method

"WriteLine" of the class "Console"

Page 10: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

10

using System; class HelloCSharp { static void Main() { Console.WriteLine("Hello, C#"); } }

The { symbol should be alone on

a new line.

The block after the { symbol

should be indented by a

TAB.

The } symbol should be under

the corresponding {.

Class names should use PascalCase and start with a

CAPITAL letter.

Page 11: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

11

using System ; class HelloCSharp { static void Main( ) { Console . WriteLine ("Hello, C#" ) ;Console. WriteLine ( "Hello again" ) ;}}

Such formatting makes the

source code unreadable.

Page 12: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Programming language

◦ A syntax that allow to give instructions to the computer

C# features:

◦ New cutting edge language

◦ Extremely powerful

◦ Easy to learn

◦ Easy to read and understand

◦ Object-oriented

12

Page 13: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Live Demo-Example 01-01, 01-02

Page 14: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project
Page 15: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Environment for execution of .NET programs

Powerful library of classes

Programming model

Common execution engine for many programming languages ◦ C#

◦ Visual Basic .NET

◦ Managed C++

◦ ... and many others

15

Page 16: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Operating System (OS)

Common Language Runtime (CLR)

Base Class Library (BCL)

ADO.NET, LINQ and XML (Data Tier)

WCF and WWF (Communication and Workflow Tier)

ASP.NET Web Forms, MVC, AJAX Mobile Internet Toolkit

Windows Forms

WPF Silverlight

C# C++ VB.NET J# F# JScript Perl Delphi …

Building blocks of .NET Framework

16

FCL

CLR

Page 17: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Common Language Runtime (CLR) ◦ Managed execution environment

Executes .NET applications

Controls the execution process

◦ Automatic memory management (garbage collection)

◦ Programming languages integration

◦ Multiple versions support for assemblies

◦ Integrated type safety and security

17

CLR

Page 18: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Framework Class Library (FCL)

◦ Provides basic functionality to developers:

Console applications

WPF and Silverlight rich-media applications

Windows Forms GUI applications

Web applications (dynamic Web sites)

Web services, communication and workflow

Server & desktop applications

Applications for mobile devices

18

Page 19: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project
Page 20: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Visual Studio – Integrated Development Environment (IDE)

Development tool that helps us to: ◦ Write code

◦ Design user interface

◦ Compile code

◦ Execute / test / debug applications

◦ Browse the help

◦ Manage project's files

20

Page 21: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Single tool for:

◦ Writing code in many languages (C#, VB, …)

◦ Using different technologies (Web, WPF, …)

◦ For different platforms (.NET CF, Silverlight, …)

Full integration of most development activities (coding, compiling, testing, debugging, deployment, version control, ...)

Very easy to use!

21

Page 22: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

22

Page 23: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Compiling, Running and Debugging C# Programs

Page 24: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

1. File New Project ... 2. Choose C# console application 3. Choose project directory and name

24

Page 25: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

4. Visual Studio creates some source code for you

25

Namespace

not

required

Class name

should be changed

Some imports

are not

required

Page 26: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

The process of compiling includes: ◦ Syntactic checks

◦ Type safety checks

◦ Translation of the source code to lower level language (MSIL)

◦ Creating of executable files (assemblies)

You can start compilation by ◦ Using Build->Build Solution/Project

◦ Pressing [F6] or [Shift+Ctrl+B]

26

Page 27: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

The process of running application includes:

◦ Compiling (if project not compiled)

◦ Starting the application

You can run application by:

◦ Using Debug->Start menu

◦ By pressing [F5] or [Ctrl+F5]

* NOTE: Not all types of projects are able to be started!

27

Page 28: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

The process of debugging application includes: ◦ Spotting an error

◦ Finding the lines of code that cause the error

◦ Fixing the code

◦ Testing to check if the error is gone and no errors are introduced

Iterative and continuous process

28

Page 29: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Visual Studio has built-in debugger

It provides: ◦ Breakpoints

◦ Ability to trace the code execution

◦ Ability to inspect variables at runtime

29

Page 30: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project
Page 31: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Complete documentation of all classes and their functionality ◦ With descriptions of all methods, properties, events,

etc.

◦ With code examples

Related articles

Library of samples

Use local copy or the Web version at http://msdn.microsoft.com/

31

Page 32: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

Questions?

Page 33: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

1. Create, compile and run a “Hello C#”

console application.

2. Modify the application to print your name.

3. Write a program to print the numbers 1,

101 and 1001.

4. Create console application that prints your

first and last name.

33

Page 34: Creating and Running Your First C# Program · 2010-10-01 · Environment for execution of .NET programs ... Compiling, Running and Debugging C# Programs . 1. File New Project

6. Create a console application that prints the current date and time.

7. Create a console application that calculates and prints the square of the number 12345.

8. Write a program that prints the first 10 members of the sequence: 2, -3, 4, -5, 6, -7, ...

9. Write a program to read your age from the console and print how old you will be after 10 years.

34