![]() The first parameter of the extension method should use the this keyword. Every extension method must be a static method. These classes are used in the client side only to display the objects so the basic usage is accessing properties. Following points need to be considered when creating an extension method: The class which defines an extension method must be non-generic, static and non-nested. I have a bunch of classes that represent objects that I have in a database. I use them to reuse my object model classes. ![]() and, as with 'Enumerable.xxxxxx', only obstructs the different class name that has zero informational value You might then get queries such as this one: Enumerable.Select(MyEnumerableExtensions.RemoveNegativeNumbers(numbers), x => -x) Therefore, you'd have to define your own static class containing extension methods. Second, if you now wanted to introduce your own query operator, you would naturally have no way of defining it inside the Enumerable static class, like all the other standard query operators, because Enumerable is in the framework and you have no control over that class. See how much nastier that query just got. ![]() Thus, if extension methods didn't exist, and these were normal static methods, the last line of code would essentially have to look like this: Enumerable.Select(Enumerable.Where(numbers, x => x > 0), x => -x) Let's take this LINQ query as an example: numbers.Where(x => x > 0).Select(x => -x)īoth Where and Select are extension methods, defined in the static class Enumerable. nested method calls, and cluttering of a LINQ query with meaningless static class names. I'll demonstrate this with two aspects of this: method chaining vs. Invisible Bead Extensions (IBE) are discreet and give you the ability to wear you hair up without pain, discomfort or damage to your natural hair. ![]() I would like to support the other answers here that mention improved code readability as an important reason behind extension methods. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |