Propeties, Indexers, Generics and Collections
Properties
screen position using structure
public data violates golden rule of encapsulation
Public fields are syntactically cleaner, shorter and easier than methods
Unfortunately they break encapsulation
-between field and method
-looks like field, acts like method
syntax:
Accessmodifier Type PropertyName
{
get
{
//read code
}
set
{
//write code
}
}
property accessibility: public, private, protected
accessor accessibility: public, private,protected
Rules for accessor accesibility:
-only one accessor at a time
-less restrictive than prperty
- can assign value of property of a structure/class only after structure/class has been initialized
- cant use ref or out with property
- contains atmost one get and set accessor methods
- get and set accessors cannot take parameters
- const properties do not exist
Interface properties:
-class/struct can implement properties
- class implementing properties can mark it virtual ma derived class to override
-automatic properties generated by compiler
-automatic read-only properties
-no automatic write-only
properties-smart fields
indexers-smart arrays
3 bitwise expressions:
- (bits & (1<<5))!=0
- bits &= ~ (1<<5)
- bits |= (1<<5)
Indexers vs Arrays
- nonnumeric subscripts
- overloading
- cannot be used with ref or out
Indexers in Interfaces
Generics vs Generalized classes
- Generics take type parameters, Generalized classes takes objects
- casting issues
- instantiation issues
- multiple parameters
E.g 1,5,-2,1,6
Variances
I<A>!=I<B> even if
A is derived from B
-Contravariant Interfaces:
-I<A>=I<B> as long as there is valid conversion from A to B
- use of in qualifier
- used for method parameters
Covariant Interfaces- I<A>=I<B> as long
as there is a valid conversion from A to B
- use of out qualifier with T
- can be used for only for return values
Generic Interfaces
Using Collections
- collections are generic types
- different methods for insertion, deletion
List<T>
- accessed by index
- additional methods to search and sort
Queue<T>
- first-in-first-out
- examine without removing
Stack<T>
- Last-in-First-out
-Peek method
LinkedList<T>
- double ended list
-supports random access also
HashSet<T>
- fast retrieval of data
- set oriented methods - subset, union and intersection
Dictionary<TKey,TValue>
- retrieved using keys
SortedList<TKey,TValue>
- sorted lst of key/value pairs
List Collection
Class
Limitations of Arrays
- resizing array - new copy, update references
- remove an element - reorder
- insert an element, reorder
Features of List<T>
- no size specification
- Remove() and RemoveAt()
- Add()
- Insert()
-Sort()
- dont worry about resizing or reordering as in arrays
LinkedList<T>
-double ended list
- AddFirst()
-AddLast()
-AddBefore()
-AddAfter()
-Remove()
-RemoveFirst()
-RemoveLast()
- First
-Last
-Previous
-Next
- Dictionary<TKey,Tval>
- Add()
- square bracket [ ]
- KeyValuePair<TKey,Tval> in foreach
-SortedList<TKey,Tval>
-same as Dictionary except keys are always sorted
- more time consuming
-Add(),[ ], KeyValuePair<TKey,Tval>
-HashSet<T>
- faster retrieval
-internal hashing function
-destructive operations:
IntersectWith, UnionWith,ExceptWith
-non-destructive operations:
IsSubsetOf, IsSupersetOf
- FInd method used in LinkedList<T>
- no subscript support
Predicate
Lambda expressions
Also provided
FindLast
FindAll
forms of lambda expressions:
x=>x*x
x=>{return x*x;}
(int x)=>x/2
()=>display();
(x,y)=>{x++;return x/y;}
(ref int x,int y)=>{x++; return x/y;}
Arrays v/s Collections
-resizing
-number of dimensions
- index support
- ToArray() and constructor support
Indexers
Write Only
Introducing Generics