A Beginner’s Tutorial on Delegates, Extension Methods, Cookies, Optional and Named Parameters
In this Article we will go through with some of the .Net topics which are important for better programming.
We will discuss following four features of C# in this article.
- Extension Methods
- Named and Optional Parameters
So now will start first with Delegates-
What is delegates?
Delegates are particularly used for implementing events and also the call-back methods. All delegates are derived from the System.Delegate class. A delegate may be a reference sort variable that holds the reference to a technique. The reference are often modified at run-time.
For example, consider a delegate:
When the above code is compiled and executed, it produces the following result:
Value of Num: 35 and Value of Num: 175
Extension Method in C#
Extension strategies are a brand new feature in C# 3.0. An extension methodology allows us to feature strategies to existing varieties while not making a new derived sort, recompiling, or modify the initial varieties. We will say that it extends the practicality of an existing sort in .NET. An extension methodology may be a static method to the present static class. We tend to call an extension methodology in the same general way; there is no distinction in line.
Feature and Property of Extension Methods
The following list contains basic features and properties of extension methods:
- It is a static method.
2. It should be settled in an static category.
3. It uses the (this) keyword because the initial parameter with a sort in .NET and this method are referred to as by a given kind instance on the consumer facet.
4. It additionally shown by VS intellisense. When we press the dot (.) after a sort instance, then it comes in VS intellisense.
5. Associate in nursing extension methodology ought to be within the same namespace because it is employed otherwise you have to import the namespace of the category by a victimization statement.
6. You will be able to provide any name for the category that has an extension method however the class ought to be static.
7. If you wish to add new strategies to a sort and you do not have the source code for it, then the answer is to use and implement extension strategies of that kind.
If you produce extension strategies that have the identical signature methods because the kind you are extending, then the extension strategies can never be called
Optional and Named Parameters
Optional Arguments: The second associated feature introduced in .NET 4 is Optional Arguments, now we can state default value of a dispute for a method. While calling that method, we have a choice, if we want to pass that parameter or just take benefit of default parameter. Look at the below method to see how to describe default value of optional arguments I have improved the GetBonus() method to deliver multiplier 2 as default value.
Named Arguments: Starting from .NET 4, we can specify method parameter requiring their name. This helps in increasing readability of code as well as the consumer of method does not need to remember the sequence of parameter. Look at the below method (in C#):
Next, topic is used in storing some important data of user on client machine, which is used in frequent request for future.
What are Cookies?
How are Cookies started?
When a client requests to the server, the server sends cookies to the client. The identical cookies may be spoken for later requests.
Now for all subsequent requests from the same client, it uses the session-ID from the cookies and it sent to Server. Server verify is received session-ID from client is there on Server or not.
The main disadvantages of cookies are:
1. It stores information in easy text format, thus it is not secure in any respect.
2. There is a size limit for cookies knowledge (4096 bytes / 4KB).
3. The utmost variety of cookies allowed is also restricted. Most browsers offer limits the number of cookies to 20. If new cookies come, the previous ones are discarded. Some browsers support up to 300.
Thanks for reading this article, please follow, subscribe and provide me your valuable comments.