blogs

miércoles, 26 de agosto de 2015

Generic II

Restrictions of generics
When we define a  class generic is necessary impose one or more conditions to some parameters of type associate to the definition.
* Assume that a type inherited of a base class or implement some interface, only is possible apply to the variable of this type that is inside of the class,certain property or specify method  ; In this case  a parameter is replace for any kind. In this case fall from object.
The restrictions are specify using the clause where associated to the definition of the generic type.
for example public class PairSort<T> where T:struct
The restrictions that can be of five types
Heritage: The type must be inherited of a determine  base class.
Interface:The type must implement a specify interface.
Reference type  :The type must be a  reference type(e.g. class).It used to key word "Class" in the clause where.
Value type:The type must be a value type.It used to key words "Struct"  in the clause where.
Constructor:The type must have a constructor without parameters(explicit default).It used the key word new in the clause where.








Generic methods
C# allow defined only not generic type ,but also can do generic to singled method as an  instance method or  static method.




                        



Swap()this is an classical example  that allows to exchange the value of two values that are values type or reference type

IComparable: All the basic types implement to its manner  IComparable. It allow compare instance. For example Person, it can do that this class implement it too.


IComparable force us to compare and convention of type



Generic Delegate 



It is important know that some generic delegate  are already present in the library of base classes of Net Framework. for example the type processing<T> already is defined with the name of Action<T>

Action delegates from the system namespace, for example action<T> and Action<T1,T2>
Encapsulates a method that has a single parameter and does not return a value

public delegate void Action<T>(T t);

 Using an Action<T> and an IEnumerable<T>


Our Test Method to test this methods



The Predicate<T> delegate:Represent the method that defines a set of criteria and determines whether the specified object meets those criteria
public delegate bool Predicate<T>(T t);
The Comparison<T> delegate:Represent the method that compares two objects of the same type
public delegate int Comparison<T>(T x, T y);

Example of how implement a enumerable.  I have already explained on Enumerable http://marisolca.blogspot.com/2015/06/ienumerable-interface.html .It is similarly  only in this case is IEnumerable<T>





ICollection<T>:This interface  implement all the generic collections. Offering basic functionality to add,delete,copy and iterator the elements of a collection.
ICollection<T> extends of IEnumerable<T> and IEnumerable
Queue<T> and Stack<T> are implement directly to ICollection<T>
Other collections inherited indirectly of ICollection<T> for example
IList<T>
IDictionary<T>Pair collection of key and value.

miércoles, 12 de agosto de 2015

Generics Part I


  • Generics allow us to create and use strongly type collections that are type-safe because we do not need to do casting of the elements nor do boxing and unboxing of  value types. 
  • Generic classes is defined including the parameter type T in the declaration of the class or interface. 

How to create a generic class

  • Add the type parameter T between symbol minor  and  major ( < >) after the class's name.
  • Use the type parameter T instead the  data  type names in the  member classes. 





  • Each instance of T inside the class is replace with parameter type  that we have provided.
  • Specify the type that we want to pass as parameter type.
For example



In this example our list only allow object of type person if we want of other type we can create other list.




  • One of the more commons and important use of the generics are in collection classes..
advantages of generics

The generic collections provide :
  • Type safety: assure that we have a homogeneous  collection,
  • No casting: Casting is very expensive while generics improve the performance the application because add or read items do not need to do casting.
  • No boxing and unboxing: they are long expensive and can make slow an application, specially in long collections
Generic list Collections .
Generic  class  list store collections in objects of type T
  • List<T> : It is a list of generic use
  • LinkedList<T>:it is list in which each item is linked  to previous  item  and the next in the collection.
  • Stack<T>: it is a collection last in, first out
  • Queue<T>: it is a collection first in, first out
Generic Dictionary collections  

It is dictionary classes that store pair key and value.
the key and the value are strongly types
  • Dictionary<Tkey, TValue>:it is a generic dictionary to use general.
  • SortedList<Tkey,TvalueZ>:
  • SortedDictionary<TKey,Tvalue>: They are sort collections by  key 

miércoles, 5 de agosto de 2015

Generate code since a diagram in C#

we can generate code since a diagram, in this case we only need to add the implementation  of our classes.We also can create a diagram of the classes from code.

1: Generate code since a diagram





 We can create class, interface,enumeration.





















2:Generate a diagram from code

Select all classes of a project