+ All Categories
Home > Documents > C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A....

C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A....

Date post: 12-Jan-2016
Category:
Upload: aubrey-mccormick
View: 224 times
Download: 2 times
Share this document with a friend
31
C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1
Transcript
Page 1: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

C#C#

Classes & Methods

CS3260

Dennis A. FaircloughVersion 1.1

Classes & Methods

CS3260

Dennis A. FaircloughVersion 1.1

Page 2: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

C# Program StructureC# Program Structure

• Namespace(s)– Class(es)

• Method(s)– Statements

• Namespace(s)– Class(es)

• Method(s)– Statements

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 2

Page 3: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

namespace Program

{

static class A

{

static void Main( )

{

Console.WriteLine(“Hello”);

}

}

}

namespace Program

{

static class A

{

static void Main( )

{

Console.WriteLine(“Hello”);

}

}

}Copyright © 2010 by Dennis A. Fairclough all rights reserved. 3

Page 4: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

NamespacesNamespaces• Global• System• User

– Separate– Cascaded

• namespace – keyword –• public implied

• Global• System• User

– Separate– Cascaded

• namespace – keyword –• public implied

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 4

Page 5: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Fully Qualified NameFully Qualified Name

• Uniquely identifies the namespace• i.e. System.Text.RegularExpressions

• Uniquely identifies the namespace• i.e. System.Text.RegularExpressions

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 5

Page 6: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

NamespacesNamespaces

• Namespaces provide a hierarchical means of organizing C# programs and libraries. Namespaces contain types and other namespaces …

C# 3.0 Specification

• Scoping Mechanism

• Namespaces provide a hierarchical means of organizing C# programs and libraries. Namespaces contain types and other namespaces …

C# 3.0 Specification

• Scoping MechanismCopyright © 2010 by Dennis A. Fairclough all rights reserved. 6

Page 7: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

using Directive / Statementusing Directive / Statement• using – keyword –• using System; //directs compiler to use System namespace

using (TextWriter w) //statement

{

w.WriteLine(“Line One”);

}

• using – keyword –• using System; //directs compiler to use System namespace

using (TextWriter w) //statement

{

w.WriteLine(“Line One”);

}Copyright © 2010 by Dennis A. Fairclough all rights reserved. 7

Page 8: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Classes & ObjectsClasses & Objects• Classes are the most fundamental of C#’s types. A class

is a data structure that combines state (fields) and actions (methods and other function members) in a single unit. A class provides a definition for dynamically created instances of the class, also known as objects. Classes support inheritance and polymorphism, mechanisms whereby derived classes can extend and specialize base classes.

C# 3.0 Specification

• Classes are the most fundamental of C#’s types. A class is a data structure that combines state (fields) and actions (methods and other function members) in a single unit. A class provides a definition for dynamically created instances of the class, also known as objects. Classes support inheritance and polymorphism, mechanisms whereby derived classes can extend and specialize base classes.

C# 3.0 Specification

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 8

Page 9: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

ClassesClasses• Instance class

– Instantiated as an object to be used

• Instance class with static members– class level for static members– instantiated for instance members

• static class– class level for members

• abstract class – Discuss later

• Instance class– Instantiated as an object to be used

• Instance class with static members– class level for static members– instantiated for instance members

• static class– class level for members

• abstract class – Discuss laterCopyright © 2010 by Dennis A. Fairclough all rights reserved. 9

Page 10: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Class MembersClass Members

• The members of a class are either static members, abstract members or instance members. Static members belong to classes, and instance members belong to objects (instances of classes). C# 3.0 Specification

• The members of a class are either static members, abstract members or instance members. Static members belong to classes, and instance members belong to objects (instances of classes). C# 3.0 Specification

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 10

Page 11: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Class MembersClass Members

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 11

Member DescriptionConstants Constant values associated with the classFields Variables of the classMethods Computations and actions that can be performed by the classProperties Actions associated with reading and writing named properties of the classIndexers Actions associated with indexing instances of the class like an arrayEvents Notifications that can be generated by the classOperators Conversions and expression operators supported by the classConstructors Actions required to initialize instances of the class or the class itselfDestructors Actions to perform before instances of the class are permanently discardedTypes Nested types declared by the class

Page 12: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

AccessibilityAccessibility

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 12

Accessibility Meaningpublic Access not limitedprotected Access limited to this class or classes derived from this classinternal Access limited to this programprotected internal Access limited to this program or classes derived from this classprivate Access limited to this class

Page 13: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

System.ObjectSystem.Object

• The MOST base class for any class• Provided by the compiler if you don’t• object or Object or System.Object

• The MOST base class for any class• Provided by the compiler if you don’t• object or Object or System.Object

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 13

Page 14: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Class ModifiersClass Modifiers

• abstract – Must be inherited from• sealed – Cannot be inherited from• static – Only static members• Contained class – A class declared in

containing class• Generic – Meta class

• abstract – Must be inherited from• sealed – Cannot be inherited from• static – Only static members• Contained class – A class declared in

containing class• Generic – Meta class

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 14

Page 15: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

MethodsMethods• Method, function, procedure, other• Parameters – Zero or more• Method body• Local variables• Static or instance• virtual, override, new, abstract• Overloaded

• Method, function, procedure, other• Parameters – Zero or more• Method body• Local variables• Static or instance• virtual, override, new, abstract• Overloaded

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 15

Page 16: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Other MethodsOther Methods• Constructors• Properties• Indexers• Events• Operators• Destructors• Partial

• Constructors• Properties• Indexers• Events• Operators• Destructors• Partial Copyright © 2010 by Dennis A. Fairclough all rights reserved. 16

Page 17: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Method ParametersMethod Parameters

• Value• Reference• Output• Parameter Arrays

• Value• Reference• Output• Parameter Arrays

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 17

Page 18: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

On and OnOn and On• Static• Instance• Virtual• Override/new• Sealed• Abstract• External• Partial• Extension• Overloaded• Generic

• Static• Instance• Virtual• Override/new• Sealed• Abstract• External• Partial• Extension• Overloaded• Generic

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 18

Page 19: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Method - WorkerMethod - Worker

• A method is a member that implements a computation or action that can be performed by an object or class. Static methods are accessed through the class. Instance methods are accessed through instances of the class.

• A method is a member that implements a computation or action that can be performed by an object or class. Static methods are accessed through the class. Instance methods are accessed through instances of the class.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 19

Page 20: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Method ParametersMethod Parameters

• Methods have a (possibly empty) list of parameters, which represent values or variable references passed to the method, and a return type, which specifies the type of the value computed and returned by the method. A method’s return type is void if it does not return a value.

• Methods have a (possibly empty) list of parameters, which represent values or variable references passed to the method, and a return type, which specifies the type of the value computed and returned by the method. A method’s return type is void if it does not return a value.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 20

Page 21: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Methods Pass ByMethods Pass By

• Value• Reference• out• param• etc.

• Value• Reference• out• param• etc.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 21

Page 22: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Method SignaturesMethod Signatures

• The signature of a method must be unique in the class in which the method is declared. The signature of a method consists of the name of the method, the list of parameters (if any), modifiers, and types of its parameters. The signature of a method does not include the return type.

• The signature of a method must be unique in the class in which the method is declared. The signature of a method consists of the name of the method, the list of parameters (if any), modifiers, and types of its parameters. The signature of a method does not include the return type.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 22

Page 23: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Parameters vs ArgumentsParameters vs Arguments

• Parameters are used to pass values or variable references to methods. The parameters of a method get their actual values from the arguments that are specified when the method is invoked. There are four kinds of parameters: value parameters, reference parameters, output parameters, and parameter arrays.

• Parameters are used to pass values or variable references to methods. The parameters of a method get their actual values from the arguments that are specified when the method is invoked. There are four kinds of parameters: value parameters, reference parameters, output parameters, and parameter arrays.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 23

Page 24: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Method BodyMethod Body

• A method’s body specifies the statements to execute when the method is invoked.

• A method body can declare variables that are specific to the invocation of the method. Such variables are called local variables. A local variable declaration specifies a type name, a variable name, and possibly an initial value.

• A method’s body specifies the statements to execute when the method is invoked.

• A method body can declare variables that are specific to the invocation of the method. Such variables are called local variables. A local variable declaration specifies a type name, a variable name, and possibly an initial value.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 24

Page 25: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Implicitly TypedImplicitly Typed

• When the local-variable-type is specified as var and no type named var is in scope, the declaration is an implicitly typed local variable declaration, whose type is inferred from the type of the associated initializer expression.

• When the local-variable-type is specified as var and no type named var is in scope, the declaration is an implicitly typed local variable declaration, whose type is inferred from the type of the associated initializer expression.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 25

Page 26: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Local VariablesLocal Variables• Data Typing

– Signed Integer – sbyte, short, int, long– Unsigned Integer – byte, ushort, uint, ulong– Floating Point – float, double, decimal– Character – char– Boolen – bool– Implicit - var

• Data Typing– Signed Integer – sbyte, short, int, long– Unsigned Integer – byte, ushort, uint, ulong– Floating Point – float, double, decimal– Character – char– Boolen – bool– Implicit - var

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 26

Page 27: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

KeywordsKeywords

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 27

abstract As base bool break

byte case catch char checked

class const continue decimal default

delegate do double else enum

event explicit extern false finally

fixed float for foreach goto

if implicit in int interface

internal is lock long namespace

new null object operator out

override params private protected public

readonly ref return sbyte sealed

short sizeof stackalloc static string

struct switch this throw true

try typeof uint ulong unchecked

unsafe ushort using virtual void

while        

Page 28: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Contextual KeywordsContextual Keywords

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 28

add ascending by descending equals

from get global group in

into join let on orderby

partial remove select set value

var where yield    

 Contextual keywordsThe language also has contextual keywords, which, while recognized by the compiler, can still be used, unqualified, as identifiers. These are:

Page 29: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Literal SuffixesLiteral Suffixes

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 29

Category C# type Notes Example

F float   float f = 1.0F;

D double   double d = 1D;

M decimal   decimal d = 1.0M;

U uint or ulong Combinable with L uint i = 1U;

L long or ulong Combinable with U ulong i = 1UL;

Page 30: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Floating Point ValuesFloating Point Values

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 30

Special value Double constant Float constant

NaN double.NaN float.NaN

+ double.PositiveInfinity float.PositiveInfinity

- double.NegativeInfinity float.NegativeInfinity

-0 -0.0 -0.0f

Page 31: C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.

Copyright © 2010 by Dennis A. Fairclough all rights reserved. 31

Character and string escape sequences

Char Meaning Value

\' Single quote 0x0027

\" Double quote 0x0022

\\ Backslash 0x005C

\0 Null 0x0000

\a Alert 0x0007

\b Backspace 0x0008

\f Form feed 0x000C

\n New line 0x000A

\r Carriage return 0x000D

\t Horizontal tab 0x0009

\v Vertical tab 0x000B


Recommended