blogs

miércoles, 29 de julio de 2015

Nullable types

All C# developer must know what are and how work with value types, e.g. int ,double,char,Datetime,etc. all  values type can not container null values by default, but we can do that support nullables types

For example:




the nullables types are necessary to develop programs more robust, it is not necessary to store data by default as a datatime, the datatime  saves complex values.
For example:





 Run the test




We can use nullable types when we need it

Run the test



miércoles, 22 de julio de 2015

Boxing and Unboxing

The System,Object class:
It is the reference type more important en .Net Framework,
All classes are a specialization  of the System.object type.
One variable of this type can refer to one reference of anyone other type.
On variable of this type can reference a anyone value type.
The reserve word is Object, it is an alias for System.object

Two views of the same object in memory



Boxing
Boxing is the act of converting a value type to a object type(reference type, this admit null values).
boxing is computationally expensive.
Value type do not contain  null values. examples bool, int,struct, enum, int, datetime,etc.

For Example:

In other examples I explained about the struct, value type and reference type , review this  post http://marisolca.blogspot.com/2015/05/struct-and-enum.html
and I used this class of this post http://marisolca.blogspot.com/2015/06/interface.html


Examples of Boxing



Run test






Unboxing

Unboxing is the reverse the boxing.
It is a explicit  convert  of the object type  to a value type that implement an interface
unboxing requires a cast.


Casting
programmer  specify option of the referenced data  for an object is of a  type specified and that it is sure  to reference  to this object using that type.
If the type of the object in memory does not match with specified in the cast,in the runtime will launch an exception.

For example:



 Run Test


IS and AS
C# provides us with the IS operator
Verify if the type is of an object is compatible with other type, value type or reference type

For example: 
Using IS operator






The AS operator is used for converter a type  an other different

For example:
Using AS Operator



miércoles, 15 de julio de 2015

Linq

Linq allows make queries in collections that implement IEnumerable<T>: Array, list, xml and source of remote data as SQL server.

Linq Syntax

We have to specify where get the results (FROM IN).
We can specify or not a condition (WHERE), then specify that  attributes we want to select or show (SELECT)

For example

These are our classes





Test method 




Linq doesn't execute the query until access  the result, either walking in a foreach or calling some method as count.

Walk the results using a foreach

Run test 







Linq allow get the results of a query in a specific order, using the orderby instruction  before of the select instruction
For Example:

Run test using Orderby






Grouped data with Linq 
this used Group By instruction, which return the collection  of type IGrouping<T,S>, where T  is the field to which we want grouped y S is the type of grouped objects .

For example

These are our classes 



Test Method using IGrouping




Anonymous types

This was introduced for helping to Linq , it allows to create  dynamic classes and its instance quickly. Only need define properties

For example

These are our classes 


Test Method using Anonymous type







miércoles, 8 de julio de 2015

Lambda Expression


Lambda expression

it is an anonymous function that we can use to create a delegate type or tree expression
Instead of create a method, we give it a name and pass the name of this method to  the definition a delegate, we can use lambda expression to put code directly without create a method,give it the name and assign it to a delegate

Syntax 

(parameters the input)=>expression
(parameters the input)=>{sentence};


Assign a lambda expression to a delegate




Lambda expression as an argument type<Func>

Func, represent method that can return something value, for example: Define a delegate for methods that receive  one parameters and return a  type
Func<int, bool> define a delegate with input parameters and return type .
in the example the input is a int and return type is bool



When we invoke, return true or false to point if the  parameter of input is equal a 5




For Example:


Test result

For Examples

Lambda expression to count  odd numbers that has a array 
we need the library "System.LINQ".


Test result





miércoles, 1 de julio de 2015

Delegates and EventHandler

What is a delegate?

A delegate is an object that knows how to call a  method
A delegate type defines the method signature.
A delegate is a type, that represent the reference to a method with a list particular of parameters and a return type. Can treat methods as an assigned  object to variables and pass them as parameters .
the main  application is in the creation and handled of events.




Declaration of a delegate and methods same signature



To an instance method and static methods:




Reduce syntax


Invoke to the instance of delegate
To invoke to method or subscribed methods to a new instance of delegate , we can call it using Invoke with the parameters  and we can save the result in a variable.
For example:




Reduce syntax

Run Unit Test









What is an event?
A event allows to notify when something happens
A method can register its interest to an event.
The methods registered  will be executed when the event happens.
Define an  event
An event is defined based on a delegate.




Initialize the event as an empty delegate to avoid having that validate if this is null every time that we launch the event.

Launch Event
This is invoke the event as we know do it with a delegate. The type event is  an EventHandler, this is a delegate that accepts void method and receive two parameters: an Object and EventArgs.



Handle and receive events 
We must create methods(EventHandlers) that comply with signature of the event and those must be registered with the event to be notified when happens the event.
Add the eventHandler as a event on list of execution





Define the eventHandler that it will execute in reaction to the event.




Run Test