Generics Ashima Wadhwa. What are generics Generics were added by C# 2.0 the term generics means parameterized types. Using generics, you can define a.

Slides:



Advertisements
Similar presentations
Template. 2 Using templates, it is possible to create generic functions and classes. In a generic function or class, the type of data upon which the function.
Advertisements

CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
תכנות ב C#. דוגמא לפלט using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void.
 2006 Pearson Education, Inc. All rights reserved Generics Many slides modified by Prof. L. Lilien (even many without an explicit message). Slides.
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 9: Pass-by-Value.
1 Chapter 21 Generics. 2 Objectives F To know the benefits of generics (§21.1). F To use generic classes and interfaces (§21.2). F To declare generic.
GTECH 731 Lab Session 4 Lab 3 Review, Lab 4 Intro 9/28/10 Lab 3 Review Lab 4 Overview.
 2006 Pearson Education, Inc. All rights reserved Generics.
CSE 332: C++ templates and generic programming I Motivation for Generic Programming in C++ We’ve looked at procedural programming –Reuse of code by packaging.
Intro to Generic Programming Templates and Vectors.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
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. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
Java Generics.
Copyright © 2006 Thomas P. Skinner1 Chapter 5 Indexers, Interfaces, and Enumerators.
Chapter 21 Generics 1. Generics - Overview Generic Methods specify a set of related methods Generic classes specify a set of related types Software reuse.
Methods Session 04 Mata kuliah: M0874 – Programming II Tahun: 2010.
Data Types in Java James Burns. Recitation Name some characteristics of objects Name some characteristics of objects Chemical Bank Chemical Bank Describe.
CSE 332: C++ Type Programming: Associated Types, Typedefs and Traits A General Look at Type Programming in C++ Associated types (the idea) –Let you associate.
Delegates Programming in C# Delegates CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis.
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.
Keith Elder Microsoft MVP
Features of Object Oriented Programming:  A class is a collection of things which posses common similarities.  In C#.NET a class is a user defined.
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
4-Methods Dr. John P. Abraham Professor UTPA. Common ways of packaging code Properties Methods Classes Namespaces (related classes are grouped into a.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
Chapter 7 Templates. Objectives Introduction Function Templates Class Templates Standard Template Library.
CSE 332: C++ template examples Concepts and Models Templates impose requirements on type parameters –Types that are plugged in must meet those requirements.
Methods SWE 344 Internet Protocols & Client Server Programming.
Generic Instructor : Sarah Alodan. Problem?! Java code used to look like this: public class Courses { public static void main(String[] args) { ArrayList.
Object Oriented Programming Generic Collections and LINQ Dr. Mike Spann
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Generics.
Generic(Parameterized ) types Mehdi Einali Advanced Programming in Java 1.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
Chapter 4 Generic Vector Class. Agenda A systemic problem with Vector of Object – Several approaches at a solution – Generic structures Converting classes.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
BİL527 – Bilgisayar Programlama I Functions 1. Contents Functions Delegates 2.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
IAP C# 2011 Lecture 2: Delegates, Lambdas, LINQ Geza Kovacs.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
Generics & Collection Classes Version 1.0. Topics Generic Methods and Classes Generic Collection Classes List Enumerators Queue Stack LinkedList.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
C# - Inheritance Ashima Wadhwa. Inheritance One of the most important concepts in object- oriented programming is inheritance. Inheritance allows us to.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Enum,Structure and Nullable Types Ashima Wadhwa. Enumerations, Enumerations, or enums, are used to group named constants similar to how they are used.
Lecture 11: Generics. The Generic List loading data from a file using System.IO; : namespace EmpListDemo { static class Program { static void Main(string[]
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
CSE 332: C++ templates and generic programming II Review: Concepts and Models Templates impose requirements on type parameters –Types that are plugged.
Chapter  Array-like data structures  ArrayList  Queue  Stack  Hashtable  SortedList  Offer programming convenience for specific access.
OOP Tirgul 10. What We’ll Be Seeing Today  Generics – A Reminder  Type Safety  Bounded Type Parameters  Generic Methods  Generics and Inner Classes.
Staples are our staple Building upon our solution.
Motivation for Generic Programming in C++
Programming with ANSI C ++
התוכנית: using System; using System.Collections.Generic;
C# Arrays.
Generic(Parameterized ) Types
Introduction to Custom Templates
CS212: Object Oriented Analysis and Design
Functions Used to write code only once Can use parameters.
عرض اجمالي المهام الشرطية في سي شارب (الأمر if)
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Chapter 19 Generics Jung Soo (Sue) Lim Cal State LA.
Presentation transcript:

Generics Ashima Wadhwa

What are generics Generics were added by C# 2.0 the term generics means parameterized types. Using generics, you can define a solution once, independently of any specific type of data, and then apply that solution to a wide variety of data types without any additional effort.

Parameterized types Parameterized types are important because they enable you to create – classes, structures, interfaces, methods, and delegates in which the type of data upon which they operate is specified as a parameter. A class, structure, interface, method, or delegate that operates on a parameterized type is called a generic. For eg. generic class or generic method

What was used before generics C# 1.0 had the ability to create generalized code by operating through references of type object. Since Object is the base class of all other classes, an object reference can refer to any type of object. Thus, in pre-generics code, generalized code used object references to operate on a variety of different kinds of objects.

Need for generics Generalized code using object references did not provide type safety because casts were needed to convert between the object type and the actual type of the data. This was a potential source of errors because it was possible to accidentally use an incorrect cast. Generics avoid this problem by providing the type safety that was lacking. Generics also streamline the process because it is no longer necessary to employ casts to translate between object and the type of data that is actually being operated upon. Thus, generics expand your ability to re-use code, and let you do so safely and easily.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class MyStack { T[] StackArray; int StackPointer = 0; public void Push(T x) { if ( !IsStackFull) StackArray[StackPointer++]=x; } public T Pop() { return (!IsStackempty) ? StackArray[--StackPointer] : StackArray[0]; }

const int Maxstack =10; bool IsStackFull { get { return StackPointer >= Maxstack; } bool IsStackempty { get { return StackPointer <=0; } public MyStack() { StackArray = new T[Maxstack]; } public void Print() { for(int i = StackPointer-1 ; i>=0; i--) Console.WriteLine(" Value:{0}",StackArray[i]); }

class Program { static void Main() { MyStack StackInt = new MyStack (); MyStack StackString = new MyStack (); StackInt.Push(3); StackInt.Push(63); StackInt.Push(73); StackInt.Push(34); StackInt.Push(30); StackInt.Print(); StackString.Push("IN GIBS"); StackString.Push("learning C#"); StackString.Push("We MCA3 Students"); StackString.Print(); }

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class simple { static public void ReverseAndPrint (T[] arr) { Array.Reverse(arr); foreach (T item in arr) Console.WriteLine("{0}", item.ToString()); Console.WriteLine(" "); }

class Program { static void Main() { var intArray = new int[] { 1, 3, 2, 4, 9 }; var stringArray = new string[] { "first","second", "third" }; var doublearray = new double[] { 8.78,8.96,2.34 }; simple.ReverseAndPrint (intArray); simple.ReverseAndPrint (stringArray); simple.ReverseAndPrint (doublearray); }

interface IMyIfc { T ReturnIt(T invalue); } class Simple : IMyIfc, IMyIfc { public int ReturnIt(int invalue) { return invalue; } public string ReturnIt(string invalue) { return invalue; } class Program { static void Main() { Simple trivial = new Simple(); Console.WriteLine("{0}", trivial.ReturnIt(5)); Console.WriteLine("{0}",trivial.ReturnIt("Hi there")); }

Queries?