0% found this document useful (0 votes)
7 views15 pages

04 Methods Stmts

The document provides an overview of statements and methods in C#, including types of statements such as assignment, conditional, and iteration. It explains the usage of 'foreach' for data structure iteration and the 'using' directive for namespace access. Additionally, it covers method types, parameter passing techniques, and provides examples of calling methods and performing operations.

Uploaded by

kushparekh943
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

04 Methods Stmts

The document provides an overview of statements and methods in C#, including types of statements such as assignment, conditional, and iteration. It explains the usage of 'foreach' for data structure iteration and the 'using' directive for namespace access. Additionally, it covers method types, parameter passing techniques, and provides examples of calling methods and performing operations.

Uploaded by

kushparekh943
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Statements and Methods

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

Microsoft 2
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++)
{
...
}

Microsoft 3
Other statements

• C# contains a couple surprises…


– data structure iteration via foreach
– namespace importing via using

Microsoft 4
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 foreach (int x in data)


{
sum += x;
}

type value collection

Microsoft 5
using

• using directive allows unqualified access to namespace


– a convenience mechanism only…
– still must ref underlying assembly namespace Workshop
{
public class Customer
{
// before .
.
.
Workshop.Customer c; }
c = new Workshop.Customer("joe hummel", 94652); public class Product
{
.
.
.
// after }
}

using Workshop;

Customer c;
c = new Customer("joe hummel", 94652);

Microsoft 6
Complete example

• using directive(s) specified at top of file namespace Workshop


{
public class Customer
{
.
.
/* main.cs */ .
}
using System; public class Product
using Workshop; {
.
.
public class App .
}
{ }
public static void Main()
{
Customer c;
c = new Customer("joe hummel", 94652);
Console.WriteLine( c.ToString() );
}
}

Microsoft 7
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

Microsoft 8
Example

• Array class in FCL


– fully-qualified name is System.Array

namespace System
{
public class Array
{
instance method public int GetLength(int dimension)
(absence of static) { ... }

static method public static void Sort(Array a)


(presence of static) { ... }
.
.
.
}
}

Microsoft 9
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]);
}
}

Microsoft 10
Other useful static methods
• A program to add 2 integers and output the sum:

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);
}
}
Microsoft 11
Parameter passing

• C# offers three options:


– pass-by-value (default)
– pass-by-reference
– pass-by-result ("copy-out")

Microsoft 12
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;

y unchanged F(y);

Microsoft 13
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,
void G(ref int x)
initially 9
{
x += 1;
}

int y = 9;

y set to 10 G(ref y);

Microsoft 14
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)


{
assignment required x = 0;
}

int y;

y set to 0 H(out y);

Microsoft 15

You might also like