cis 3301 c# lesson 5 methods. cis 3302 objectives understand the structure of a method. know the...

13
CIS 330 1 C# Lesson 5 Methods

Upload: ashlee-spencer

Post on 31-Dec-2015

215 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 1

C# Lesson 5

Methods

Page 2: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 2

Objectives

• Understand the structure of a method.

• Know the difference between static and instance methods.

• Learn to instantiate objects.

• Learn how to call methods of an instantiated object.

• Understand the 4 types of parameters.

• Learn how to use the this reference.

Page 3: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 3

Method Structure

• Methods allows us you to separate your logic into different units

• Syntax:attributes modifiers return-type method-name( parameters )

{ statements }

• Return-type can be any C# type• Method name must be a unique identifier • Parameters allow you to pass information

to and from a method

Page 4: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 4

Methods (cont)string getChoice()

    {        string myChoice;

        // Print A Menu        Console.WriteLine("My Address Book\n");

        Console.WriteLine("A - Add New Address");        Console.WriteLine("D - Delete Address");        Console.WriteLine("M - Modify Address");        Console.WriteLine("V - View Addresses");        Console.WriteLine("Q - Quit\n");

        Console.Write("Choice (A,D,M,V,or Q): ");

        // Retrieve the user's choice        myChoice = Console.ReadLine();        Console.WriteLine();

        return myChoice;     }

Page 5: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 5

Methods (cont)• To instantiate a new object:

OneMethod om = new OneMethod()

• Methods, fields, and other class members can be accessed, identified, or manipulated through the "." (dot) operator myChoice = om.getChoice();

• Could use parameters:void makeDecision(string myChoice)

Page 6: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 6

Methods (cont)

• There are 4 kinds of parameters a C# method can handle:– out: parameters only returned from a method – ref: reference to the parameter is copied– params: allows methods to accept a variable

number of arguments – value : default – copies value of parameter

• this: reference to the current object

Page 7: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 7

C# Lesson 6

Namespaces

Page 8: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 8

Objectives

• Understand what Namespace is.

• Learn how to implement the using directive.

• Learn to use alias directive.

• Understand what are namespace members.

Page 9: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 9

Namespaces • C# program elements designed to help you organize

your programs.• Namespaces don't correspond to file or directory names • Syntax:

// Namespace Declarationusing System;

// The C# Station Namespacenamespace csharp_station {    // Program start class    class NamespaceCSS     {        // Main begins program execution.        public static void Main()         {            // Write to console            Console.WriteLine("This is the new C# Station Namespace.");         }    }}

Page 10: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 10

Nested Namespaces// Namespace Declaration

using System;

// The C# Station Tutorial Namespacenamespace csharp_station {    namespace tutorial     {        // Program start class        class NamespaceCSS         {            // Main begins program execution.            public static void Main()             {                // Write to console                Console.WriteLine("This is the new C# Station Tutorial Namespace.");            }        }    }}

Page 11: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 11

Calling Namespaces// Namespace Declaration

using System;

namespace csharp_station {    // nested namespace    namespace tutorial     {        class myExample1         {            public static void myPrint1()             {                Console.WriteLine("First Example of calling another namespace member.");            }        }    }

    // Program start class    class NamespaceCalling     {        // Main begins program execution.        public static void Main()         {            // Write to console            tutorial.myExample1.myPrint1();             tutorial.myExample2.myPrint2();         }    }}// same namespace as nested namespace abovenamespace csharp_station.tutorial {    class myExample2     {        public static void myPrint2() …

Page 12: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 12

Namespaces (cont)• Every namespace member of the same

type must have a unique name• The using directive allows you to type the

method names of members of the namespace without typing the namespace

// Namespace Declarationusing System;using csharp_station.tutorial;

• Alias directives shorten typingusing csTut =

csharp_station.tutorial.myExample;

Page 13: CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn

CIS 330 13

Namespaces (cont)• Namespaces can hold other types:

– Classes– Structures– Interfaces– Enumerations– Delegates