Skip to main content

Posts

Showing posts with the label .net generics

C# Advanced Topics - Extension Methods

Extension Methods What are extension methods ?  Extension methods provide us the ability to add methods to an existing class without changing its source code or creating a new class that inherits from it .  Extension methods are the static methods but they are called as if they were instance methods on the extended type .  Before we dig deep , lets find our reason on why does it came into existence ?  Why and when should or we are recommend to use an extension method in your application .  Let me show you an example here :   Lets consider a scenario where we need to shorten the number of characters displayed based on the passed input . So as String being a sealed class we are neither allowed to inherit or extend it.This is where the extension methods are quite helpful . Define a static class that holds a static method which takes numberOfWords as one of its parameter and return a string . Their first parameter will be the type on which the method opera...

C# Advanced Topics - Lambda Expression

Lambda Expression  So what is a lambda expression and why are these lambda expression used for ?  As every article of me says you what and why , here as well we are going to get an answer for it .  A lambda expression is nothing but an anonymous method .   Oh really !! But what is anonymous method here ? It might be the next question which strikes you . Just follow through get an answer for it .  An Anonymous method is a method with no access modifier , no name and no return statement . But apart from this book definition we can look its snippet for a better understanding. Before that why do we use them ? Any guess ?  For convenience . We can write less code and achieve the same thing plus our code will be more readable readable . For our practical knowledge , lets take an example , lets say we would like to write a method which takes a number and returns cube of the number . Typically , this is how we do that   Now with lambda expression w...

C# Advanced Topics - Generics

Generics  Before we start learning What Generics is ?  , let me explain what problem generics came to solve . In .NET version 1 , we don't have generics .So ,lets say we need to create a list to store a list of numbers . The implementation of that class looks like below :  Currently , as I am not concentrating on how data structures works , just assumed a sample list class with  only Add method , which add the passed integer into its list. Rather than defining all the methods like Remove , find and properties and functionality and so on , just planned to have my class and projected on how generics came into existence . Let me define a simple class object here :  Now , if i plan to store a book to the list defined above it doesn't work as int is a value type and our book class will be a reference type . So , I am forced to create some class like below, which will give the ability to add a list of books :  So , th...