Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1."— Presentation transcript:

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

2 C# 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

3 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

4 Namespaces 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

5 Fully 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

6 Namespaces 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 Mechanism Copyright © 2010 by Dennis A. Fairclough all rights reserved. 6

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

8 Classes & 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

9 Classes 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 later Copyright © 2010 by Dennis A. Fairclough all rights reserved. 9

10 Class 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 Copyright © 2010 by Dennis A. Fairclough all rights reserved. 10

11 Class Members Copyright © 2010 by Dennis A. Fairclough all rights reserved. 11 MemberDescription ConstantsConstant values associated with the class FieldsVariables of the class MethodsComputations and actions that can be performed by the class PropertiesActions associated with reading and writing named properties of the class IndexersActions associated with indexing instances of the class like an array EventsNotifications that can be generated by the class OperatorsConversions and expression operators supported by the class ConstructorsActions required to initialize instances of the class or the class itself DestructorsActions to perform before instances of the class are permanently discarded TypesNested types declared by the class

12 Accessibility Copyright © 2010 by Dennis A. Fairclough all rights reserved. 12 AccessibilityMeaning public Access not limited protected Access limited to this class or classes derived from this class internal Access limited to this program protected internal Access limited to this program or classes derived from this class private Access limited to this class

13 System.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

14 Class 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

15 Methods 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

16 Other 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

17 Method Parameters Value Reference Output Parameter Arrays Value Reference Output Parameter Arrays Copyright © 2010 by Dennis A. Fairclough all rights reserved. 17

18 On 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

19 Method - 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. Copyright © 2010 by Dennis A. Fairclough all rights reserved. 19

20 Method 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. Copyright © 2010 by Dennis A. Fairclough all rights reserved. 20

21 Methods Pass By Value Reference out param etc. Value Reference out param etc. Copyright © 2010 by Dennis A. Fairclough all rights reserved. 21

22 Method 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. Copyright © 2010 by Dennis A. Fairclough all rights reserved. 22

23 Parameters 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. Copyright © 2010 by Dennis A. Fairclough all rights reserved. 23

24 Method 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

25 Implicitly 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. Copyright © 2010 by Dennis A. Fairclough all rights reserved. 25

26 Local 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

27 Keywords Copyright © 2010 by Dennis A. Fairclough all rights reserved. 27 abstractAsbaseboolbreak bytecasecatchcharchecked classconstcontinuedecimaldefault delegatedodoubleelseenum eventexplicitexternfalsefinally fixedfloatforforeachgoto ifimplicitinintinterface internalislocklongnamespace newnullobjectoperatorout overrideparamsprivateprotectedpublic readonlyrefreturnsbytesealed shortsizeofstackallocstaticstring structswitchthisthrowtrue trytypeofuintulongunchecked unsafeushortusingvirtualvoid while

28 Contextual Keywords Copyright © 2010 by Dennis A. Fairclough all rights reserved. 28 addascendingbydescendingequals fromgetglobalgroupin intojoinletonorderby partialremoveselectsetvalue varwhereyield Contextual keywords The language also has contextual keywords, which, while recognized by the compiler, can still be used, unqualified, as identifiers. These are:

29 Literal Suffixes Copyright © 2010 by Dennis A. Fairclough all rights reserved. 29 Categ oryC# typeNotesExample Ffloat float f = 1.0F; Ddouble double d = 1D; Mdecimal decimal d = 1.0M; Uuint or ulongCombinable with Luint i = 1U; Llong or ulongCombinable with Uulong i = 1UL;

30 Floating Point Values Copyright © 2010 by Dennis A. Fairclough all rights reserved. 30 Special valueDouble constantFloat constant NaNdouble.NaNfloat.NaN +double.PositiveInfinityfloat.PositiveInfinity -double.NegativeInfinityfloat.NegativeInfinity -0-0.0-0.0f

31 Copyright © 2010 by Dennis A. Fairclough all rights reserved. 31 Character and string escape sequences CharMeaningValue \'Single quote0x0027 \"Double quote0x0022 \\Backslash0x005C \0Null0x0000 \aAlert0x0007 \bBackspace0x0008 \fForm feed0x000C \nNew line0x000A \rCarriage return0x000D \tHorizontal tab0x0009 \vVertical tab0x000B


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

Similar presentations


Ads by Google