GenericsCh. Vishwa MohanFreelance Software Consultant &Corporate Trainer
GenericsGenerics are similar to C++ templates. Generics has the following features: Type Safety, No boxing and unboxing when using collections for value types. No Downcasts.  Generic types can be defined in one language and used from any other .NET languages (Binary code reuse). Reduced Code bloat (typed collections).Instantiated at run-time not compile time. Work for both reference and value types. Complete run-time type information. It is not possible to assign null to generic types. In this case the keyword default can be used.
Generics Type parameter can be applied to ClassesStructuresInterfaces DelegatesYou can also apply type parameters to methods alsoType parameters can have constraints (See next slide). Some more info on Generics:T.defaultNull ChecksType Casts
Generic Class FeaturesIt is not possible to assign null to generic types. In this case the keyword default can be used. T   obj = default(T);   //Initializing to default. You can apply constraints on generic types. public MyGenericType<T> where T : IEnumerable {  ..,  } Generics supports some more constraints as follow:where T : struct //This constrain says type T must be value type. where T : class // This constrain says type T must be reference type. where T : Foo   //T is required to be derived from base class Foo. where T : new() // Specifies T must have default ctor. Constructor Constraint.where T : V   //The type T derived from a generic type V. This constraint 	            // is known as  “Naked Type Constraint”.You can also combine multiple constraints:public class MyClass<T>where T : IFoo, new()//Here T implements IFoo and have default constructor.
GenericsInheritance in Generics:  A generic type can implement a generic interface. Similarly generic class can be derived from a generic base class provided it must follow the below requirement. Generic type of the interface must be repeated or the type of the base class must be specified. Static Members: Static members of a generic class are only shared with one instantiation of the class. There is separate instance of the static variable exist for each type parameter. Generic Interfaces: Using generics you can define interfaces that defines methods with generic parameters. Generic Methods: Like C++, it is also possible to generic methods. Here generic type is define with method declaration. void Swap<T>(ref T x,   ref T y) {  …,}		Swap<int>(ref i, ref j);   //Invoking a generic method.
Generic DelegatesWith generic delegates the parameter of the delegate can be defined later. Simple delegate examples is given below: public delegate void Del<T>(T item); public delegateT2  Action<T1, T2>(T1  tVal); Nullable<T>: Suppose if you are instantiated as Nullable<int> x. The variable x can now be used like int and also you can assign null to it.Nullable<int> x; //or int? x; 		if(x.HasValue)		x = 5;					     int y = x.Value; 		x += 20; 					  x = null;EventHandler<TEventArgs>: With Windows forms, Web Applications delegates many different event handlers are defined. ArraySegment<T>:  This structure is used to represent a segment of an array. The offset and count of segment is stored in this structure. Benefit of ArraySegment is you can pass it to method as a argument. Take note, ArraySegment don’t make copy of elements of array. It just refers it.
Thank You !

More Related Content

PPTX
Type casting
PPTX
Finite state system or finite automata
PPTX
PPTX
Unusual C# - OOP
PPTX
OOP in C#
PPTX
Overview of c
PPTX
Module 3 : using value type variables
PPTX
What is identifier c programming
Type casting
Finite state system or finite automata
Unusual C# - OOP
OOP in C#
Overview of c
Module 3 : using value type variables
What is identifier c programming

What's hot (20)

PPTX
Interfaces and abstract classes
PPTX
#OOP_D_ITS - 8th - Class Diagram
PDF
Chapter23 friend-function-friend-class
PPTX
Introduction to c++
PDF
Generics
PPTX
Regular expressions for JavaScript
PPT
Friends function and_classes
PPTX
Abstract Class Presentation
PDF
Total oop in c# dot net
PPT
Constants in C Programming
PPT
Md04 flow control
PPTX
Variables in C and C++ Language
PPTX
Abstract Class & Abstract Method in Core Java
PPTX
PPTX
C keywords and identifiers
PPTX
Friend Function
PPT
C Structures And Unions
PPTX
datatypes_variables_constants
PPTX
CPU : Structures And Unions
PPTX
Lecture 2 variables
Interfaces and abstract classes
#OOP_D_ITS - 8th - Class Diagram
Chapter23 friend-function-friend-class
Introduction to c++
Generics
Regular expressions for JavaScript
Friends function and_classes
Abstract Class Presentation
Total oop in c# dot net
Constants in C Programming
Md04 flow control
Variables in C and C++ Language
Abstract Class & Abstract Method in Core Java
C keywords and identifiers
Friend Function
C Structures And Unions
datatypes_variables_constants
CPU : Structures And Unions
Lecture 2 variables
Ad

Viewers also liked (7)

PPT
Generics n delegates
PPT
Javascript
PPTX
Microsoft.net architecturte
PPT
Controls
PPT
MS SQL Server 1
PPT
Ms sql server ii
PPT
Ajaxppt
Generics n delegates
Javascript
Microsoft.net architecturte
Controls
MS SQL Server 1
Ms sql server ii
Ajaxppt
Ad

Similar to Generics (20)

PPTX
CSharp_03_Generics_introduction_withexamples
PPTX
PPTX
More Little Wonders of C#/.NET
PPT
Csharp4 generics
DOCX
unit 5.docx...............................
PPTX
Generic Programming in java
PDF
From DOT to Dotty
PPTX
PPTX
Evolution of c# - by K.Jegan
PPTX
Java generics(Under The Hood Of The Compiler) by Harmeet singh
PPT
Generic Types in Java (for ArtClub @ArtBrains Software)
PPT
Generics_RIO.ppt
PPT
CSharp_03_ClassesStructs_and_introduction
PPTX
.Net F# Generic class
PPT
Generic Programming seminar
PPT
Advanced c#
PPT
Generics in java
PPT
Jdk1.5 Features
PDF
Language tour of dart
CSharp_03_Generics_introduction_withexamples
More Little Wonders of C#/.NET
Csharp4 generics
unit 5.docx...............................
Generic Programming in java
From DOT to Dotty
Evolution of c# - by K.Jegan
Java generics(Under The Hood Of The Compiler) by Harmeet singh
Generic Types in Java (for ArtClub @ArtBrains Software)
Generics_RIO.ppt
CSharp_03_ClassesStructs_and_introduction
.Net F# Generic class
Generic Programming seminar
Advanced c#
Generics in java
Jdk1.5 Features
Language tour of dart

More from Iblesoft (10)

PPTX
Master pages ppt
DOCX
State management
PPTX
State management
PPT
Validation controls ppt
PPT
Ado.net
PPT
Data controls ppt
PPT
Asp.net architecture
PPTX
Delegates and events
PPT
Html ppt
PPT
Exception handling
Master pages ppt
State management
State management
Validation controls ppt
Ado.net
Data controls ppt
Asp.net architecture
Delegates and events
Html ppt
Exception handling

Recently uploaded (20)

PDF
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
PDF
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
PDF
0520_Scheme_of_Work_(for_examination_from_2021).pdf
PPTX
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
PDF
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
PDF
M.Tech in Aerospace Engineering | BIT Mesra
PDF
anganwadi services for the b.sc nursing and GNM
PDF
Journal of Dental Science - UDMY (2021).pdf
PDF
Chevening Scholarship Application and Interview Preparation Guide
PPTX
Reproductive system-Human anatomy and physiology
PPTX
UNIT_2-__LIPIDS[1].pptx.................
PDF
Literature_Review_methods_ BRACU_MKT426 course material
PPTX
Macbeth play - analysis .pptx english lit
PPTX
2025 High Blood Pressure Guideline Slide Set.pptx
PDF
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
PPTX
Diploma pharmaceutics notes..helps diploma students
PDF
The TKT Course. Modules 1, 2, 3.for self study
PDF
Disorder of Endocrine system (1).pdfyyhyyyy
PDF
Journal of Dental Science - UDMY (2020).pdf
PDF
Everyday Spelling and Grammar by Kathi Wyldeck
Fun with Grammar (Communicative Activities for the Azar Grammar Series)
LIFE & LIVING TRILOGY - PART (3) REALITY & MYSTERY.pdf
0520_Scheme_of_Work_(for_examination_from_2021).pdf
Integrated Management of Neonatal and Childhood Illnesses (IMNCI) – Unit IV |...
fundamentals-of-heat-and-mass-transfer-6th-edition_incropera.pdf
M.Tech in Aerospace Engineering | BIT Mesra
anganwadi services for the b.sc nursing and GNM
Journal of Dental Science - UDMY (2021).pdf
Chevening Scholarship Application and Interview Preparation Guide
Reproductive system-Human anatomy and physiology
UNIT_2-__LIPIDS[1].pptx.................
Literature_Review_methods_ BRACU_MKT426 course material
Macbeth play - analysis .pptx english lit
2025 High Blood Pressure Guideline Slide Set.pptx
Lecture on Viruses: Structure, Classification, Replication, Effects on Cells,...
Diploma pharmaceutics notes..helps diploma students
The TKT Course. Modules 1, 2, 3.for self study
Disorder of Endocrine system (1).pdfyyhyyyy
Journal of Dental Science - UDMY (2020).pdf
Everyday Spelling and Grammar by Kathi Wyldeck

Generics

  • 1. GenericsCh. Vishwa MohanFreelance Software Consultant &Corporate Trainer
  • 2. GenericsGenerics are similar to C++ templates. Generics has the following features: Type Safety, No boxing and unboxing when using collections for value types. No Downcasts. Generic types can be defined in one language and used from any other .NET languages (Binary code reuse). Reduced Code bloat (typed collections).Instantiated at run-time not compile time. Work for both reference and value types. Complete run-time type information. It is not possible to assign null to generic types. In this case the keyword default can be used.
  • 3. Generics Type parameter can be applied to ClassesStructuresInterfaces DelegatesYou can also apply type parameters to methods alsoType parameters can have constraints (See next slide). Some more info on Generics:T.defaultNull ChecksType Casts
  • 4. Generic Class FeaturesIt is not possible to assign null to generic types. In this case the keyword default can be used. T obj = default(T); //Initializing to default. You can apply constraints on generic types. public MyGenericType<T> where T : IEnumerable { .., } Generics supports some more constraints as follow:where T : struct //This constrain says type T must be value type. where T : class // This constrain says type T must be reference type. where T : Foo //T is required to be derived from base class Foo. where T : new() // Specifies T must have default ctor. Constructor Constraint.where T : V //The type T derived from a generic type V. This constraint // is known as “Naked Type Constraint”.You can also combine multiple constraints:public class MyClass<T>where T : IFoo, new()//Here T implements IFoo and have default constructor.
  • 5. GenericsInheritance in Generics: A generic type can implement a generic interface. Similarly generic class can be derived from a generic base class provided it must follow the below requirement. Generic type of the interface must be repeated or the type of the base class must be specified. Static Members: Static members of a generic class are only shared with one instantiation of the class. There is separate instance of the static variable exist for each type parameter. Generic Interfaces: Using generics you can define interfaces that defines methods with generic parameters. Generic Methods: Like C++, it is also possible to generic methods. Here generic type is define with method declaration. void Swap<T>(ref T x, ref T y) { …,} Swap<int>(ref i, ref j); //Invoking a generic method.
  • 6. Generic DelegatesWith generic delegates the parameter of the delegate can be defined later. Simple delegate examples is given below: public delegate void Del<T>(T item); public delegateT2 Action<T1, T2>(T1 tVal); Nullable<T>: Suppose if you are instantiated as Nullable<int> x. The variable x can now be used like int and also you can assign null to it.Nullable<int> x; //or int? x; if(x.HasValue) x = 5; int y = x.Value; x += 20; x = null;EventHandler<TEventArgs>: With Windows forms, Web Applications delegates many different event handlers are defined. ArraySegment<T>: This structure is used to represent a segment of an array. The offset and count of segment is stored in this structure. Benefit of ArraySegment is you can pass it to method as a argument. Take note, ArraySegment don’t make copy of elements of array. It just refers it.