0% found this document useful (0 votes)
55 views12 pages

C# 02 - Generics

This document provides an overview of generics in C# and .NET Core. It discusses creating generic classes, features of generics like default values and constraints, generic interfaces and structs, and generic methods. The key points covered include: generics allow defining classes, interfaces and methods once that can work with multiple types, generics are type-safe, inheritance works the same for generics as non-generics, and generic interfaces support covariance and contravariance of type parameters.

Uploaded by

Ateik Alzehla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views12 pages

C# 02 - Generics

This document provides an overview of generics in C# and .NET Core. It discusses creating generic classes, features of generics like default values and constraints, generic interfaces and structs, and generic methods. The key points covered include: generics allow defining classes, interfaces and methods once that can work with multiple types, generics are type-safe, inheritance works the same for generics as non-generics, and generic interfaces support covariance and contravariance of type parameters.

Uploaded by

Ateik Alzehla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

C# 7 and .Net Core 2.

2
Generics

Ateik Alzehla
Content

An overview of generics


Creating generic classes
Features of generic classes
Generic interfaces
Generic structs
Generic methods
An overview of generics
 If we want to get the sum of two integers (int)
public int Add(int x, int y)
{
return x + y;
}

 Now, if I want get the sum of two long numbers:


public long Add(long x, long y)
{
return x + y;
}

 This what will happen with other types like: float, double or decimal.
 So, we are going to create a method for every type. THIS IS A PROBLEM.
An overview of generics (Continued)


We can solve this problem with object type:
public object Add(object x, object y)
{
return x + y;
}


But this is not Type Safe, especially with complex type like classes, and it
doesn’t allow us using the feature of the type such as methods properties

var list = new ArrayList();


list.Add(44);
list.Add("mystring");
list.Add(new MyClass());
An overview of generics (Continued)

Generics is the best solution public T Add(T x, T y)
{
return x + y;

Generics is useful: }

Reducing code : A generic class can be defined once and can be


instantiated with many different types.
Type safe.
Performance is better.
An overview of generics (Continued)

Naming Guidelines:
Prefix generic type names with the letter T.
If only one generic type and not special so the letter T is enough.
public class List<T>{ }

If there’s a special requirement for a generic type, use descriptive names
for the type names.
public class Dictionary<TKey,TValue>{ }
public class Dictionary<TK,TV>{ }
public delegate void EventHandler<TEventArgs>()
Creating generic classes
public class MyList public object this[int i]
{ {
private object[] _items; get
public MyList():this(0) {
{ return _items[i];
}
} set
public MyList(int capacity) {
{ _items[i] = value;
_items = new object[capacity]; }
} }
public void Add(object item) public int Count { get=> _items.Length; }
{ public IEnumerator GetEnumerator()
object[] temp = new object[Count + 1]; {
for (int i = 0; i < Count; i++) for (int i = 0; i < Count; i++)
{ {
temp[i] = this[i]; yield return _items[i];
} }
temp[Count] = item; }
_items = temp;
}
Creating generic classes (Continued)
public class MyList<T> public T this[int i]
{ {
private T[] _items; get
public MyList() : this(0) {
{ return _items[i];
}
} set
public MyList(int capacity) {
{ _items[i] = value;
_items = new T[capacity]; }
} }
public void Add(T item) public int Count { get => _items.Length; }
{ public IEnumerator<T> GetEnumerator()
T[] temp = new T[Count + 1]; {
for (int i = 0; i < Count; i++) for (int i = 0; i < Count; i++)
{ {
temp[i] = this[i]; yield return _items[i];
} }
temp[Count] = item; }
_items = temp; }
}
Generics Features:
 Default values
 Uses keyword: default
 Is used to initialize generic types either to null or to 0
 Constraints
 If the generic class needs to invoke some methods from the generic type, you have to add
constraints.(class, struct, IFoo, Foo, new(), T2)
 Inheritance
 Same inheritance.
 Partial specialization.
 Static members
 Static class for every type;
Generic Interfaces & Generic structs:
 Same as Classes but with much more.
 each type parameter of a generic interface can be marked covariant ( out ),
contravariant ( in ), or invariant (no annotation)
 Invariant => same as class
 Covariant: method return type;
 Contravariant: method arguments type
 Generic structs: same as Classes but without inheritance features
 Nullable<T>
Generic methods:
 Methods can be generic.
 With a generic method, the generic type is defined with the method
declaration.
 Can be defined within non-generic classes.
 Generic constraints can be used.
 Useful for Extensions.
Any questions?

You might also like