際際滷

際際滷Share a Scribd company logo
油
C# Generics
C# Generics Pre-requisites C# OO  Interfaces Stack data structure
Overview  Generic classes and methods  defer the specification of one or more  types until declaration by client code
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--]; } } 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()); This is type safe. Compile time errors not run time errors Faster Execution!
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  Namespace System.Collections.Generic List <T> :  IList <T>,  ICollection <T>,  IEnumerable <T> Used throughout C# to enforce type safety
Summary  Generic classes and methods  defer the specification of one or more  types until declaration by client code
Ad

Recommended

Generics in .NET, C++ and Java
Generics in .NET, C++ and Java
Sasha Goldshtein
C# Generics
C# Generics
Rohit Vipin Mathews
Generics collections
Generics collections
Yaswanth Babu Gummadivelli
Java generics
Java generics
Hosein Zare
C# p9
C# p9
Renas Rekany
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Mario Fusco
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
Mario Fusco
Intake 38 5
Intake 38 5
Mahmoud Ouf
Java Generics
Java Generics
Z端lfikar Karakaya
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
Java Generics
Java Generics
jeslie
Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario 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...
tdc-globalcode
TechTalk - Dotnet
TechTalk - Dotnet
heinrich.wendel
Class method
Class method
kamal kotecha
Functional Programming with C#
Functional Programming with C#
EastBanc Tachnologies
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
C# 3.0 Language Innovations
C# 3.0 Language Innovations
Shahriar Hyder
Lecture11 standard template-library
Lecture11 standard template-library
Hariz Mustafa
Intake 38 data access 5
Intake 38 data access 5
Mahmoud Ouf
Java programming lab manual
Java programming lab manual
sameer farooq
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and Pitfalls
Rakesh 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 Wrong
Mario Fusco
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
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# 8
Christian Nagel
Generics C#
Generics C#
Raghuveer Guthikonda
Csharp4 generics
Csharp4 generics
Abed Bukhari
Generics
Generics
Iblesoft

More Related Content

What's hot (20)

Java Generics
Java Generics
Z端lfikar Karakaya
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
Java Generics
Java Generics
jeslie
Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario 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...
tdc-globalcode
TechTalk - Dotnet
TechTalk - Dotnet
heinrich.wendel
Class method
Class method
kamal kotecha
Functional Programming with C#
Functional Programming with C#
EastBanc Tachnologies
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
C# 3.0 Language Innovations
C# 3.0 Language Innovations
Shahriar Hyder
Lecture11 standard template-library
Lecture11 standard template-library
Hariz Mustafa
Intake 38 data access 5
Intake 38 data access 5
Mahmoud Ouf
Java programming lab manual
Java programming lab manual
sameer farooq
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and Pitfalls
Rakesh 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 Wrong
Mario Fusco
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
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# 8
Christian Nagel
Generics C#
Generics C#
Raghuveer Guthikonda
From object oriented to functional domain modeling
From object oriented to functional domain modeling
Mario Fusco
Java Generics
Java Generics
jeslie
Lecture02 class -_templatev2
Lecture02 class -_templatev2
Hariz Mustafa
OOP and FP - Become a Better Programmer
OOP and FP - Become a Better Programmer
Mario 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...
tdc-globalcode
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
ROHIT JAISWAR
C# 3.0 Language Innovations
C# 3.0 Language Innovations
Shahriar Hyder
Lecture11 standard template-library
Lecture11 standard template-library
Hariz Mustafa
Intake 38 data access 5
Intake 38 data access 5
Mahmoud Ouf
Java programming lab manual
Java programming lab manual
sameer farooq
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and Pitfalls
Rakesh 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 Wrong
Mario Fusco
Object Oriented Solved Practice Programs C++ Exams
Object Oriented Solved Practice Programs C++ Exams
MuhammadTalha436
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# 8
Christian Nagel

Similar to Generics (20)

Csharp4 generics
Csharp4 generics
Abed Bukhari
Generics
Generics
Iblesoft
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)
Bruce Hantover
Generic Programming &amp; Collection
Generic Programming &amp; Collection
Arya
Generic Programming &amp; Collection
Generic Programming &amp; Collection
Arya
CSharp_03_Generics_introduction_withexamples
CSharp_03_Generics_introduction_withexamples
Ranjithsingh20
PATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and Java
Michael Heron
Generics
Generics
Sankar Balasubramanian
1204csharp
1204csharp
g_hemanth17
Introduction to csharp
Introduction to csharp
Satish Verma
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
Sachin Singh
Introduction to csharp
Introduction to csharp
Raga Vahini
Introduction to csharp
Introduction to csharp
singhadarsh
Introduction to CSharp
Introduction to CSharp
Mody Farouk
Introduction To Csharp
Introduction To Csharp
g_hemanth17
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
Harshana Weerasinghe
C# Language Overview Part II
C# Language Overview Part II
Doncho Minkov
C# Generics
C# Generics
Praveen Kumar
Presentation.pptx
Presentation.pptx
PavanKumar823345
Generics In and Out
Generics In and Out
Jaliya Udagedara
Csharp4 generics
Csharp4 generics
Abed Bukhari
Generics
Generics
Iblesoft
C# quick ref (bruce 2016)
C# quick ref (bruce 2016)
Bruce Hantover
Generic Programming &amp; Collection
Generic Programming &amp; Collection
Arya
Generic Programming &amp; Collection
Generic Programming &amp; Collection
Arya
CSharp_03_Generics_introduction_withexamples
CSharp_03_Generics_introduction_withexamples
Ranjithsingh20
PATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and Java
Michael Heron
Introduction to csharp
Introduction to csharp
Satish Verma
Introduction to-csharp-1229579367461426-1
Introduction to-csharp-1229579367461426-1
Sachin Singh
Introduction to csharp
Introduction to csharp
Raga Vahini
Introduction to csharp
Introduction to csharp
singhadarsh
Introduction to CSharp
Introduction to CSharp
Mody Farouk
Introduction To Csharp
Introduction To Csharp
g_hemanth17
C# Language Overview Part II
C# Language Overview Part II
Doncho Minkov
Ad

More from Simon Smith (7)

Bootstrap
Bootstrap
Simon Smith
rrr
rrr
Simon Smith
There are only 3 operations in a web app
There are only 3 operations in a web app
Simon Smith
tilemanager
tilemanager
Simon Smith
Producing Quality Software
Producing Quality Software
Simon Smith
N Tiering
N Tiering
Simon Smith
Implementing Soa
Implementing Soa
Simon Smith
There are only 3 operations in a web app
There are only 3 operations in a web app
Simon Smith
Producing Quality Software
Producing Quality Software
Simon Smith
Implementing Soa
Implementing Soa
Simon Smith
Ad

Generics

  • 1.
  • 3. C# Generics Pre-requisites C# OO Interfaces Stack data structure
  • 4. Overview Generic classes and methods defer the specification of one or more types until declaration by client code
  • 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. 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()); This is type safe. Compile time errors not run time errors Faster Execution!
  • 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. Native Generics Namespace System.Collections.Generic List <T> : IList <T>, ICollection <T>, IEnumerable <T> Used throughout C# to enforce type safety
  • 14. Summary Generic classes and methods defer the specification of one or more types until declaration by client code