logo_aptech_worldwide
Session 08
Module 14: Generics and Iterator
Module 15: Anonymous & partial class& Nullable type
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 2 of 26
Review
ArrayList class allow to increase and decrease thesize of the collection during program execution.
It allow you to store elements of different data types.
Hashtable class stores elements as key and valuepair where the data is organized based on the hashcode.
Each value in the hash table is uniquely identified byits key
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 3 of 26
Module 14 - Objectives
Explain the concept of generics in C#
Explain the System.Collections.ObjectModelnamespace
State the syntax of creating a generic class
Describe iterators in C#
Explain how to use iterators
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 4 of 26
Generic
All classes and generic interfaces is included in namespace:System.Collections.Generic
List<T>
Stack<T>
Queue<T>
Dictionary<K,V>
SortedDictionary<T>
LinkedList<T>
Icollection
Idictionary
Ienumerator
IList
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 5 of 26
Creating generic Type
A generic declaration always accepts a typeparameter
Image 3
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 6 of 26
Benefit
Image 4
Image 5
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 7 of 26
Generic method
Virtual
Override
abstract
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 8 of 26
Generic interface
Syntax declaration
Example
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 9 of 26
Iterator
An iterator is not a data member but it is away of accessing the member.
Iterator can be create by :
implementing the GetEnumerator()
Creating iterator is by creating a method whosereturn type of the IEnumerable interface. This callnamed iterator.
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 10 of 26
Module 14 - Summary
Generics are data structures that allow to create a code for differenttypes such as classes of interfaces.
Generic methods can be declared within generic as well as non-generic class declarations
An iterator is a block of code that returns sequentially orderedvalues of the same type.
One of way to create iterator is by using the GetEnumerator()method of the IEnumerable or IEnumerator interface.
The yeild keyword provides values to the enumerator object or tosignal the end of iteration.
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 11 of 26
Module 15 - Objectives
Explain how to pass parameters toanonymous methods
Explain how to implement partial types
Explain how to implement nullable types
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 12 of 26
Anonymous method
Anonymous method is new feature in C#
Anonymous method is an inline nameless block of code that canbe passed as delegate parameter.
Image 2
Image 3
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 13 of 26
Creating anonymous method
Image 4
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 14 of 26
Referencing Multiple Anonymous method
C# allows you to create and instantiate to referencemultiple anonymous methods.
Using operators +=
Image 0
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 15 of 26
Outer Variable in anonymous method
An anonymous method can declare variable,which called out variables.
The scope of variable only within the methodin which it is declared.
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 16 of 26
Passing parameters
C# allow passing parameters to anonymousmethods.
The types of parameters that can be passed to ananonymous method is specified at the time ofdeclaring the delegate
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 17 of 26
Partial types
Partial types are implemented using the partial keyword.
This keyword specifies that the code is split into multiple partsand these parts are defined in difference files and namespace.
They separate generator code and application code
They help in easier development and maintain of the code
They prevent programmers from accidentally modifying theexisting code
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 18 of 26
Implementing Partial Types
Partial types are implemented using the partialkeyword.
This keyword specifies that the code is split intomultiple parts
These parts are defined in multiple files andnamespace
Image 0
Image 2
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 19 of 26
Partial classes
A class is one types in C# that supports partialdefinitions.
Classes can be defined over multiple location tostore different members such as variables,methods…
Image 0
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 20 of 26
Nullable Type
C# 2.0 introduces a new features called nullable types to identifyand handle value type field with null value.
A nullable type is a mean by which null values can be defined forvalue type.
It indicates that a variable can be have the value null
Nullable types are instances of the System.Nullable structure
Image 1
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 21 of 26
Creating nullable Types
static void LocalNullableVariables()
{
// Define some local nullable types.
int? nullableInt = 10;
double? nullableDouble = 3.14;
bool? nullableBool = null;
char? nullableChar = 'a';
int?[] arrayOfNullableInts = new int?[10];
// Error! Strings are reference types!
string? s = "oops";
}
Image 2
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 22 of 26
Creating nullable Types
static void LocalNullableVariables()
{
// Define some local nullable types using Nullable<T>.
Nullable<int> nullableInt = 10;
Nullable<double> nullableDouble = 3.14;
Nullable<bool> nullableBool = null;
Nullable<char> nullableChar = 'a';
Nullable<int>[] arrayOfNullableInts = new int?[10];
}
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 23 of 26
Implementing Nullable Type
HasValue property
The HasValue  property return a true if the valueof the variable is not null, else it returns false
Value property
When the HasValue evaluates to true, the Valueproperty return the value of the variable,otherwise it returns an exception
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 24 of 26
The ?? Operator
This operator allows you to assign a value to anullable type if the retrieved value is in fact null
static void Main(string[] args)
{
Console.WriteLine("***** Fun with Nullable Data *****\n");
DatabaseReader dr = new DatabaseReader();
...
// If the value from GetIntFromDatabase() is null,
// assign local variable to 100.
int? myData = dr.GetIntFromDatabase() ?? 100;
Console.WriteLine("Value of myData: {0}", myData.Value);
Console.ReadLine();
}
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 25 of 26
When use nullable type?
Image 5
Image 7
logo_aptech_worldwide
Generics & Anonymous & Partial classes & Nullable / Session 8 / 26 of 26
Module 15 - Summary
Anonymous method allow to pass block of unnamed code asa parameter to a delegate
A single delegate instance can reference multiple
 anonymous by using the += operator
Partial type allow you to split the definitions of classes, structand interfaces to store them in difference C# files.
Nullable type allow to assign null values to the value types.This allow you to work with null values that may be return by adatabase.