Module 13: Properties and Indexers. Overview Using Properties Using Indexers.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Introduction to C# Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
ECE 103 Engineering Programming Chapter 11 One Minute Synopsis Herbert G. Mayer, PSU CS Status 7/1/2014.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
C# Structs, operator overloading & attributes. Structs ~ Structures Structs are similar to classes: they represent data structures with data and functions.
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Java Syntax Primitive data types Operators Control statements.
Computer Science II Exam I Review Monday, February 6, 2006.
1 CMSC 132: Object-Oriented Programming II Java Constructs Department of Computer Science University of Maryland, College Park.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
C#/Java Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
ILM Proprietary and Confidential -
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
CPS120: Introduction to Computer Science Functions.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
Module 10: Inheritance in C#. Overview Deriving Classes Implementing Methods Using Sealed Classes Using Interfaces Using Abstract Classes.
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
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
Introduction to Object-Oriented Programming Lesson 2.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
C# Classes ISYS 350. Introduction to Classes A class is the blueprint for an object. – It describes a particular type of object. – It specifies the properties.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
Const declares immutable value void Test(char* t) { t++;// OK, address is not const t[0] = 'a'; // OK, char is not const } void Test2(const char* t) {
Properties and Indexers 9. © Aptech Ltd.Building Applications Using C# / Session 92 Objectives  Define properties in C#  Explain properties, fields,
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Arrays and Indexers Programming in C# Arrays and Indexers CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
Features of.net Language independent Object oriented program Multi threading Exception handling Drag and drop Linq
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Creating and Using Objects, Exceptions, Strings
Web Design & Development Lecture 9
Classes (Part 1) Lecture 3
Module 5: Common Type System
Methods Attributes Method Modifiers ‘static’
Chapter 8 Advanced C#.
Chapter 3: Using Methods, Classes, and Objects
Computing with C# and the .NET Framework
Creating Your OwnClasses
EEC-693/793 Applied Computer Vision with Depth Cameras
CIS 199 Final Review.
Class.
Corresponds with Chapter 5
Presentation transcript:

Module 13: Properties and Indexers

Overview Using Properties Using Indexers

 Using Properties Why Use Properties? Using Accessors Comparing Properties to Fields Comparing Properties to Methods Property Types Property Example

Why Use Properties? Properties provide: A useful way to encapsulate information inside a class Concise syntax Flexibility

Using Accessors Properties provide field-like access Use get accessor statements to provide read access Use set accessor statements to provide write access class Button { public string Caption // Property { get { return caption; } set { caption = value; } } private string caption; // Field } class Button { public string Caption // Property { get { return caption; } set { caption = value; } } private string caption; // Field }

Comparing Properties to Fields Properties are “logical fields” The get accessor can return a computed value Similarities Syntax for creation and use is the same Differences Properties are not values; they have no address Properties cannot be used as ref or out parameters to methods

Comparing Properties to Methods Similarities Both contain code to be executed Both can be used to hide implementation details Both can be virtual, abstract, or override Differences Syntactic – properties do not use parentheses Semantic – properties cannot be void or take arbitrary parameters

Property Types Read/write properties Have both get and set accessors Read-only properties Have get accessor only Are not constants Write-only properties – very limited use Have set accessor only Static properties Apply to the class and can access only static data

Property Example public class Console { public static TextReader In { get { if (reader == null) { reader = new StreamReader(...); } return reader; }... private static TextReader reader = null; } public class Console { public static TextReader In { get { if (reader == null) { reader = new StreamReader(...); } return reader; }... private static TextReader reader = null; }

 Using Indexers What Is an Indexer? Comparing Indexers to Arrays Comparing Indexers to Properties Using Parameters to Define Indexers String Example BitArray Example

What Is an Indexer? An indexer provides array-like access to an object Useful if a property can have multiple values To define an indexer Create a property called this Specify the index type To use an indexer Use array notation to read or write the indexed property

Comparing Indexers to Arrays Similarities Both use array notation Differences Indexers can use non-integer subscripts Indexers can be overloaded—you can define several indexers, each using a different index type Indexers are not variables, so they do not denote storage locations—you cannot pass an indexer as a ref or an out parameter

Comparing Indexers to Properties Similarities Both use get and set accessors Neither have an address Neither can be void Differences Indexers can be overloaded Indexers cannot be static

Using Parameters to Define Indexers When defining indexers Specify at least one indexer parameter Specify a value for each parameter you specify Do not use ref or out parameter modifiers

String Example The String class Is an immutable class Uses an indexer ( get accessor but no set accessor) class String { public char this[int index] { get { if (index = Length) throw new IndexOutOfRangeException( );... }... } class String { public char this[int index] { get { if (index = Length) throw new IndexOutOfRangeException( );... }... }

BitArray Example class BitArray { public bool this[int index] { get { BoundsCheck(index); return (bits[index >> 5] & (1 << index)) != 0; } set { BoundsCheck(index); if (value) { bits[index >> 5] |= (1 << index); } else { bits[index >> 5] &= ~(1 << index); } private int[ ] bits; } class BitArray { public bool this[int index] { get { BoundsCheck(index); return (bits[index >> 5] & (1 << index)) != 0; } set { BoundsCheck(index); if (value) { bits[index >> 5] |= (1 << index); } else { bits[index >> 5] &= ~(1 << index); } private int[ ] bits; }

Lab 13.1: Using Properties and Indexers

Review Using Properties Using Indexers