Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments...

53
Creating and Running Your First C# Program

Transcript of Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments...

Page 1: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Creating and Running Your First C# Program

Page 2: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

: http://eembdersler.wordpress.com ◦ Choose the EEE-425Programming Languages (Fall)

Textbook reading schedule

Pdf lecture notes

Updated class syllabus

Midterm and final exams grades

Answers of Midterm and Final Exam

2

Page 3: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Texts and Other Course Materials ◦ Programming C#, 4th Edition - Jesse Liberty (You may down this

version the book from Internet - It is your responsibility to download it or buy the book from http://www.amazon.com/Programming-C-Jesse-Liberty/dp/B00006AVR5)

◦ Beginning.C.Sharp.3.0.An.Introduction ◦ Internet Materials

Grading : ◦ Assignments 20% ◦ Midterm 15% ◦ Final : 25% ◦ Project : 40% ( Presentation, code and short report) ◦ Bonus : 10% ( If you follow all course –no exception, you will get ◦ extra 10% of your grade)

Total 110%

3

Page 4: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

This course contains material that

demands intense mental work and an

unusual amount of time commitment.

Student dedication is advised.

4

Page 5: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

5

Instructor’s expectations: ◦ Homework. Read chapters prior to class from the textbook ◦ Expect 2-4 hours outside classroom activities; Reading ◦ Participate. Ask questions. Contribute. Actively participate

in the exercises. Consider the classroom to be a mental gymnasium where it’s ok to run, fall, and get up again

◦ Take notes. Why let any idea get away? Taking notes will help you concentrate and organize your

thoughts Notes allow you to take a refresher any time in the future.

◦ Enjoy yourself. Start relaxed and you’ll leave refreshed, inspired, and recharged Forget about what’s happening at home or at work This is your time. Get all you can out of this course and have a

good time ◦ Excessive Late assignments: 10% penalty from the grade

assigned for each day ( maximum 2 days)

Page 6: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

6

Refer to the syllabus for details regarding ◦ Academic dishonesty

NOCHEATHING is allowed for all materials.

Given and taken person will be punished with less notes( 0)

◦ Attendance : 70% of Lectures

◦ Special attention for the class

Page 7: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Covers (most of) the C# language and some of the most useful .NET API’s.

Should not be your first programming class. ◦ Assume you know C++ and basic object-oriented

programming.

Requires (lots of) practice / reading. ◦ C# and .NET cannot be learned thoroughly in this

brief course.

7

Page 8: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Require ◦ do all assignments

Assignments are ◦ You will not be given a detailed grade

◦ Show me that you understand the concepts, and can write C# code

Do a project that related with technical EEE by using C# ( It is important to apply your knowledge to one problem)

Note : Pls dont just directly copy the code from Internet or somewhere as your project- If we know that is copied somewhere we will penalty its grade as 0 (zero)

8

Page 9: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Missing class sessions Failure to do homework Poor sleeping, drinking, eating, and physical exercise habits

Failure to follow textbook Failure to prioritize and budget time for study, work, and social activities

Copying other people’s homework, quiz, or exam (Read “Academic Integrity”)

9

Page 10: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

To do well in this class, you have to

work really hard.

You need to be prepared to dedicate a

significant amount of time and

mental effort on this class.

10

Page 11: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

can determine your grade in this class

can make the choice to be successful

can choose how you use your time

can do the work of learning

11

Page 12: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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?

12

Page 13: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

13

Page 14: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

14

Definition by Google

Page 15: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

15

= Specification

= Design

= Implementation

=Testing & Debugging

= Deployment

Page 16: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Wikipedia.org definition. ◦ Object-oriented.

◦ Primarily imperative or procedural.

LINQ (Language Integrated Query) adds some functional programming language capabilities.

◦ Structured (as opposed to monolithic).

◦ Strongly typed.

◦ ISO(International Organization for Standardization) and ECMA(European Computer Manufacturers Association) standardized.

16

Page 17: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

17

Page 18: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Sample C# program:

using System; namespace ConsoleApplication; class HelloCSharp { static void Main(string[] args) { Console.WriteLine("Hello, C#"); } }

18

Page 19: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

19

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 20: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

20

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 HelloCsharp

and start with a CAPITAL letter.

Page 21: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

21

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

Such formatting

makes the source code

unreadable.

Page 22: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

using

◦ like import in Java: bring in namespaces

namespace

◦ disambiguation of names ◦ like Internet hierarchical names and C++ naming

class

◦ like in C++ or Java ◦ single inheritance up to object

Page 23: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

static void Main() ◦ Defines the entry point for an assembly. ◦ Four different overloads – taking string

arguments and returning int’s. Console.WriteLine(Write) ◦ Takes a formatted string: “Composite

Format” ◦ Indexed elements: e.g., {0} can be used multiple times only evaluated once

◦ {index [,alignment][:formatting]}

Page 24: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

24

Page 25: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Live Demo-Example 01-01, 01-02

25

Page 26: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

26

Page 27: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

27

Page 28: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,
Page 29: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

29

FCL

CLR

Page 30: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

The common classes that are used in many programs ◦ System.Console.WriteLine ◦ XML, Networking, Filesystem, Crypto, containers ◦ Can inherit from many of these classes

Many languages run on .NET framework ◦ C#, C++, J#, Visual Basic ◦ even have Python (see IronPython)

Page 32: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

32

CLR

Page 33: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Class Loader

MSIL to Native Compilers (JIT)

Code Manager

Garbage Collector (GC)

Security Engine Debug Engine

Type Checker Exception Manager

Thread Support COM Marshaler

Base Class Library Support

From MSDN

Page 34: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

CLR via C#, Jeffrey Richter

Page 35: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

35

CLR is actually an implementation by Microsoft of the CLI (Common Language Infrastructure) .

CLI is an open specification.

CLR is really a platform specific implementation.

Page 36: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Framework Class Library (FCL)

◦ Provides basic functionality to developers:

Console applications

WPF(Windows Presentation Framework) 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

36

Page 37: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

37

Page 38: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

38

Page 39: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

39

Page 40: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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!

40

Page 41: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

41

Page 42: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Compiling, Running and Debugging C# Programs

42

Page 43: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

43

Page 44: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

4. Visual Studio creates some source code for you

44

Namespace

not

required

Class name

should be changed

Some imports

are not

required

Page 45: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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]

45

Page 46: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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!

46

Page 47: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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

47

Page 48: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Visual Studio has built-in debugger

It provides: ◦ Breakpoints

◦ Ability to trace the code execution

◦ Ability to inspect variables at runtime

48

Page 49: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

49

Page 50: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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/

50

Page 51: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

Questions?

51

Page 52: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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.

52

Page 53: Creating and Running Your First C# Program · 2010-10-01 · Require do all assignments Assignments are You will not be given a detailed grade Show me that you understand the concepts,

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.

53