Top Banner
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type
26

Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Jan 03, 2016

Download

Documents

Godwin Terry
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Session 08

Module 14: Generics and Iterator

Module 15: Anonymous & partial class & Nullable type

Page 2: 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

Page 3: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 4: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 5: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Generics & Anonymous & Partial classes & Nullable / Session 8 / 5 of 26

Creating generic Type

A generic declaration always accepts a type parameter

Page 6: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Generics & Anonymous & Partial classes & Nullable / Session 8 / 6 of 26

Benefit

Page 7: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Generics & Anonymous & Partial classes & Nullable / Session 8 / 7 of 26

Generic method

Virtual Override abstract

Page 8: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Generics & Anonymous & Partial classes & Nullable / Session 8 / 8 of 26

Generic interface

Syntax declaration Example

Page 9: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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.

Page 10: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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.

Page 11: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 12: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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.

Page 13: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Generics & Anonymous & Partial classes & Nullable / Session 8 / 13 of 26

Creating anonymous method

Page 14: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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 +=

Page 15: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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.

Page 16: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 17: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 18: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 19: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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…

Page 20: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 21: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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";}

Page 22: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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];

}

Page 23: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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

Page 24: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

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 nullstatic 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();}

Page 25: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.

Generics & Anonymous & Partial classes & Nullable / Session 8 / 25 of 26

When use nullable type?

Page 26: Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & 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.