Saturday, October 29, 2022

extension methods

extension methods

  1. Extension Methods (C# Programming Guide)
  2. Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type. Extension methods are static methods, but they're called as if they were instance methods on the extended type. For client code written in C#, F# and Visual Basic, there's no apparent difference between calling an extension method and the methods defined in a type.

    The most common extension methods are the LINQ standard query operators that add query functionality to the existing System.Collections.IEnumerable and System.Collections.Generic.IEnumerable <T > types.

    To use the standard query operators, first bring them into scope with a using System.Linq directive. Then any type that implements IEnumerable <T > appears to have instance methods such as GroupBy, OrderBy, Average, and so on. You can see these additional methods in IntelliSense statement completion when you type "dot" after an instance of an IEnumerable <T > type such as List<T > or Array.

    Extension methods are defined as static methods but are called by using instance method syntax. Their first parameter specifies which type the method operates on. The parameter is preceded by the this modifier. Extension methods are only in scope when you explicitly import the namespace into your source code with a using directive.

    General Guidelines While it's still considered preferable to add functionality by modifying an object's code or deriving a new type whenever it's reasonable and possible to do so, extension methods have become a crucial option for creating reusable functionality throughout the .NET ecosystem. For those occasions when the original source isn't under your control, when a derived object is inappropriate or impossible, or when the functionality shouldn't be exposed beyond its applicable scope, Extension methods are an excellent choice.

  3. Reference: Array Class
  4. Provides methods for creating, manipulating, searching, and sorting arrays,

    thereby serving as the base class for all arrays in the common language runtime.

    Remarks The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface.

    The Array class is the base class for language implementations that support arrays. However, only the system and compilers can derive explicitly from the Array class. Users should employ the array constructs provided by the language.

    An element is a value in an Array. The length of an Array is the total number of elements it can contain. The lower bound of an Array is the index of its first element. An Array can have any lower bound, but it has a lower bound of zero by default. A different lower bound can be defined when creating an instance of the Array class using CreateInstance. A multidimensional Array can have different bounds for each dimension. An array can have a maximum of 32 dimensions.

    Unlike the classes in the System.Collections namespaces, Array has a fixed capacity. To increase the capacity, you must create a new Array object with the required capacity, copy the elements from the old Array object to the new one, and delete the old Array.

    The array size is limited to a total of 4 billion elements, and to a maximum index of 0X7FEFFFFF in any given dimension (0X7FFFFFC7 for byte arrays and arrays of single-byte structures).

    .NET Framework only: By default, the maximum size of an Array is 2 gigabytes (GB). In a 64-bit environment, you can avoid the size restriction by setting the enabled attribute of the gcAllowVeryLargeOb

  5. Type Class
  6. Represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.

    Other Good Posts

  7. C# - Extension Method
  8. Extension methods, as the name suggests, are additional methods. Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface. Extension methods can be added to your own custom class, .NET framework classes, or third party classes or interfaces.

    Note: The only difference between a regular static method and an extension method is that the first parameter of the extension method specifies the type that it is going to operator on, preceded by the this keyword.

    Points to Remember :
    1. Extension methods are additional custom methods which were originally not included with the class.
    2. Extension methods can be added to custom, .NET Framework or third party classes, structs or interfaces.
    3. The first parameter of the extension method must be of the type for which the extension method is applicable, preceded by the this keyword.
    4. Extension methods can be used anywhere in the application by including the namespace of the extension method.
  9. Extension Methods in C#
  10. A C# extension methods allows developers to extend functionality of an existing type without creating a new derived type, recompiling, or otherwise modifying the original type. C# extension method is a special kind of static method that is called as if it was an instance methods on the extended type. In this article, we will create a class library and we will then extend its functionality from the caller code by implementing extension methods in C#.

    Benefits of extension methods
    1. Extension methods allow existing classes to be extended without relying on inheritance or having to change the class's source code.
    2. If the class is sealed than there in no concept of extending its functionality. For this a new concept is introduced, in other words extension methods.
    3. This feature is important for all developers, especially if you would like to use the dynamism of the C# enhancements in your class's design.
    Important points for the use of extension methods
    1. An extension method must be defined in a top-level static class.
    2. An extension method with the same name and signature as an instance method will not be called.
    3. Extension methods cannot be used to override existing methods.
    4. The concept of extension methods cannot be applied to fields, properties or events.
    5. Overuse of extension methods is not a good style of programming.
  11. Extension Methods in C#
  12. In this article, I am going to discuss the Extension Methods in C# with Examples. Please read our previous article where we discussed Sealed Class and Sealed Methods in C# with Examples. At the end of this article, you will understand what exactly C# Extension Methods are and when and how to use these extension methods in C#.

    While working with the Extension Method in C#, we need to remember the following points.
    1. Extension methods must be defined only under the static class. If you check our NewClass, then you will see that the NewClass is a static class.
    2. We already discussed that Static Class in C# contains only Static Members. As an extension method is defined under a static class, it means the extension method should be created as a static method whereas once the method is bound with another class, the method changes into non-static. Now, if you check the methods in NewClass, then you will see that all three methods are declared as static only.
    3. The first parameter of an extension method is known as the binding parameter which should be the name of the class to which the method has to be bound and the binding parameter should be prefixed with this As here we are creating these extension methods to extend the functionality of OldClass, so, you can check the first parameter of all these methods are going to be OldClass which is also prefixed with this keyword.
    4. An extension method can have only one binding parameter and that should be defined in the first place on the parameter list.
    5. If required, an extension method can be defined with normal parameters also starting from the second place of the parameter list. If you check the Test3 method, we have passed the second parameter as int and while calling this method we also need to pass one integer value.
  13. Extension Methods in C#
  14. Have you ever needed to extend a class provided by a third-party library? You might want to add functionality or simplify a particular set of methods in the library.

  15. Extension Method in C#
  16. In C#, the extension method concept allows you to add new methods in the existing class or in the structure without modifying the source code of the original type and you do not require any kind of special permission from the original type and there is no need to re-compile the original type. It is introduced in C# 3.0.

    Important Points:
    1. Here, Binding parameters are those parameters which are used to bind the new method with the existing class or structure. It does not take any value when you are calling the extension method because they are used only for binding not for any other use. In the parameter list of the extension method binding parameter is always present at the first place. if you write binding parameter to second, or third, or any other place rather than first place ,then the compiler will give an error. The binding parameter is created using this keyword followed by the name of the class in which you want to add a new method and the parameter name. For example:

      this Geek g

      Here, this keyword is used for binding, Geek is the class name in which you want to bind, and g is the parameter name.
    2. Extension methods are always defined as a static method, but when they are bound with any class or structure they will convert into non-static methods.
    3. When an extension method is defined with the same name and the signature of the existing method, then the compiler will print the existing method, not the extension method. Or in other words, the extension method does not support method overriding.
    4. You can also add new methods in the sealed class also using an extension method concept.
    5. It cannot apply to fields, properties, or events.
    6. It must be defined in top-level static class.
    7. Multiple binding parameters are not allowed means an extension method only contains a single binding parameter. But you can define one or more normal parameter in the extension method.
  17. How to implement and use Extension Methods in C#
  18. In C#, we can use the Extension Methods to extend both built-in types and custom types. These extension methods are introduced in C# 3.0

  19. File.ReadLines(String) Method in C# with Examples
  20. File.ReadLines(String) is an inbuilt File class method that is used to read the lines of a file.

  21. How to: Add class diagrams to projects
  22. To design, edit, and refactor classes and other types, add a class diagram to your C#, Visual Basic, or C++ project. To visualize different parts of the code in a project, add multiple class diagrams to the project.

No comments:

Post a Comment