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

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Lecture 5: Interfaces.
Programming Languages and Paradigms The C Programming Language.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Lecture 2 Basics of C#. Members of a Class A field is a variable of any type that is declared directly in a class. Fields are members of their containing.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Advanced Object-Oriented Programming Features
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
1 Symbols. 2 Identifiers Syntax Identifier = (letter | '_' | {letter | digit | '_'}. Unicode! Case-sensitive can be used to treat keywords as.
Review of C++ Programming Part II Sheng-Fang Huang.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Programming in C# Language Overview
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo CET 3640 © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
C#.Net Software Development Versions 1.0. Overview  Rational for Extension Methods  C# Specification for Extension Methods  Requirements for Extension.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
Chapter -6 Polymorphism
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
By Mr. Muhammad Pervez Akhtar
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
MAITRAYEE MUKERJI Object Oriented Programming in C++
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Modern Programming Tools And Techniques-I
Creating and Using Objects, Exceptions, Strings
Classes (Part 1) Lecture 3
C# - OOP TTU IT College , Autumn SEMESTER Andres käver.
3 Introduction to Classes and Objects.
2.7 Inheritance Types of inheritance
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Computing with C# and the .NET Framework
Programming Language Concepts (CIS 635)
OOP’S Concepts in C#.Net
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 6 Methods: A Deeper Look
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Module 2 Variables, Assignment, and Data Types
Overview of C++ Polymorphism
Object Oriented Programming in java
CIS 199 Final Review.
Class.
Classes, Objects and Methods
Chengyu Sun California State University, Los Angeles
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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:

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;

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 f

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