C# Code Snippets Events

C# Events

In this tutorial, we will take a look at events in C#. A class(subscriber class) can subscribe to an event of another class(publisher class). The publisher class notifies the subscriber class every time the event occurs. This works by using delegates. The subscriber will pass its delegate to the publisher class to “make a subscription” to its event. When the event occurs the publisher will call the delegate(callback method) that was provided to it by the subscriber when it “subscribed” to the event. This is how (for example button press event) events work in .NET. This principle of operation is also called the observer pattern.

C# Code Snippets Delegates

C# Delegates

In this tutorial, we will take a look at delagates in C#. A delegate is a pointer to a method(basically an indirect call to the method). Delegates can be passed as input parameters to other methods. A benefit of this is that it provides flexibility. Such a method can then use the passed in delegate to call another method(callback method). This is actually the way events work.

C# Code Snippets Method Hiding vs Overriding

C# Method Hiding vs Overriding

In this tutorial, we will see the difference between method hiding and overriding in C#. The difference between method hiding and method overriding becomes evident when a class object is used polymorphically. The child version of the method gets called only when the method is overridden. If the method is hidden(new keyword) the parent version of the method gets called. Meanwhile, if we don’t call a method polymorphically the new keyword will act in the same way as the override keyword does and hide the original parent implementation of the method.

C# Code Snippets Using Keyword

C# Using Keyword

In this tutorial, we will take a look at the using keyword in C#. The using keyword can be used as a directive to include a namespace or it can be used as a statement. Using it as a statement the using keyword ensures that the dispose() method gets called even if an exception occurs.

C# Code Snippets Interfaces

C# Interfaces

In this tutorial, we will take a look at interfaces in C#. When I was first learning about interfaces I was given this analogy: “Interfaces are like contracts that specify what a class has to implement”. When a class uses an interface it must implement everything that the interface specifies.

C# Code Snippets Extension Methods

C# Extension Methods

In this tutorial, we will take a look at extension methods in C#. If you have a class that you don’t own or is sealed you won’t be able to add anything to it. In such cases, you can use extension methods which will be “glued” on to the class. This enables you to add your code to a class without modifying it or creating a new derived class. 

Advertisment ad adsense adlogger