71
C# 101: Intro to C# Programming

C# 101: Intro to Programming with C#

Embed Size (px)

Citation preview

Page 1: C# 101: Intro to Programming with C#

C# 101: Intro to C# Programming

Page 2: C# 101: Intro to Programming with C#

Introduction

• Your Name• Your day job• Your last holiday destination?

Page 3: C# 101: Intro to Programming with C#

C# 101

• C# Fundamentals– Setting up your development environment– Language Overview– How C# Works– Writing your first program– Built-in Data Types– Conditionals and Loops

Page 4: C# 101: Intro to Programming with C#

C# 102

• Object-oriented Programming– Classes and Objects– Polymorphism, Inheritance and Encapsulation– Functions and Libraries

Page 5: C# 101: Intro to Programming with C#

C# 103

• Data Structures– Arrays– Collections

Page 6: C# 101: Intro to Programming with C#

C# 101: Introduction to C#

Setting up your Development Environment

Page 7: C# 101: Intro to Programming with C#

Installing Integrated Development Kit

• Download latest VS IDE from https://www.visualstudio.com/en-us/downloads/download-visual-studio-vs.aspx

Page 8: C# 101: Intro to Programming with C#

What is an IDE?

• IDE = Integrated Development Environment • Makes you more productive • Includes text editor, compiler, debugger,

context- sensitive help, works with different SDKs

• Visual Studio is the most widely used IDE

Page 9: C# 101: Intro to Programming with C#

Installing Visual Studio

• Download and install the latest Visual Studio for .Net framework(64 Bit version) from https://www.microsoft.com/en-gb/download/details.aspx?id=30653

• To start Visual Studio– On PC, double-click on Visual Studio

Page 10: C# 101: Intro to Programming with C#

Hands-on Exercise

Visual Studio Setup & Demo

Page 11: C# 101: Intro to Programming with C#

C# 101: Introduction to C#

Language Overview

Page 12: C# 101: Intro to Programming with C#

C# Language Overview

• Object-oriented• Widely available• Widely used

Page 13: C# 101: Intro to Programming with C#

C# Versions • Brief History… - C#'s principal designer and lead architect at Microsoft is Anders Hejlsberg, was previously involved with the design of Pascal, Delphi and Visual J++

• Major Version Releases– C# 1.0 (2002)– C# 2.0 ( 2005)– C# 3.0 (2008)– C# 4.0 (2010)– C# 5.0 (2012)– C# 6.0 (2015)

Page 14: C# 101: Intro to Programming with C#

Visual Studio Editions

• Visual Studio Standard Edition • Visual Studio Enterprise Edition• Visual Studio Community Edition• Visual Studio Express Edition

Page 15: C# 101: Intro to Programming with C#

.NET FrameworkA programming infrastructure created by Microsoft for building, deploying, and running applications and services that use .NET technologies, such as desktop applications and Web services.

The .NET Framework contains three major parts: the Common Language Runtime. the Framework Class Library. ASP.NET.

.NET framework is required to develop and compile programs

Developers must have this installed

Page 16: C# 101: Intro to Programming with C#

C# 101: Introduction to C#

How C# works

Page 17: C# 101: Intro to Programming with C#

How C# Works

Page 18: C# 101: Intro to Programming with C#

C# File Structure

Page 19: C# 101: Intro to Programming with C#

C# 101: Introduction to C#

Writing Your First Program

Page 20: C# 101: Intro to Programming with C#

Hello, World!

Page 21: C# 101: Intro to Programming with C#

Writing Your First C# Program• Create a new project in your IDE named Csharp101• Create a HelloWorld class in the src folder inside the Csharp101 project as illustrated below. using System; namespace HelloWorldApplication { class HelloWorld { static void Main(string[] args) { /* my first program in C# */ Console.WriteLine("Hello World"); Console.ReadKey(); } } }

Page 22: C# 101: Intro to Programming with C#

Compiling Your First Java Program• Save the HelloWorld class in the IDE• This automatically compiles the HelloWorld.cs file into

into a HelloWorld.class file• Go to the folder you created the csharp101 project on

your hard disk and open the src folder.• What do you see?

Page 23: C# 101: Intro to Programming with C#

Running Your First C# Program

• Build and Run your program in Visual Studio by Ctrl+Shift+B / F5

Page 24: C# 101: Intro to Programming with C#
Page 25: C# 101: Intro to Programming with C#

Anatomy of a C# ApplicationComments Class Name

Access modifier

Function/static method

Arguments

Page 26: C# 101: Intro to Programming with C#

Language Features

Page 27: C# 101: Intro to Programming with C#

Introduction to C#

Built-in Data Types

Page 28: C# 101: Intro to Programming with C#

Built-in Data Types• Data type are sets of values and operations

defined on those values.

Page 29: C# 101: Intro to Programming with C#

Basic Definitions

• Variable - a name that refers to a value.• Assignment statement - associates a value with a

variable.

Page 30: C# 101: Intro to Programming with C#

String Data Type

Data Type AttributesValues sequence of charactersTypical literals “Hello”, “1 “, “*”Operation ConcatenateOperator +

• Useful for program input and output.

Page 31: C# 101: Intro to Programming with C#

String Data Type

Page 32: C# 101: Intro to Programming with C#

String Data Type• Meaning of characters depends on context.

Page 33: C# 101: Intro to Programming with C#

String Data Type

Expression Value“Hi, “ + “Bob” “Hi, Bob”

“1” + “ 2 “ + “ 1” “ 1 2 1”“1234” + “ + “ + “99” “1234 + 99”

“1234” + “99” “123499”

Page 34: C# 101: Intro to Programming with C#

Hands-on Exercise

Command Line Arguments

Page 35: C# 101: Intro to Programming with C#

Exercise: Command Line Arguments

• Create the C# program below that takes a name as command-line argument and prints “Hi <name>, How are you?”

namespace ConsoleApplication1{ class Program { static void Main(string[] args) { string x; System.Console.WriteLine("Enter your name"); x = Console.ReadLine(); System.Console.WriteLine("Hi {0} , How are you", x); Console.ReadKey();

} }}

Page 36: C# 101: Intro to Programming with C#

Integer Data Type

Data Type Attributes

Values Integers between -2E31 to +2E31-1

Typical literals 1234, -99 , 99, 0, 1000000Operation Add subtract multiply divide remainder

Operator + - * / %

• Useful for expressing algorithms.

Page 37: C# 101: Intro to Programming with C#

Integer Data TypeExpression Value Comment

5 + 3 85 – 3 25 * 3 155 / 3 1 no fractional

part5 % 3 2 remainder1 / 0 run-time error

3 * 5 - 2 13 * has precedence

3 + 5 / 2 5 / has precedence

3 – 5 - 2 -4 left associative(3-5) - 2 -4 better style3 – (5-2) 0 unambiguous

Page 38: C# 101: Intro to Programming with C#

Double Data Type

• Useful in scientific applications and floating-point arithmetic

Data Type Attributes

Values Real numbers specified by the IEEE 754 standard

Typical literals 3.14159 6.022e23 -3.0 2.0 1.41421356237209

Operation Add subtract multiply divide

Operator + - * /

Page 39: C# 101: Intro to Programming with C#

Double Data TypeExpression Value

3.141 + 0.03 3.171

3.141 – 0.03 3.111

6.02e23 / 2 3.01e23

5.0 / 2.0 1.6666666666667

10.0 % 3.141 0.577

1.0 / 0.0 Infinity

Math.sqrt(2.0) 1.4142135623730951

Page 40: C# 101: Intro to Programming with C#

C# Math Library

Methods

Math.sin() Math.cos()Math.log() Math.exp()Math.sqrt() Math.pow()

Math.min() Math.max()Math.abs() Math.PI

http://java.sun.com/javase/6/docs/api/java/lang/Math.html

Page 41: C# 101: Intro to Programming with C#

Hands-on Exercise

Integer Operations

Page 42: C# 101: Intro to Programming with C#

Exercise: Integer Operations• Create a C# class named IntOps in the C101 project that performs integer operations

on a pair of integers from the command line and prints the results.

Page 43: C# 101: Intro to Programming with C#

Solution: Integer Operationsint num1, num2, sum, prod, quot, rem;num1 = int.Parse(Console.ReadLine());num2 = int.Parse(Console.ReadLine());sum = num1 + num2; prod = num1 * num2; quot = num1 / num2; rem = num1 % num2;System.Console.WriteLine(" {0} : {1} = {2}", num1, num2, sum.ToString());System.Console.WriteLine(" {0} : {1} = {2}", num1, num2, prod.ToString());System.Console.WriteLine(" {0} : {1} = {2}", num1, num2, quot.ToString());System.Console.WriteLine(" {0} : {1} = {2}", num1, num2, rem.ToString());Console.ReadLine();

Page 44: C# 101: Intro to Programming with C#

Boolean Data Type

• Useful to control logic and flow of a program.

Data Type Attributes

Values true or false

Typical literals true false

Operation and or not

Operator && || !

Page 45: C# 101: Intro to Programming with C#

Truth-table of Boolean Operations

a !a a b a && b a || b

true false false false false false

false true false true false true

true false false true

true true true true

Page 46: C# 101: Intro to Programming with C#

Boolean Comparisons

• Take operands of one type and produce an operand of type boolean.

operation meaning true false== equals 2 == 2 2 == 3!= Not equals 3 != 2 2 != 2< Less than 2 < 13 2 < 2

<= Less than or equal

2 <= 2 3 <= 2

> Greater than 13 > 2 2 > 13>= Greater than

or equal3 >= 2 2 >= 3

Page 47: C# 101: Intro to Programming with C#

Type Conversion

• Convert from one type of data to another. • Implicit – no loss of precision– with strings

• Explicit: – cast – method.

Page 48: C# 101: Intro to Programming with C#

Type Conversion Examplesexpression Expression type Expression value“1234” + 99 String “123499”

Int.Parse(“123”) int 123(int) 2.71828 int 2

Math.round(2.71828) long 3(int) Math.round(2.71828) int 3(int) Math.round(3.14159) int 3

11 * 0.3 double 3.3(int) 11 * 0.3 double 3.311 * (int) 0.3 int 0

(int) (11 * 0.3) int 3

Page 49: C# 101: Intro to Programming with C#

Hands-on Exercise

Leap Year Finder

Page 50: C# 101: Intro to Programming with C#

Exercise: Leap Year Finder

• A year is a leap year if it is either divisible by 400 or divisible by 4 but not 100.

• Write a java class named LeapYear in the Java101 project that takes a numeric year as command line argument and prints true if it’s a leap year and false if not

Page 51: C# 101: Intro to Programming with C#

Solution: Leap Year Finder

int year = int.Parse(Console.ReadLine());

Boolean isLeapYear;

isLeapYear = (year % 4 == 0) && (year % 100 != 0); isLeapYear = isLeapYear || (year % 400 == 0);

System.Console.WriteLine("the Year {0} is {1} ", year.ToString(), isLeapYear.ToString());

Console.ReadLine();

Page 52: C# 101: Intro to Programming with C#

Data Types Summary

• A data type is a set of values and operations on those values. – String for text processing– double, int for mathematical calculation– boolean for decision making

• Why do we need types? – Type conversion must be done at some level. – Compiler can help do it correctly. – Example: in 1996, Ariane 5 rocket exploded after takeoff

because of bad type conversion.

Page 53: C# 101: Intro to Programming with C#

Introduction to C#

Conditionals and Loops

Page 54: C# 101: Intro to Programming with C#

Conditionals and Loops

• Sequence of statements that are actually executed in a program.

• Enable us to choreograph control flow.

Page 55: C# 101: Intro to Programming with C#

Conditionals

• The if statement is a common branching structure. – Evaluate a boolean expression.

• If true, execute some statements. • If false, execute other statements.

Page 56: C# 101: Intro to Programming with C#

If Statement Example

if (Math.Sqrt(16) < 3) System.Console.WriteLine("the number is less than 3");Else System.Console.WriteLine("the number is greater than 3"); Console.ReadLine();

Page 57: C# 101: Intro to Programming with C#

More If Statement Examples

Page 58: C# 101: Intro to Programming with C#

While Loop

• A common repetition structure. – Evaluate a boolean expression. – If true, execute some statements. – Repeat.

Page 59: C# 101: Intro to Programming with C#

For Loop• Another common repetition structure.

– Execute initialization statement. – Evaluate a boolean expression.

• If true, execute some statements. – And then the increment statement. – Repeat.

Page 60: C# 101: Intro to Programming with C#

Anatomy of a For Loop

for (int i = 0; i < 5; i++) { System.Console.WriteLine("{0}", i); }

Console.ReadLine();

Page 61: C# 101: Intro to Programming with C#

Hands-on Exercise

Powers of Two

Page 62: C# 101: Intro to Programming with C#

Exercise: Powers of Two• Create a new C# project in Visual Studio named Pow2• Write a C# class named PowerOfTwo to print powers of 2 that are <= 2N

where N is a number passed as an argument to the program.– Increment i from 0 to N. – Double v each time

Page 63: C# 101: Intro to Programming with C#

Solution: Power of 2

Page 64: C# 101: Intro to Programming with C#

Control Flow Summary• Sequence of statements that are actually executed in a

program. • Conditionals and loops enable us to choreograph the

control flow. Control flow Description Example

Straight line programs

all statements are executed in the order given

Conditionals certain statements are executed depending on the values of certain variables

IfIf-else

Loops certain statements are executed repeatedly until certain conditions are met

while for

do-while

Page 65: C# 101: Intro to Programming with C#

Homework Exercises

Java 101: Introduction to C#

Page 66: C# 101: Intro to Programming with C#

Hands-on Exercise

Array of Days

Page 67: C# 101: Intro to Programming with C#

Exercise: Array of Days

• Create a C# class named DayPrinter that prints out names of the days in a week from an array using a for-loop.

Page 68: C# 101: Intro to Programming with C#

Solution: Arrays of Days

string[] daysOfTheWeek = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; for(int i= 0;i < daysOfTheWeek.Length ;i++){ System.Console.WriteLine("{0}", daysOfTheWeek[i]);}

Page 69: C# 101: Intro to Programming with C#

Hands-on Exercise

Print Personal Details

Page 70: C# 101: Intro to Programming with C#

Exercise: Print Personal Details

• Write a program that will print your name and address to the console, for example:

Alex Johnson23 Main StreetNew York, NY 10001 USA

Page 71: C# 101: Intro to Programming with C#

Further Reading