Uploaded November 2020 | Updated November 2026, 3 hours ago
In C# Extension Methods provide a way to add additional functionality onto a type that you have created or imported into your project. You may use them to extend C#'s built in types like strings and integers or to extend new classes you have constructed. Lets take a look at how you can implement extension methods and start using them in your project!
Extension methods are implemented by creating a static class in your library or C# project. Static classes are different from normal classes in C# in the fact that they can not be instantiated. All of the methods attached to a static class must also be static. This means that extension methods must be static functions.
In addition to being located in static classes, extension methods are defined by a special `this` keyword before the first argument in the function. This keyword causes the first argument to be automatically provided by C#. The value of that argument will be the left hand side of the method being invoked. For example in our code `2.FizzBuzz()` we are extending the `2`. This means that the first argument (`this`) will be equal to `2`.
Other than those few special syntax requirements extension methods behave similar to other functions in C# and you may implement them in a similar way.
Our example extension method that implements Fizz Buzz looks like this:
```csharp
static class Extensions {
public static string FizzBuzz(this int value) {
if (value % 5 == 0 && value % 3 == 0) {
return "FizzBuzz";
} else if (value % 5 == 0) {
return "Buzz";
} else if (value % 3 == 0) {
return "Fizz";
} else {
return value.ToString();
}
}
}
```
### Enabling Function Chaining
Because extension methods alter the structure of your code they may also be used to introduce function chaining to classes which previously may not have supported that capability. This allows queries in LINQ to be chained like:
```csharp
Users.Select(user.IsActive).Where(user.Name.StartsWith("A"));
```
Instead of what normal static methods might introduce:
```csharp
Where(Select(Users, user.IsActive), user.Name.StartsWith("A"));
```
This second version isn't quite as easy to read or implement as the first example.
***
You may learn more about using Extension Methods in the C# language documentation. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
Join the World of Zero Discord Server: discord.gg/hU5Kq2u
In C# Extension Methods provide a way to add additional functionality onto a type that you have created or imported into your project. You may use them to extend C#'s built in types like strings and integers or to extend new classes you have constructed. Lets take a look at how you can implement extension methods and start using them in your project!
Extension methods are implemented by creating a static class in your library or C# project. Static classes are different from normal classes in C# in the fact that they can not be instantiated. All of the methods attached to a static class must also be static. This means that extension methods must be static functions.
In addition to being located in static classes, extension methods are defined by a special `this` keyword before the first argument in the function. This keyword causes the first argument to be automatically provided by C#. The value of that argument will be the left hand side of the method being invoked. For example in our code `2.FizzBuzz()` we are extending the `2`. This means that the first argument (`this`) will be equal to `2`.
Other than those few special syntax requirements extension methods behave similar to other functions in C# and you may implement them in a similar way.
Our example extension method that implements Fizz Buzz looks like this:
```csharp
static class Extensions {
public static string FizzBuzz(this int value) {
if (value % 5 == 0 && value % 3 == 0) {
return "FizzBuzz";
} else if (value % 5 == 0) {
return "Buzz";
} else if (value % 3 == 0) {
return "Fizz";
} else {
return value.ToString();
}
}
}
```
### Enabling Function Chaining
Because extension methods alter the structure of your code they may also be used to introduce function chaining to classes which previously may not have supported that capability. This allows queries in LINQ to be chained like:
```csharp
Users.Select(user.IsActive).Where(user.Name.StartsWith("A"));
```
Instead of what normal static methods might introduce:
```csharp
Where(Select(Users, user.IsActive), user.Name.StartsWith("A"));
```
This second version isn't quite as easy to read or implement as the first example.
***
You may learn more about using Extension Methods in the C# language documentation. docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods
Join the World of Zero Discord Server: discord.gg/hU5Kq2u

![Lets Make a Lazy String Parser in C#
Lets create a quick string parsing script that can read a long string and return a delimited set of results (well be finding words). We can use this to iterate through every word in the string until we find the value we are looking for without having to pre-process the entire original string.
A lazily evaluated function is one that calculates the results of the function as the values are requested. In this case it means we can create a set of integers or strings that we generate dynamically without having to first fill an array or other data structure with all the data first. This also means that for large data sets where we will stop iterating early in the list that we can process only parts of the provided data.
C#s `yield` keyword allows us to return a value from a function and then continue from that point in our code on the next iteration. This allows us to iterate through a loop and `yield` results from the inside of the loop to expose new data to the calling function before generating the complete list. Instead, our list is generated procedurally from the code in our function.
Using `yield` we can generate a lazily calculated set of values from an infinite loop.
```csharp
public static IEnumerable˂int˃ PositiveIntegers() {
for(int i = 1; i ˂ int.MaxValue; ++i) {
yield return i;
}
}
// We can print the first ten elements in the set of positive integers without
// looping through the complete set of integers.
public static void Main(string[] args) {
foreach(var number in PositiveIntegers().Take(10)) {
Console.WriteLine(number);
}
}
```
To create our lazy string parser well use the concepts above but introduce a few extra steps. Were going to step through an input string character by character to form words. When a delimiter character is found well yield the word weve found so far and then reset that word. When the next word is queried from the `IEnumerable` well repeat the process. This will keep going until we stop iterating over the `IEnumerable` result of the function or we reach the end of the function.
Our lazy string parser implementation looks like this:
```csharp
public static IEnumerable˂string˃ Parse(string content, char delimiter) {
var word = String.Empty;
foreach(var character in content) {
if (character delimiter) {
if (word != String.Empty) {
var result = word;
word = String.Empty;
yield return result;
}
} else {
word += character;
}
}
yield return word;
}
```
***
Learn more about using `yield` with more examples in this video: https://www.youtube.com/watch?v=bsZjfuTrPSA
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u
***
The thumbnail of this video includes a modified image of [Duke Humfreys Library](https://commons.wikimedia.org/wiki/File:Duke_Humfreys_Library_Interior_3,_Bodleian_Library,_Oxford,_UK Diliff.jpg). This file is licensed under the [Creative Commons Attribution-Share Alike 3.0 Unported](https://creativecommons.org/licenses/by-sa/3.0/deed.en) license. Lets Make a Lazy String Parser in C#](https://i.ytimg.com/vi/pauxOqYU038/mqdefault.jpg)


 or my video that covers using [Unity Quaternion operators](https://worldofzero.com/videos/rotation-math-tricks-using-unitys-quaternion-operators/).
Gimble locking occurs when two or more of the axis of rotation align with one another causing the available rotations the object can take to be limited (multiple rotations cause the same overall effect on the object). You can learn more about this effect on Wikipedia: https://en.wikipedia.org/wiki/Gimbal_lock
Ive been a big fan of 6DoF games since I was really young and draw a lot of inspiration from games like Descent, Overload and more. The style of games are really fun and I highly recommend giving one a try if youve never played one before.
Join the World of Zero Discord Server: https://discord.gg/hU5Kq2u Live Coding a Basic 6 Degree of Freedom Flight Controller](https://i.ytimg.com/vi/sCSnitP6xPo/mqdefault.jpg)




The end result of this is will allow us to tweak the radius, location, inside and outside color of the circle we draw in world space. This allows us to do a lot more than we could previously even though we havent really added that many new features. We also migrate to use an unlit shader instead of using the default PBR shader. This is new compared to our original surface shader and is closer to the end result were trying to build. An unlit shader ignores lighting in the scene including things like global illumination and allows us to get a single consistent color across the entire surface our shaders material is applied to.
The World of Zero Discord Server: https://discord.gg/hU5Kq2u Using Properties and Steps in Part 2 of our Ripple in Shader Graph](https://i.ytimg.com/vi/tL3GbBAqF3E/mqdefault.jpg)

