SlideShare une entreprise Scribd logo
1  sur  14
 
C# Generics
C# Generics ,[object Object],[object Object],[object Object],[object Object],[object Object]
Overview ,[object Object],[object Object],[object Object]
Life without Generics public   class   ExampleStack { private   object [] contents =  new   object [99]; private   int  ptr ; public   void  Push( object  o) { contents[++ptr] = o;  } public   object  Pop() { return  contents[ptr--]; } } What can go wrong ? ExampleStack  stack2 =  new   ExampleStack (); stack2.Push( "A" ); stack2.Push( "B" ); stack2.Push( "C" ); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop());
Life without Generics public   class   ExampleStack { private   object [] contents =  new   object [99]; private   int  ptr ; public   void  Push( object  o) { contents[++ptr] = o;  } public   object  Pop() { return  contents[ptr--]; } } What can go wrong ? ExampleStack  stack2 =  new   ExampleStack (); stack2.Push( "A" ); stack2.Push( null ); stack2.Push( 12.7 ); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop());
Life with Generics public   class   ExampleStack <T> { private  T[] contents =  new  T[99]; private   int  ptr; public   void  Push(T o) { contents[++ptr] = o; } public  T Pop() {  return  contents[ptr--]; } } What can go wrong ? ExampleStack < int > stack1 =  new   ExampleStack < int >(); stack1.Push(32); stack1.Push(99); stack1.Push(101); // Wont compile // stack1.Push(&quot;string &quot;); // stack1.Push('a'); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop());
Life with Generics public   class   ExampleStack <T> { private  T[] contents =  new  T[99]; private   int  ptr; public   void  Push(T o) { contents[++ptr] = o; } public  T Pop() {  return  contents[ptr--]; } } ,[object Object],[object Object],[object Object],[object Object],[object Object]
The 'Where' Clause  public   class   ExampleStack <T> where  T: IComparable { private  T[] contents =  new  T[99]; private   int  ptr; public   void  Push(T o) { contents[++ptr] = o; } public  T Pop() {  return  contents[ptr--]; } } Similar to SQL 'where' Enforces compile time checks on the generic May be Interfaces May enforce constructors E.G. new().new(int)
Multiple Generics  public   class   ExampleStack <Tinterface, Tclass>  where  Tclass : Tinterface  where  Tinterface : ICloneable  { private  Tclass[] contents =  new  Tclass[99]; private   int  ptr; public   void  Push(Tclass o) { contents[++ptr] = o; } public  Tinterface Pop() { return  contents[ptr--]; }  } Any number of Generic classes can be added ExampleStack < ICloneable ,  string > stack1 = new   ExampleStack < ICloneable ,  string >(); stack1.Push( &quot;Some String&quot; ); ICloneable  clone = stack1.Pop();
Generics are not just for classes!  public   interface   Interface <T>  { bool  DoWork(T workItem); } public   interface   InterfaceString  :  Interface < string >  { // now has method  // bool DoWork(string workItem); } public   class   Implemetation  :  InterfaceString { public   bool  DoWork( string  workItem) { // Do work return   false ;  }  } Generics are not just for classes  Interfaces  Methods  NOT Properties and Fields directly
Generics are not just for classes!  public   class   Example  { public  T GenericMethod<T>(T obj1 , T obj2)  where  T: IComparable { if  (obj1.CompareTo(obj2) > 0) return  obj1; else return  obj2;  } } Generics are not just for classes  Interfaces  Methods  NOT Properties and Fields directly
Native Generics  ,[object Object],[object Object],[object Object],[object Object],[object Object],Used throughout C# to enforce type safety
Summary ,[object Object],[object Object],[object Object]

Contenu connexe

Tendances

From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modelingMario Fusco
 
Java Generics
Java GenericsJava Generics
Java Genericsjeslie
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2Hariz Mustafa
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerMario Fusco
 
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...tdc-globalcode
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language InnovationsShahriar Hyder
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-libraryHariz Mustafa
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5Mahmoud Ouf
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manualsameer farooq
 
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and PitfallsJava Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and PitfallsRakesh Waghela
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongMario Fusco
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsMuhammadTalha436
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)RichardWarburton
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8Christian Nagel
 

Tendances (20)

Java Generics
Java GenericsJava Generics
Java Generics
 
From object oriented to functional domain modeling
From object oriented to functional domain modelingFrom object oriented to functional domain modeling
From object oriented to functional domain modeling
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
 
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better ProgrammerOOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
 
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
TDC2016POA | Trilha .NET - C# como você nunca viu: conceitos avançados de pro...
 
TechTalk - Dotnet
TechTalk - DotnetTechTalk - Dotnet
TechTalk - Dotnet
 
Class method
Class methodClass method
Class method
 
Functional Programming with C#
Functional Programming with C#Functional Programming with C#
Functional Programming with C#
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
 
Lecture11 standard template-library
Lecture11 standard template-libraryLecture11 standard template-library
Lecture11 standard template-library
 
Intake 38 data access 5
Intake 38 data access 5Intake 38 data access 5
Intake 38 data access 5
 
Java programming lab manual
Java programming lab manualJava programming lab manual
Java programming lab manual
 
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and PitfallsJava Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and Pitfalls
 
If You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are WrongIf You Think You Can Stay Away from Functional Programming, You Are Wrong
If You Think You Can Stay Away from Functional Programming, You Are Wrong
 
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ ExamsObject Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
 
Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
 
C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8C# 7.x What's new and what's coming with C# 8
C# 7.x What's new and what's coming with C# 8
 
Generics C#
Generics C#Generics C#
Generics C#
 

Similaire à Generics

The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Languageintelliyole
 
Java interface and inheritance
Java interface and inheritanceJava interface and inheritance
Java interface and inheritanceJaromirJagr
 
Templates presentation
Templates presentationTemplates presentation
Templates presentationmalaybpramanik
 
Effective Java - Still Effective After All These Years
Effective Java - Still Effective After All These YearsEffective Java - Still Effective After All These Years
Effective Java - Still Effective After All These YearsMarakana Inc.
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8ecomputernotes
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaJevgeni Kabanov
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxDIPESH30
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003R696
 
Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Peter Antman
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Featurestarun308
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpg_hemanth17
 

Similaire à Generics (20)

The Kotlin Programming Language
The Kotlin Programming LanguageThe Kotlin Programming Language
The Kotlin Programming Language
 
Java interface and inheritance
Java interface and inheritanceJava interface and inheritance
Java interface and inheritance
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Effective Java - Still Effective After All These Years
Effective Java - Still Effective After All These YearsEffective Java - Still Effective After All These Years
Effective Java - Still Effective After All These Years
 
computer notes - Data Structures - 8
computer notes - Data Structures - 8computer notes - Data Structures - 8
computer notes - Data Structures - 8
 
Templates2
Templates2Templates2
Templates2
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
14 thread
14 thread14 thread
14 thread
 
Templates
TemplatesTemplates
Templates
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Embedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for JavaEmbedded Typesafe Domain Specific Languages for Java
Embedded Typesafe Domain Specific Languages for Java
 
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docxlab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
lab08build.bat@echo offclsset DRIVE_LETTER=1s.docx
 
C chap22
C chap22C chap22
C chap22
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 
Thread
ThreadThread
Thread
 
Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)Java 1.5 - whats new and modern patterns (2007)
Java 1.5 - whats new and modern patterns (2007)
 
Java New Programming Features
Java New Programming FeaturesJava New Programming Features
Java New Programming Features
 
Lezione03
Lezione03Lezione03
Lezione03
 
Lezione03
Lezione03Lezione03
Lezione03
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 

Plus de Simon Smith

There are only 3 operations in a web app
There are only 3 operations in a web appThere are only 3 operations in a web app
There are only 3 operations in a web appSimon Smith
 
Producing Quality Software
Producing Quality SoftwareProducing Quality Software
Producing Quality SoftwareSimon Smith
 
Implementing Soa
Implementing SoaImplementing Soa
Implementing SoaSimon Smith
 

Plus de Simon Smith (7)

Bootstrap
BootstrapBootstrap
Bootstrap
 
rrr
rrrrrr
rrr
 
There are only 3 operations in a web app
There are only 3 operations in a web appThere are only 3 operations in a web app
There are only 3 operations in a web app
 
tilemanager
tilemanagertilemanager
tilemanager
 
Producing Quality Software
Producing Quality SoftwareProducing Quality Software
Producing Quality Software
 
N Tiering
N TieringN Tiering
N Tiering
 
Implementing Soa
Implementing SoaImplementing Soa
Implementing Soa
 

Generics

  • 1.  
  • 3.
  • 4.
  • 5. Life without Generics public class ExampleStack { private object [] contents = new object [99]; private int ptr ; public void Push( object o) { contents[++ptr] = o; } public object Pop() { return contents[ptr--]; } } What can go wrong ? ExampleStack stack2 = new ExampleStack (); stack2.Push( &quot;A&quot; ); stack2.Push( &quot;B&quot; ); stack2.Push( &quot;C&quot; ); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop());
  • 6. Life without Generics public class ExampleStack { private object [] contents = new object [99]; private int ptr ; public void Push( object o) { contents[++ptr] = o; } public object Pop() { return contents[ptr--]; } } What can go wrong ? ExampleStack stack2 = new ExampleStack (); stack2.Push( &quot;A&quot; ); stack2.Push( null ); stack2.Push( 12.7 ); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop()); Console .WriteLine(stack2.Pop());
  • 7. Life with Generics public class ExampleStack <T> { private T[] contents = new T[99]; private int ptr; public void Push(T o) { contents[++ptr] = o; } public T Pop() { return contents[ptr--]; } } What can go wrong ? ExampleStack < int > stack1 = new ExampleStack < int >(); stack1.Push(32); stack1.Push(99); stack1.Push(101); // Wont compile // stack1.Push(&quot;string &quot;); // stack1.Push('a'); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop()); Console .WriteLine(stack1.Pop());
  • 8.
  • 9. The 'Where' Clause public class ExampleStack <T> where T: IComparable { private T[] contents = new T[99]; private int ptr; public void Push(T o) { contents[++ptr] = o; } public T Pop() { return contents[ptr--]; } } Similar to SQL 'where' Enforces compile time checks on the generic May be Interfaces May enforce constructors E.G. new().new(int)
  • 10. Multiple Generics public class ExampleStack <Tinterface, Tclass> where Tclass : Tinterface where Tinterface : ICloneable { private Tclass[] contents = new Tclass[99]; private int ptr; public void Push(Tclass o) { contents[++ptr] = o; } public Tinterface Pop() { return contents[ptr--]; } } Any number of Generic classes can be added ExampleStack < ICloneable , string > stack1 = new ExampleStack < ICloneable , string >(); stack1.Push( &quot;Some String&quot; ); ICloneable clone = stack1.Pop();
  • 11. Generics are not just for classes! public interface Interface <T> { bool DoWork(T workItem); } public interface InterfaceString : Interface < string > { // now has method // bool DoWork(string workItem); } public class Implemetation : InterfaceString { public bool DoWork( string workItem) { // Do work return false ; } } Generics are not just for classes Interfaces Methods NOT Properties and Fields directly
  • 12. Generics are not just for classes! public class Example { public T GenericMethod<T>(T obj1 , T obj2) where T: IComparable { if (obj1.CompareTo(obj2) > 0) return obj1; else return obj2; } } Generics are not just for classes Interfaces Methods NOT Properties and Fields directly
  • 13.
  • 14.