Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn."— Presentation transcript:

1 CIS 3301 C# Lesson 5 Methods

2 CIS 3302 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.

3 CIS 3303 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

4 CIS 3304 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; }

5 CIS 3305 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)

6 CIS 3306 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

7 CIS 3307 C# Lesson 6 Namespaces

8 CIS 3308 Objectives Understand what Namespace is. Learn how to implement the using directive. Learn to use alias directive. Understand what are namespace members.

9 CIS 3309 Namespaces C# program elements designed to help you organize your programs. Namespaces don't correspond to file or directory names Syntax: // Namespace Declaration using System; // The C# Station Namespace namespace 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."); } } }

10 CIS 33010 Nested Namespaces // Namespace Declaration using System; // The C# Station Tutorial Namespace namespace 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."); } } } }

11 CIS 33011 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 above namespace csharp_station.tutorial { class myExample2 { public static void myPrint2() …

12 CIS 33012 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 Declaration using System; using csharp_station.tutorial; Alias directives shorten typing using csTut = csharp_station.tutorial.myExample;

13 CIS 33013 Namespaces (cont) Namespaces can hold other types: –Classes –Structures –Interfaces –Enumerations –Delegates


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

Similar presentations


Ads by Google