blogs

miércoles, 24 de junio de 2015

IEnumerable interface

IEnumerable and IEnumerator interfaces:

Provide all methods that we need to can iterator in the elements of each collection or array.
1.Define a simple business object
2.Define a collection in term of simple business object. Every class must implement IEnumerable  such that we can use with a foreach.We must implement the GetEnumerator method.
3.When we implement IEnumerable, we also must implement IEnumerator interface.
 The enumerators must define them before the first element, index=-1
4.Use the define collection in term of simple business object.

using System.Collections;

Example:
if our business is about a library, and the book is very import for our business.
Simple business object: Library.
IEnumerable

Create a new class Book



Create a new class that implements the methods of IEnumerator


Create a new class that implements the method of IEnumerable

Create a test method in which we will fill  our array of books and where we will iterate our books collection.



 Our test run
Our library has a books collection

                            


Call to method getEnumerator


A our class that is implementing the methods IEnumerator


Verify that exist a next element to get the value because if the index is equals to length of  array can not get the data.


In this case the index=0 and length of array is equal to 6 so than shows the element






miércoles, 17 de junio de 2015

Extension Methods

Extension Methods
What make when we want  extend a existing type without modifier its definition.
  • We must create a static method in a static class.
  • We must use the first parameter to point the type that we can extend.
  • We must precede the first parameter with the this keyword .

For example:

public static bool ContainsNumbers(this string s)
{

}

Invoke extension methods
The method must be invoke like any other method.
For example:

string text= "Text with mumbers2";
if(text.ContainsNumbers)
{

}

In this case we will use the transport project.

We create two methods, one of them doesn't receive attribute and the other receive the attributes. We will use the class "Car" to make the extension methods.

Create a new class and a unit test

Run the first method



Run the test method





 Result  of the executing the first method.


Run the second method







Result of the executing the second method.


Interface

Interface: An interface contains only the signatures of methods, properties,events or indexers. A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition.

An interface can not contain constructors.
An interface only has the method signature.
An interface can not contain fields.
An interface can contain automatic property.
The modifier "Public" is not valid for methods
All methods and properties should be implemented in  classes or structs that use the interface.
The interface's access modifier is internal by default. if we want to call it in other namespace, we need change it to public.

Create a new interface


Define the methods


Create an class to implement the methods defined by the interface. If we don't want to implement them, then the class must be abstract. In this case we will define a base class.




If we want to implement  the methods  define by the interface in a struct, we must make directly  of the interface because the struct does not support inheritance.
  




Implementation the derived classes


Create a test method to test the struct

                                 
Run the struct





Create a test method to test the derived class using base class.



Test run








Create a test method to test derived class using the interface.


Test run










miércoles, 10 de junio de 2015

Optional Argument and Name Argument

An argument is data passed to a method's parameter when it is invoked
When calling methods, we must supply arguments that match the parameter.

Optional Argument or parameter Default values:

A optional argument :

  • Allow developers to supply typical values.
  • Allow developers to hint at usage
  • Reduce the number of method overrides
  • Allow parameters to be optional to the caller.

For example

we have a  method:
class Customer{
public  int AddPoints(int pointToAdd=1)
{
    point=pointToAdd;
}
}
This parameter is optional, if we don't put the parameter, the values by default is 1.

For Example.

We will use the transport's project

Create a test method and create an instance class.

Run test



Execution on line 13




Add new parameter and property in class Car to test the order the parameters




Optional argument forces us to put in order the parameters


 Method execution





the problem is when we want to put just some parameters, we must put them sequentially.

For this reason exists other way to do this.

Name argument:

Name argument
  • Allow developers to specify argument in an order
  • Allow developers to easily handle optional arguments
  • Positional and named arguments can be mixed.

Create a test method







 Run test