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

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Data Structures and Collections
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
1 Generic Collections Chapter Objectives You will be able to Use generic collection classes available in the.NET framework.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Road Map Introduction to object oriented programming. Classes
C# Programming: From Problem Analysis to Program Design1 Advanced Object-Oriented Programming Features C# Programming: From Problem Analysis to Program.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
ASP.NET Programming with C# and SQL Server First Edition
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
2000 Jordan Anastasiade. All rights reserved. 1 Class In this lesson you will be learning about: Class. Inheritance. Polymorphism. Nested and.
The Java Programming Language
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
CIS 3301 C# Lesson 5 Methods. CIS 3302 Objectives Understand the structure of a method. Know the difference between static and instance methods. Learn.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
C# D1 CSC 298 Elements of C# code (part 2). C# D2 Writing a class (or a struct)  Similarly to Java or C++  Fields: to hold the class data  Methods:
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
C#.Net Development Version 1.0. Overview Nullable Datatype Description ? HasValue Lifted Conversions null coalescing operator ?? Partial Classes Copyright.
Keith Elder Microsoft MVP
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
Copyright © 2010 Certification Partners, LLC -- All Rights Reserved Perl Specialist.
Generics Collections. Why do we need Generics? Another method of software re-use. When we implement an algorithm, we want to re-use it for different types.
C# 2.0 and Future Directions Anders Hejlsberg Technical Fellow Microsoft Corporation.
Session 04 Module 8: Abstract classes and Interface Module 9: Properties and Indexers.
[ISRAR ALI] Hammad Khan. The namespace keyword is used to declare a scope. Making software components reusable can result in naming collisions (two classes.
Advanced C# Types Tom Roeder CS fa. From last time out parameters difference is that the callee is required to assign it before returning not the.
Copyright © 2003 ProsoftTraining. All rights reserved. Perl Fundamentals.
Interface: (e.g. IDictionary) Specification class Appl{ ---- IDictionary dic; dic= new XXX(); application class: Dictionary SortedDictionary ----
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
Introduction to C# 2.0 An Advanced Look Adam Calderon Principal Engineer - Interknowlogy Microsoft MVP – C#
PROGRAMMING IN C#. Collection Classes (C# Programming Guide) The.NET Framework provides specialized classes for data storage and retrieval. These classes.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
Session 07 Module 13 - Collections. Collections / Session 7 / 2 of 32 Review  A delegate in C# is used to refer to a method in a safe manner.  To invoke.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
Interfaces and Inner Classes
Lecture 10: Generics & Basic Desktop Programming Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Data Structures and Collections Principles.NET: –Two libraries: System.Collections System.Collections.Generics FEN 2014UCN Teknologi/act2learn1 Deprecated.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Topics Instance variables, set and get methods Encapsulation
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Features of.net Language independent Object oriented program Multi threading Exception handling Drag and drop Linq
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
C# for C++ Programmers 1.
Static data members Constructors and Destructors
Objectives Describe anonymous methods Define extension methods
Methods Attributes Method Modifiers ‘static’
Chapter 5: Programming with C#
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Interface.
Java Programming Language
CIS 199 Final Review.
Class.
Advanced .NET Programming I 3rd Lecture
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

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.