Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type
Generics & Anonymous & Partial classes & Nullable / Session 8 / 2 of 26 Review ArrayList class allow to increase and decrease the size of the collection during program execution. It allow you to store elements of different data types. Hashtable class stores elements as key and value pair where the data is organized based on the hash code. Each value in the hash table is uniquely identified by its key
Generics & Anonymous & Partial classes & Nullable / Session 8 / 3 of 26 Module 14 - Objectives Explain the concept of generics in C# Explain the System.Collections.ObjectModel namespace State the syntax of creating a generic class Describe iterators in C# Explain how to use iterators
Generics & Anonymous & Partial classes & Nullable / Session 8 / 4 of 26 Generic All classes and generic interfaces is included in namespace: System.Collections.Generic List Stack Queue Dictionary SortedDictionary LinkedList Icollection Idictionary Ienumerator IList
Generics & Anonymous & Partial classes & Nullable / Session 8 / 5 of 26 Creating generic Type A generic declaration always accepts a type parameter
Generics & Anonymous & Partial classes & Nullable / Session 8 / 6 of 26 Benefit
Generics & Anonymous & Partial classes & Nullable / Session 8 / 7 of 26 Generic method Virtual Override abstract
Generics & Anonymous & Partial classes & Nullable / Session 8 / 8 of 26 Generic interface Syntax declaration Example
Generics & Anonymous & Partial classes & Nullable / Session 8 / 9 of 26 Iterator An iterator is not a data member but it is a way of accessing the member. Iterator can be create by : implementing the GetEnumerator() Creating iterator is by creating a method whose return type of the IEnumerable interface. This call named iterator.
Generics & Anonymous & Partial classes & Nullable / Session 8 / 10 of 26 Module 14 - Summary Generics are data structures that allow to create a code for different types 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 ordered values 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 to signal the end of iteration.
Generics & Anonymous & Partial classes & Nullable / Session 8 / 11 of 26 Module 15 - Objectives Explain how to pass parameters to anonymous methods Explain how to implement partial types Explain how to implement nullable types
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 can be passed as a delegate parameter.
Generics & Anonymous & Partial classes & Nullable / Session 8 / 13 of 26 Creating anonymous method
Generics & Anonymous & Partial classes & Nullable / Session 8 / 14 of 26 Referencing Multiple Anonymous method C# allows you to create and instantiate to reference multiple anonymous methods. Using operators +=
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 method in which it is declared.
Generics & Anonymous & Partial classes & Nullable / Session 8 / 16 of 26 Passing parameters C# allow passing parameters to anonymous methods. The types of parameters that can be passed to an anonymous method is specified at the time of declaring the delegate
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 parts and 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 the existing code
Generics & Anonymous & Partial classes & Nullable / Session 8 / 18 of 26 Implementing Partial Types Partial types are implemented using the partial keyword. This keyword specifies that the code is split into multiple parts These parts are defined in multiple files and namespace
Generics & Anonymous & Partial classes & Nullable / Session 8 / 19 of 26 Partial classes A class is one types in C# that supports partial definitions. Classes can be defined over multiple location to store different members such as variables, methods…
Generics & Anonymous & Partial classes & Nullable / Session 8 / 20 of 26 Nullable Type C# 2.0 introduces a new features called nullable types to identify and handle value type field with null value. A nullable type is a mean by which null values can be defined for value type. It indicates that a variable can be have the value null Nullable types are instances of the System.Nullable structure
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"; }
Generics & Anonymous & Partial classes & Nullable / Session 8 / 22 of 26 Creating nullable Types static void LocalNullableVariables() { // Define some local nullable types using Nullable. Nullable nullableInt = 10; Nullable nullableDouble = 3.14; Nullable nullableBool = null; Nullable nullableChar = 'a'; Nullable [] arrayOfNullableInts = new int?[10]; }
Generics & Anonymous & Partial classes & Nullable / Session 8 / 23 of 26 Implementing Nullable Type HasValue property The HasValue property return a true if the value of the variable is not null, else it returns false Value property When the HasValue evaluates to true, the Value property return the value of the variable, otherwise it returns an exception
Generics & Anonymous & Partial classes & Nullable / Session 8 / 24 of 26 The ?? Operator This operator allows you to assign a value to a nullable 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(); }
Generics & Anonymous & Partial classes & Nullable / Session 8 / 25 of 26 When use nullable type?
Generics & Anonymous & Partial classes & Nullable / Session 8 / 26 of 26 Module 15 - Summary Anonymous method allow to pass block of unnamed code as a 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, struct and 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 a database.