0% found this document useful (0 votes)
11 views

Indexer S

Properties and indexers provide ways to encapsulate data and access functionality in classes. Properties use get and set accessors to provide field-like access, allowing computed values. Indexers provide array-like access to objects and can be overloaded, while properties cannot. Both use syntax similar to arrays but have important differences like not representing storage locations. Examples show how strings and bit arrays can use indexers to provide non-integer indexed access to their data.

Uploaded by

drakefeel408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Indexer S

Properties and indexers provide ways to encapsulate data and access functionality in classes. Properties use get and set accessors to provide field-like access, allowing computed values. Indexers provide array-like access to objects and can be overloaded, while properties cannot. Both use syntax similar to arrays but have important differences like not representing storage locations. Examples show how strings and bit arrays can use indexers to provide non-integer indexed access to their data.

Uploaded by

drakefeel408
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

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
class Button
Button
{{
public
public string
string Caption
Caption //// Property
Property
{{
get
get {{ return
return caption;
caption; }}
set
set {{ caption
caption == value;
value; }}
}}
private
private string
string caption;
caption; //// Field
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
public class
class Console
Console
{{
public
public static
static TextReader
TextReader In In
{{
get
get {{
if
if (reader
(reader ==
== null)
null) {{
reader
reader == new
new StreamReader(...);
StreamReader(...);
}}
return
return reader;
reader;
}}
}}
...
...
private
private static
static TextReader
TextReader reader
reader == null;
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
class String
String
{{
public
public char
char this[int
this[int index]
index]
{{
get
get {{
if
if (index
(index << 00 ||
|| index
index >=
>= Length)
Length)
throw
throw new
new IndexOutOfRangeException(
IndexOutOfRangeException( );
);
...
...
}}
}}
...
...
}}
BitArray Example

class
class BitArray
BitArray
{{
public
public bool
bool this[int
this[int index]
index]
{{
get
get {{
BoundsCheck(index);
BoundsCheck(index);
return
return (bits[index
(bits[index >>>> 5]
5] && (1
(1 <<
<< index))
index)) !=
!= 0;
0;
}}
set
set {{
BoundsCheck(index);
BoundsCheck(index);
if
if (value)
(value) {{
bits[index
bits[index >>>> 5]
5] |=
|= (1
(1 <<
<< index);
index);
}} else {
else {
bits[index
bits[index >>>> 5]
5] &=
&= ~(1
~(1 <<
<< index);
index);
}}
}}
}}
private
private int[
int[ ]] bits;
bits;
}}
Lab 13.1: Using Properties and Indexers
Review

 Using Properties
 Using Indexers

You might also like