Download presentation
Presentation is loading. Please wait.
1
4. Statements and Methods
2
2 Microsoft Objectives “With regards to programming statements and methods, C# offers what you would come to expect from a modern OOPL…” Statements Methods
3
3 Microsoft Part 1 Statements…
4
4 Microsoft Statements in C# C# supports the standard assortment… Assignment Subroutine and function call Conditional –if, switch Iteration –for, while, do-while Control Flow –return, break, continue, goto
5
5 Microsoft Examples x = obj.foo(); if (x > 0 && x < 10) count++; else if (x == -1)... else {... } while (x > 0) {... x--; } for (int k = 0; k < 10; k++) {... }
6
6 Microsoft Other statements C# contains a couple surprises… –data structure iteration via foreach –namespace importing via using
7
7 Microsoft foreach Specialized foreach loop provided for collections like array –reduces risk of indexing error –provides read only access int[] data = { 1, 2, 3, 4, 5 }; int sum = 0; foreach (int x in data) { sum += x; } foreach typevaluecollection
8
8 Microsoft using using directive allows unqualified access to namespace –a convenience mechanism only… –still must ref underlying assembly // before Workshop.Customer c; c = new Workshop.Customer("joe hummel", 94652); // after using Workshop; Customer c; c = new Customer("joe hummel", 94652); namespace Workshop { public class Customer {. } public class Product {. }
9
9 Microsoft Complete example using directive(s) specified at top of file /* main.cs */ using System; using Workshop; public class App { public static void Main() { Customer c; c = new Customer("joe hummel", 94652); Console.WriteLine( c.ToString() ); } namespace Workshop { public class Customer {. } public class Product {. }
10
10 Microsoft Part 2 Methods…
11
11 Microsoft Types of methods Classes contain 2 types of methods: –those with no return value ( void ) –those with a return value ( int, string, etc.) Methods may be: –instance –static Instance methods require an object to call Static methods are global and thus require only class name
12
12 Microsoft Example Array class in FCL –fully-qualified name is System.Array namespace System { public class Array { public int GetLength(int dimension) {... } public static void Sort(Array a) {... }. } instance method (absence of static) static method (presence of static)
13
13 Microsoft Calling methods Here's an example of calling into the Array class: /* main.cs */ using System; public class App { public static void Main() { int[] data = { 11, 7, 38, 55, 3 }; Array.Sort(data); for (int i=0; i<data.GetLength(0); i++) Console.WriteLine(i + ": " + data[i]); }
14
14 Microsoft Other useful static methods using System; public class Calculator { public static void Main() { string input, output; int a, b, sum; Console.Write("Enter first integer: "); input = Console.ReadLine(); a = Convert.ToInt32(input); Console.Write("Enter second integer: "); input = Console.ReadLine(); b = Convert.ToInt32(input); sum = a + b; output = String.Format("{0} + {1} = {2}", a, b, sum); Console.WriteLine(output); } A program to add 2 integers and output the sum:
15
15 Microsoft Parameter passing C# offers three options: –pass-by-value (default) –pass-by-reference –pass-by-result ("copy-out")
16
16 Microsoft Pass-by-value Pass by value is default parameter passing mechanism –data copied into method –any changes to parameter inside method affect local copy only value parameter void F(int x) { x = 0; } int y = 9; F(y); y unchanged
17
17 Microsoft Pass-by-reference ref parameter passes data in and out –use keyword ref in definition and call –must use variable in call –must initialize passed variable before call ref parameter, initially 9 void G(ref int x) { x += 1; } int y = 9; G(ref y); y set to 10
18
18 Microsoft Pass-by-result ("copy-out") out parameter returns data through parameter –use keyword out in both definition and call –must use variable in call –must assign to parameter inside method or compiler error out parameter void H(out int x) { x = 0; } int y; H(out y); y set to 0 assignment required
19
19 Microsoft Summary Standard statements, a few non-standard ones –assignment, if, for, while, foreach, using Two types of methods –instance methods require an object to call –static methods are global and thus require only class name
20
20 Microsoft References Books: –I. Pohl, "C# by Dissection" –S. Lippman, "C# Primer" –J. Mayo, "C# Unleashed"
21
21 Microsoft Lab? Work on lab #2, "Types, Stmts and Methods"…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.