CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Similar documents
2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS111: PROGRAMMING LANGUAGE II

CS110D: PROGRAMMING LANGUAGE I

CS111: PROGRAMMING LANGUAGE II

Lecture 5: Methods CS2301

by Pearson Education, Inc. All Rights Reserved. 2

CS1150 Principles of Computer Science Methods

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

CS-201 Introduction to Programming with Java

Introduction to Programming Using Java (98-388)

CS1150 Principles of Computer Science Methods

Personal SE. Functions & Arrays

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Object Oriented Programming. Java-Lecture 6 - Arrays

Announcements. PS 3 is due Thursday, 10/6. Midterm Exam 1: 10/14 (Fri), 9:00am-10:53am

1 Lexical Considerations

Computer Programming: C++

C Programming for Engineers Functions

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Introduction to Programming (Java) 4/12

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Chapter 4 Defining Classes I

Lecture 18 Tao Wang 1

EECS168 Exam 3 Review

New Concepts. Lab 7 Using Arrays, an Introduction

Java Methods. Lecture 8 COP 3252 Summer May 23, 2017

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

4. C++ functions. 1. Library Function 2. User-defined Function

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

IT 374 C# and Applications/ IT695 C# Data Structures

ECE 122. Engineering Problem Solving with Java

( &% class MyClass { }

Chapter 7 Functions. Now consider a more advanced example:

Lab #10 Multi-dimensional Arrays

Programming Languages: Lecture 11

Object Oriented Methods : Deeper Look Lecture Three

Object Oriented Design

엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University COPYRIGHTS 2017 EOM, HYEONSANG ALL RIGHTS RESERVED

Methods. CSE 114, Computer Science 1 Stony Brook University

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

CMSC 4023 Chapter 9. Fundamentals of Subprograms Introduction

Computer Science & Engineering 150A Problem Solving Using Computers

4. Inputting data or messages to a function is called passing data to the function.

Chapter 10 Introduction to Classes

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lexical Considerations

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Chapter 3 Function Overloading

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

CSCE 206: Structured Programming in C++

Chapter 9. Subprograms

BITG 1113: Function (Part 2) LECTURE 5

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

4. Java language basics: Function. Minhaeng Lee

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Chapter 9 Subprograms

CS-201 Introduction to Programming with Java

Lexical Considerations

CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

CS171:Introduction to Computer Science II

Functions and Recursion

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS110: PROGRAMMING LANGUAGE I

Constructors for classes

Array. Prepared By - Rifat Shahriyar

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Chapter 7 Array. Array. C++, How to Program

Introduction To C#.NET

Computer Science 1 Honors

Chapter 6: Methods. Objectives 9/21/18. Opening Problem. Problem. Problem. Solution. CS1: Java Programming Colorado State University

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Java Primer 1: Types, Classes and Operators

Lecture 9 - C Functions

Functions. Lecture 6 COP 3014 Spring February 11, 2018


Methods: A Deeper Look

Methods. Bok, Jong Soon

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

CS313D: ADVANCED PROGRAMMING LANGUAGE

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Exam 3 Chapters 7 & 9

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

HST 952. Computing for Biomedical Scientists Lecture 5

Protection Levels and Constructors The 'const' Keyword


COP3502 Programming Fundamentals for CIS Majors 1. Instructor: Parisa Rashidi

CS159. Nathan Sprague

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

EK131 E5 Introduction to Engineering

1/16/2013. Program Structure. Language Basics. Selection/Iteration Statements. Useful Java Classes. Text/File Input and Output.

C Pointers. 6th April 2017 Giulio Picierro

C++ for Engineers and Scientists. Third Edition. Chapter 12 Pointers

Transcription:

CS313D: ADVANCED PROGRAMMING LANGUAGE Lecture 3: C# language basics II

Lecture Contents 2 C# basics Methods Arrays

Methods 3 A method: groups a sequence of statement takes input, performs actions, and produces output In C#, each method is defined within specific class

Method Declaration: Header 4 A method declaration begins with a method header public class MyClass { static int min ( int num1, int num2 ) properties return type method name parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a formal argument

Method Declaration: Body 5 The header is followed by the method body: class MyClass { static int min(int num1, int num2) { int minvalue = num1 < num2? num1 : num2; return minvalue; } }

The return Statement 6 The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type The return statement specifies the value that will be returned Its expression must conform to the return type

Calling a Method 7 Each time a method is called, the values of the actual arguments in the invocation are assigned to the formal arguments int num = min(2, 3); static int min (int num1, int num2) { } int minvalue = (num1 < num2? num1 : num2); return minvalue;

Example 8

9

Method Call Stack 10 A method can call another method, who can call another method, main Max(num1, num2, num3) WriteLine() Max(1, 2, 3); WriteLine( )

Method Overloading 11 Methods of the same name declared in the same class Must have different sets of parameters (signatures). the compiler differentiates signatures by : the number of parameters, the types of the parameters and the order of the parameter types in each signature. Method calls cannot be distinguished by return type. Overloaded methods can have different return types if the methods have different parameter lists.

Example 12

13

14 Error!!

Optional Parameters 15 Methods can have optional parameters that allow the calling method to vary the number of arguments to pass. An optional parameter specifies a default value that s assigned to the parameter if the optional argument is omitted. Example: public int Power( int basevalue, int exponentvalue = 2) You can create methods with one or more optional parameters. All optional parameters must be placed to the right of the method s non-optional parameters.

16 Be careful!!

Example 17 Power() Power(10) Power(10, 3)

Passing arguments to Methods 18 Pass-by-value Pass-by-reference also called call-by-value A copy of the argument s value is passed to the called method. The called method works exclusively with the copy Changes to the called method s copy do not affect the original variable s value in the caller. also called call-byreference The called method can access the argument s value directly and modify that data, if necessary Improves performance by eliminating the need to copy

Passing arguments to Methods 19 Applying the ref keyword to a parameter declaration allows you to pass a variable to a method by reference The ref keyword is used for variables that already have been initialized in the calling method. Preceding a parameter with keyword out creates an output parameter. This indicates to the compiler that the argument will be passed by reference and that the called method will assign a value to it. A method can return multiple output parameters.

Example 20

21

22 Be careful!!

23 Packaging Code in C# The Framework Class Library provides many predefined classes that contain methods for performing common tasks.

24

Because these methods are static, you can access them via the class name Math and the member access (.) operator, just like class Math s methods. 25

26 That s all.. Chapter 7

27 Arrays

What is an array? 28 Array data structures Group of variables (called elements) containing values of the same type. related data items of the same type. fixed length once created. Elements referred to using index or subscript. In C#, Arrays are objects, so they re considered reference types. Every array object knows its own length and stores it in a Length instance variable. Elements can be either primitive types or reference types (strings).

29 Array elements An index must be a nonnegative integer Can use an expression as an index Every array object knows its own length and stores it in a length instance variable

Arrays in C# 30 declare create initialize int[] a; int a[]; a = new int[5]; for(int i=0;i<5;i++) a[i] = i*i;

Example 31

Example: 32

Example 33

Example 34

35

foreach Statement 36 The foreach statement iterates through the elements of an entire array or collection. syntax foreach( type identifier in arrayname ) statement type and identifier are the type and name (e.g., int number) of the iteration variable. arrayname is the array through which to iterate. The type of the iteration variable must be consistent with the type of the elements inthe array. The iteration variable represents successive values in the array on successive iterations of the foreach statement.

Example 37

38 That s all.. Chapter 8

39 Case Studies

Q: 40