SlideShare a Scribd company logo
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
}
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;
}
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 < 0 || 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;
}
Review
 Using Properties
 Using Indexers

More Related Content

PPT
Ppt lesson 12
Linda Bodrie
 
PPTX
Document type definitions part 2
Randy Riness @ South Puget Sound Community College
 
PPTX
Semantic DEX Components
David Price
 
PPTX
Week 1: Getting Your Hands Dirty - Part 1
Jamshid Hashimi
 
PPTX
Symbol of e r diagram presentation
Mehedi Hasan
 
PPTX
Data base
Byju Antony
 
PDF
Android ui adapter
Krazy Koder
 
Ppt lesson 12
Linda Bodrie
 
Document type definitions part 2
Randy Riness @ South Puget Sound Community College
 
Semantic DEX Components
David Price
 
Week 1: Getting Your Hands Dirty - Part 1
Jamshid Hashimi
 
Symbol of e r diagram presentation
Mehedi Hasan
 
Data base
Byju Antony
 
Android ui adapter
Krazy Koder
 

Similar to Module 14 properties and indexers (20)

PPTX
CSharp for Unity Day 3
Duong Thanh
 
PPTX
Indexers in C#
AA Coaching Academy
 
PPTX
Objects and Types C#
Raghuveer Guthikonda
 
PPTX
Properties and indexers in C#
Hemant Chetwani
 
PPTX
QSpiders - Major difference
Qspiders - Software Testing Training Institute
 
PPTX
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
NicheTech Com. Solutions Pvt. Ltd.
 
PPTX
PCSTt11 overview of java
Archana Gopinath
 
PDF
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
PPT
VB.net
PallaviKadam
 
PPT
Abap Inicio
unifor
 
PPT
Oo abap-sap-1206973306636228-5
prakash185645
 
PPTX
Apex code (Salesforce)
Mohammed Safwat Abu Kwaik
 
PPT
Intro To Scala
chambeda
 
PPTX
Ch.03 - Class Diagram_1 OBJECT ORIENTED ANALYSIS AND DESIGN [O] .pptx
SohagSrz
 
PPTX
C# - Igor Ralić
Software StartUp Academy Osijek
 
PPTX
Module 9 : using reference type variables
Prem Kumar Badri
 
PPTX
Structure.pptx
MohammedOmer401579
 
PPT
08. Object Oriented Database in DBMS
koolkampus
 
DOCX
C# concepts
lexilijoseph
 
PDF
C# chap 4
Shehrevar Davierwala
 
CSharp for Unity Day 3
Duong Thanh
 
Indexers in C#
AA Coaching Academy
 
Objects and Types C#
Raghuveer Guthikonda
 
Properties and indexers in C#
Hemant Chetwani
 
Cocoa and MVC in ios, iOS Training Ahmedbad , iOS classes Ahmedabad
NicheTech Com. Solutions Pvt. Ltd.
 
PCSTt11 overview of java
Archana Gopinath
 
Swift Tutorial Part 2. The complete guide for Swift programming language
Hossam Ghareeb
 
VB.net
PallaviKadam
 
Abap Inicio
unifor
 
Oo abap-sap-1206973306636228-5
prakash185645
 
Apex code (Salesforce)
Mohammed Safwat Abu Kwaik
 
Intro To Scala
chambeda
 
Ch.03 - Class Diagram_1 OBJECT ORIENTED ANALYSIS AND DESIGN [O] .pptx
SohagSrz
 
Module 9 : using reference type variables
Prem Kumar Badri
 
Structure.pptx
MohammedOmer401579
 
08. Object Oriented Database in DBMS
koolkampus
 
C# concepts
lexilijoseph
 
Ad

More from Prem Kumar Badri (20)

PPTX
Module 15 attributes
Prem Kumar Badri
 
PPTX
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
PPTX
Module 13 operators, delegates, and events
Prem Kumar Badri
 
PPTX
Module 11 : Inheritance
Prem Kumar Badri
 
PPTX
Module 10 : creating and destroying objects
Prem Kumar Badri
 
PPTX
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
PPTX
Module 7 : Arrays
Prem Kumar Badri
 
PPTX
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
PPTX
Module 5 : Statements & Exceptions
Prem Kumar Badri
 
PPTX
Module 4 : methods & parameters
Prem Kumar Badri
 
PPTX
Module 3 : using value type variables
Prem Kumar Badri
 
PPTX
Module 2: Overview of c#
Prem Kumar Badri
 
PPTX
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
PPTX
C# Non generics collection
Prem Kumar Badri
 
PPTX
C# Multi threading
Prem Kumar Badri
 
PPT
C# Method overloading
Prem Kumar Badri
 
PPTX
C# Inheritance
Prem Kumar Badri
 
PPTX
C# Generic collections
Prem Kumar Badri
 
PPTX
C# Global Assembly Cache
Prem Kumar Badri
 
PPTX
C# Filtering in generic collections
Prem Kumar Badri
 
Module 15 attributes
Prem Kumar Badri
 
Module 12 aggregation, namespaces, and advanced scope
Prem Kumar Badri
 
Module 13 operators, delegates, and events
Prem Kumar Badri
 
Module 11 : Inheritance
Prem Kumar Badri
 
Module 10 : creating and destroying objects
Prem Kumar Badri
 
Module 8 : Implementing collections and generics
Prem Kumar Badri
 
Module 7 : Arrays
Prem Kumar Badri
 
Module 6 : Essentials of Object Oriented Programming
Prem Kumar Badri
 
Module 5 : Statements & Exceptions
Prem Kumar Badri
 
Module 4 : methods & parameters
Prem Kumar Badri
 
Module 3 : using value type variables
Prem Kumar Badri
 
Module 2: Overview of c#
Prem Kumar Badri
 
Module 1 : Overview of the Microsoft .NET Platform
Prem Kumar Badri
 
C# Non generics collection
Prem Kumar Badri
 
C# Multi threading
Prem Kumar Badri
 
C# Method overloading
Prem Kumar Badri
 
C# Inheritance
Prem Kumar Badri
 
C# Generic collections
Prem Kumar Badri
 
C# Global Assembly Cache
Prem Kumar Badri
 
C# Filtering in generic collections
Prem Kumar Badri
 
Ad

Recently uploaded (20)

PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PDF
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
PPTX
CDH. pptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
PPTX
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
Biological Classification Class 11th NCERT CBSE NEET.pdf
NehaRohtagi1
 
CDH. pptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
How to Track Skills & Contracts Using Odoo 18 Employee
Celine George
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Artificial Intelligence in Gastroentrology: Advancements and Future Presprec...
AyanHossain
 
Information Texts_Infographic on Forgetting Curve.pptx
Tata Sevilla
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
20250924 Navigating the Future: How to tell the difference between an emergen...
McGuinness Institute
 

Module 14 properties and indexers

  • 3. Using Properties  Why Use Properties?  Using Accessors  Comparing Properties to Fields  Comparing Properties to Methods  Property Types  Property Example
  • 4. Why Use Properties?  Properties provide:  A useful way to encapsulate information inside a class  Concise syntax  Flexibility
  • 5. 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 }
  • 6. 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
  • 7. 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
  • 8. 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
  • 9. Property Example public class Console { public static TextReader In { get { if (reader == null) { reader = new StreamReader(...); } return reader; } } ... private static TextReader reader = null; }
  • 10. Using Indexers  What Is an Indexer?  Comparing Indexers to Arrays  Comparing Indexers to Properties  Using Parameters to Define Indexers  String Example  BitArray Example
  • 11. 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
  • 12. 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
  • 13. 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
  • 14. 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
  • 15. 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 < 0 || index >= Length) throw new IndexOutOfRangeException( ); ... } } ... }
  • 16. 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; }