C#: Statements and Methods Based on slides by Joe Hummel.
Transcript of C#: Statements and Methods Based on slides by Joe Hummel.
![Page 1: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/1.jpg)
C#: Statements and Methods
Based on slides by Joe Hummel
![Page 2: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/2.jpg)
2UCN Technology: Computer Science – 2014
Contents
“With regards to programming statements and methods, C# offers what you would come to expect from a modern OOPL…”
• Statements• Methods
![Page 3: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/3.jpg)
3UCN Technology: Computer Science – 2014
Part 1
• Statements
![Page 4: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/4.jpg)
4UCN Technology: Computer Science – 2014
Statements in C#
• Assignments• Sub routines and functions• Conditionals
– if, switch• Iteration
– for, while, do-while• (Uncontrolled) Control Flow
– return, break, continue, goto
![Page 5: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/5.jpg)
5UCN Technology: Computer Science – 2014
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++)
{ ...}
![Page 6: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/6.jpg)
6UCN Technology: Computer Science – 2014
Other Statements
• C# also includes…– iteration over a data structure (Collection) using foreach– namespace imported using using
![Page 7: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/7.jpg)
7UCN Technology: Computer Science – 2014
foreach
• Specialised foreach loop for sweeping through collections, e.g. array– Reduces the risk for indexing errors– Allows only read only access to elements
int[] data = { 1, 2, 3, 4, 5 };int sum = 0;
foreach (int x in data){ sum += x;}
foreach
type value collection
![Page 8: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/8.jpg)
8UCN Technology: Computer Science – 2014
using
• using directive provides access to classes in a namespace without pre-fixing
// 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 { . . . }}
![Page 9: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/9.jpg)
9UCN Technology: Computer Science – 2014
Example
• using directives in the top of the 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 { . . . }}
![Page 10: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/10.jpg)
10UCN Technology: Computer Science – 2014
Part 2
• Methods…
![Page 11: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/11.jpg)
11UCN Technology: Computer Science – 2014
Types of methods
• Classes may define 2 types of methods:– instance– static
• Instance methods can only be applied to instances (objects) of the class. So an object must be created before the method can be used.
• Static methods may be called using the class name
![Page 12: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/12.jpg)
12UCN Technology: Computer Science – 2014
Example
• The 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(no static modifier)
static method(static modifier)
![Page 13: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/13.jpg)
13UCN Technology: Computer Science – 2014
Method call
• Method call (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]); }}
![Page 14: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/14.jpg)
14UCN Technology: Computer Science – 2014
Other static methods that may come handy…
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); }}
• Adding two integers and printing the sum:
![Page 15: C#: Statements and Methods Based on slides by Joe Hummel.](https://reader035.fdocuments.us/reader035/viewer/2022072011/56649e1b5503460f94b09e4f/html5/thumbnails/15.jpg)
15UCN Technology: Computer Science – 2014
Summing Up
• All the usual statements (known from Java), and a few new ones– assignment, if, for, while, foreach, using
• Two types of methods– instance methods, needs an object– static methods, may be called on the class