In the lesson about extension methods, we learned that they are a nice way of adding extra functionality to existing types, and we did that by adding a static method inside a static class, that took a type parameter prefixed with the
this keyword, like this:
So, although my original
Book class does not contain a method called
SellBook(), I am able to call it inside the
Main() method because I declared an extension method for the
Book class, and this static
SellBook() method took a
Book type parameter prefixed with the
this keyword, that signals the compiler that the method is intended as an extension method for the
Book type.… Read more
Sometimes, programmers find themselves in need of adding new functionality to already existing codes, in order to improve or complete them. If the said source code is available, the task is simple – they only need to add the required functionality and recompile.… Read more
Remember from the lesson lambda expressions that we can declare a method locally, without a name, and use it only in one place, where we declare it. This is an example of a lambda expression:
In the previous lesson I was writing that we can further improve our code by using lambda expressions. A lambda expression is a convenient way of defining an anonymous (unnamed) function that can be passed around as a variable or as a parameter to a method call.… Read more
Virtual methods are methods that can be overridden in inheriting (derived) classes. By default, in .NET, methods are not virtual. In order to declare a method as virtual, we need to declare it using the keywordvirtual, like so:
Generic methods, like generic classes, are parameterized (typified) methods, which we use when we cannot specify the type of the method’s parameters. Also like in the case of generic classes, the replacement of unknown types with specific types happens when the method is called.… Read more
There are two main reasons why methods and functions are even used. First one, which I already mentioned in a few of the previous posts, is code re-usability. The second is modularization of the code, splitting of complex tasks into smaller sub-tasks, which can offer us a better overview of the entire functionality.… Read more
I talked in the previous lesson about methods and functions signatures, and I offered there a brief explanation on the topic. It is time to explain this new concept in more detail. At a basic level, there are only two things that are mandatory to specify a method or function signature: the parameters type and the order in which the parameters are listed.… Read more
There are times when we have functions or methods that perform basically the same thing, but use different kind of parameters. So, in other words, there are cases when we have methods with the same name, but with different signatures.… Read more
When I explained functions and methods parameters, I said that we can use them to send different values that our methods and functions can use in their calculations. You should also know that methods and functions can have optional parameters, parameters with a default value, which can be skipped when calling a method or function.… Read more