C# Extension Methods

c# extension methodsWhen a person programs, they have to give a computer detailed instructions on what to do, which is more often than not a time consuming task. Computer programming languages allow us to communicate with computers, but it involves giving them a laundry list of instructions. Extension methods help to make this process a great deal easier for many programmers. Using an extension method, a programmer will have the skeleton of a method already outlined, and instead of them creating an entire method for the program to run, they simply have to use the extension method instead.

In C# programming, there are various ways in which people use extension methods, and there are some cases where people create their own methods to reuse in their future programs. You can not only create your own extension methods, but use some of the most important ones in the .NET Framework if you want. Using C# and .NET together can make your life a lot easier when it comes to programming. If you want to learn how these two work in sync in order to create great programs, then try the Udemy course Learning C# .NET – Programming for Beginners. The course is great for even those who have absolutely zero experience in the world of programming.

What is an Extension Method? 

Extension methods are pretty common in the world of computer programming, and whether a programmer uses them in .NET or LINQ, they can get a great deal of work done with it. In fact, C# is one of the only, if not the only, program to inherently allow you to use extension methods with relative ease.

An extension method simply gives you the ability to extend a data type in your program. In the simplest terms possible, this method defines a static method in a static class. The method then identifies the data type that is being extended as the very first parameter in the method by identifying the “this” that is placed in front of it.

Below you will find an example of an extension method in action.

.public static class Extensions
{
Public static int OneHalf (this int source)
{
Return source / 2;
}
}

Notice some of the key aspects of the program. The first line of the program, public static class Extensions, names and identifies the class. Notice the word static in the identity of the class. In order to use an extension method, you have to make sure that the class is static.

The next line in the program is public static int OneHalf(this int source). In this part of the program there are key aspects you have to remember as well. This program is the start of the method, and just like the class it has to be static. There’s also the data type of int. For this extended method OneHalf is an integer.

Within the parenthesis there is the word source. This is the first parameter that will be extended. The parameter is identified by the “this” that is placed in parenthesis with it. Notice the int data type as well. When creating an extension method, the data type must be the same as that of the data type of the method.

The rest of the program is pretty straightforward. The program returns the value known as source and divides it by 2 thus creating the one half that the method is named for.

Examples of an Extension Method 

Extension methods are one of the basic concepts of c# programming, and it’s important for any programmer to learn. If you want to see how you can make great use of extension methods and other c# concepts, check out the Explore the Fundamentals of Programming course in Udemy.

How Extension Methods Make Programming Easier 

public static class ExtensionInt
{
public static int OneHalf(this int solution)
{
return solution / 2;
}
public static int ExponentCube(this int solution)
{
return (int)Math.Pow(solution, 3);
}
public static int ExponentSquare(this int solution)
{
return (int)Math.Pow(source, 2);
}
}

The above methods are an example of the ways in which extension methods can be used to make programming easier. Look at the three methods above. They all do a specific math problem. The first method takes an integer and divides it by two. The second method takes a number and multiplies it exponentially by three. The second method does the same thing, except that instead of multiplying exponentially by three it takes the number and multiplies it by two.

Now, say for instance that you wanted to take a number and cube it or multiply it exponentially by 3. You then wanted to multiply that new result exponentially by 2, and then finally you would want to divide the result by 2. If you wanted to do that with traditional programming methods you would get an incredibly complex line of code. Let’s look at what the traditional way to do this would be with the number 25.

var answer = ExtensionsInt.ExponentCube(ExtensionsInt.ExponentSquare(ExtensionsInt.OneHalf(25)));

As you can see above, there’s a ton of nesting involved in doing a problem like this. You have to make sure that you nest together everything perfectly, and that you do it in the right order as well. Imagine making a mistake; you would have to check every character in this line of code to see if everything stacks up. One particular mistake a lot of people make is that they do not know how many parentheses goes into a problem such as this.

On the other hand, if you were to use extension methods, this line of code would be far simpler.

var answer = 25.ExponentCube().ExponentSquare().OneHalf();

The above line of code does the same thing as the line of code that you saw before, but it is far shorter and a lot easier to read. You can easily see what the program is doing and if you were to make a mistake, you could catch it rather easily.

This is the power of extension methods; they allow you to take a particular parameter and add a level of functionality to it that you wouldn’t have necessarily had before. Extension methods can make your programs very fluid and run much better than if you were to program without them.

If you want to truly master C# programming but need a bit of help, you should learn it while using Visual Studio. Visual Studio gives you several useful helpful resources, such as IntelliSense to help identify when you are using extension methods and other concepts in your programming. Additionally, IntelliSense makes helpful suggestions in order to make programming much easier for you.

Check out the Udemy course A 13 Hour C# Course with Visual Studio 2013 to get a crash course in Visual Studio 2013 and C# programming together.

Mastering Concepts in C# 

C# may be one of the easier programming languages for you to start with, but it is far from simple. Once you get the basic concepts down, there are many more for you to learn. If you want to become a great programmer it’s going to take a bit of practice to fully understand everything you must know.

A great way to get started with programming is to take a crash course. This will get the basic concepts to you quickly and give you more time to program and try those concepts out yourself. Check out the Udemy’s Learn C# in 1 Hour for a basic primer in this programming language.