Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Task 1 – Declaring Extension Methods

This task begins by extending a class using C# 2.0 and then utilizes the C# 3.0 Extension Methods feature.

 

1. Add a new static class, Extensions, to the NewLanguageFeatures namespace:

 

namespace NewLanguageFeatures

{

public static class Extensions

{

}

 

public class Customer

…

 

2. In this new class add a method, Compare that given two customers checks to see if all the properties of each are the same, and if so returns true.

public static class Extensions

{

public static bool Compare(Customer customer1, Customer customer2)

{

if (customer1.CustomerID == customer2.CustomerID &&

customer1.Name == customer2.Name &&

customer1.City == customer2.City )

{

return true;

}

 

return false;

}

}

 

3. Rewrite Main to compare a new Customer with all the others in the list to see if it is present already:

 

static void Main(string[] args)

{

var customers = CreateCustomers();

 

var newCustomer = new Customer(10)

{

Name = "Diego Roel",

City = "Madrid"

};

 

foreach (var c in customers)

{

if (Extensions.Compare(newCustomer, c))

{

Console.WriteLine("The new customer was already in the list");

return;

}

}

 

Console.WriteLine("The new customer was not in the list");

}

 

The previous code demonstrates how you might extend a type with new functionality in C# 2.0.

 

4. Press Ctrl+F5 to build and run the application, which displays “The new customer was not in the list”. Press any key to terminate the application.

With C# 3.0, you can now define an extension method that can be invoked using instance method syntax. An extension method is declared by specifying the keyword this as a modifier on the first parameter of the method.

5. Add the modifier this to the first parameter accepted by Compare:

public static class Extensions

{

public static bool Compare(this Customer customer1, Customer customer2)

{

…

 

6. In the Main method, change the invocation of Compare to use the instance method syntax, making Compare appear as a method of the Order class:

foreach (var c in customers)

{

 

if (newCustomer.Compare(c))

{

Console.WriteLine("The new customer was already in the list");

return;

}

…

 

7. Press Ctrl+F5 to build and run the application again and verify that it displays the same output, then press any key to close the console window and terminate the application.

Extension methods are only available if declared in a static class and are scoped by the associated namespace. They then appear as additional methods on the types that are given by their first parameter.


Date: 2015-02-03; view: 708


<== previous page | next page ==>
Task 3 – Using var to create more concise code | Task 1 – Understanding Lambda Expressions
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)