cis 3301 c# lesson 5 methods. cis 3302 objectives understand the structure of a method. know the...
TRANSCRIPT
CIS 330 1
C# Lesson 5
Methods
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.
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
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; }
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)
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
CIS 330 7
C# Lesson 6
Namespaces
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.
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."); } }}
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."); } } }}
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() …
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;
CIS 330 13
Namespaces (cont)• Namespaces can hold other types:
– Classes– Structures– Interfaces– Enumerations– Delegates