Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type."— Presentation transcript:

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

2 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

3 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

4 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

5 Generics & Anonymous & Partial classes & Nullable / Session 8 / 5 of 26 Creating generic Type  A generic declaration always accepts a type parameter

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

7 Generics & Anonymous & Partial classes & Nullable / Session 8 / 7 of 26 Generic method  Virtual  Override  abstract

8 Generics & Anonymous & Partial classes & Nullable / Session 8 / 8 of 26 Generic interface  Syntax declaration  Example

9 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.

10 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.

11 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

12 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.

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

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

15 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.

16 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

17 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

18 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

19 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…

20 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

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

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

23 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

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

25 Generics & Anonymous & Partial classes & Nullable / Session 8 / 25 of 26 When use nullable type?

26 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.


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

Similar presentations


Ads by Google