← Back
IAmTimCorey July 13, 2026 10m

Extension Members in C# - Static Methods, Properties, and More

Read full transcript 10 segments
  1. Extension members are a new thing in Extension members are a new thing in C-sharp 14, but what are they and why C-sharp 14, but what are they and why C-sharp 14, but what are they and why are they valuable? That's what we're are they valuable? That's what we're are they valuable? That's what we're going to cover in this video. Now, for going to cover in this video. Now, for going to cover in this video. Now, for most of my training, I work to give an most of my training, I work to give an most of my training, I work to give an in-depth look at technology, so you in-depth look at technology, so you in-depth look at technology, so you understand when and why to use understand when and why to use understand when and why to use something, not just how to use it. But something, not just how to use it. But something, not just how to use it. But sometimes you just need a quick answer sometimes you just need a quick answer sometimes you just need a quick answer to the question, how do I do this? to the question, how do I do this? to the question, how do I do this? That's why I created this 10-minute That's why I created this 10-minute That's why I created this 10-minute training series. training series. training series. So, let's look at this quick demo I have So, let's look at this quick demo I have So, let's look at this quick demo I have already set up. I want to kind of go already set up. I want to kind of go already set up. I want to kind of go over what we have. So, I created a over what we have. So, I created a over what we have. So, I created a person model, really simple, first name, person model, really simple, first name, person model, really simple, first name, last name, a boolean if you're married last name, a boolean if you're married last name, a boolean if you're married or not, and then an integer for number or not, and then an integer for number or not, and then an integer for number of kids you have. I also created two of kids you have. I also created two of kids you have. I also created two constructors, one that's empty and one constructors, one that's empty and one constructors, one that's empty and one that takes in all the properties. So, that takes in all the properties. So, that takes in all the properties. So, this makes my life a little easier. Over this makes my life a little easier. Over this makes my life a little easier. Over here, I created a a list of person model here, I created a a list of person model here, I created a a list of person model called people, which has my five samples called people, which has my five samples called people, which has my five samples in there. in there. in there. Now, I create a for each loop. This is Now, I create a for each loop. This is Now, I create a for each loop. This is This is all set up work, right? So, I This is all set up work, right? So, I This is all set up work, right? So, I create a for each loop like you might create a for each loop like you might create a for each loop like you might do, and I want to say, "Hey, I want to do, and I want to say, "Hey, I want to do, and I want to say, "Hey, I want to get the family size, the number of get the family size, the number of get the family size, the number of people in the family total." So, the people in the family total." So, the people in the family total." So, the number of people starts with the person number of people starts with the person number of people starts with the person themselves, themselves, themselves, and then if they have a spouse, well, and then if they have a spouse, well, and then if they have a spouse, well, that's two, and then add in also the that's two, and then add in also the that's two, and then add in also the number of kids they have.

  2. number of kids they have. number of kids they have. So, we have one for the person So, we have one for the person So, we have one for the person themselves, and then we have this if themselves, and then we have this if themselves, and then we have this if statement, if expression, where we say, statement, if expression, where we say, statement, if expression, where we say, "If they're married, then add then do "If they're married, then add then do "If they're married, then add then do one, otherwise zero in this place." And one, otherwise zero in this place." And one, otherwise zero in this place." And so, we're going to have one plus one if so, we're going to have one plus one if so, we're going to have one plus one if they're married, um and then also add they're married, um and then also add they're married, um and then also add the number of kids. So, for me, Tim the number of kids. So, for me, Tim the number of kids. So, for me, Tim Corey, true and two, that means four Corey, true and two, that means four Corey, true and two, that means four people total. Now, once I have that people total. Now, once I have that people total. Now, once I have that information, then I want to only print information, then I want to only print information, then I want to only print out a console.writeline out a console.writeline out a console.writeline if the family size is greater than or if the family size is greater than or if the family size is greater than or equal to three. equal to three. equal to three. So, this is just a simple setup. There's So, this is just a simple setup. There's So, this is just a simple setup. There's no real reason why I might want to do no real reason why I might want to do no real reason why I might want to do this specifically, but it's a good demo this specifically, but it's a good demo this specifically, but it's a good demo to start from. So, let's talk about to start from. So, let's talk about to start from. So, let's talk about extension members, not methods, members. extension members, not methods, members. extension members, not methods, members. And let's start by looking at extension And let's start by looking at extension And let's start by looking at extension method first to compare. method first to compare. method first to compare. So, we're going to call this person So, we're going to call this person So, we're going to call this person extensions. extensions. extensions. And we'll get rid of the usings up here And we'll get rid of the usings up here And we'll get rid of the usings up here and make this public static.

  3. And you can get the source code for this And you can get the source code for this um down below in the description. um down below in the description. um down below in the description. So, what we used to do, or what we still So, what we used to do, or what we still So, what we used to do, or what we still do actually sometimes too, is say public do actually sometimes too, is say public do actually sometimes too, is say public static string full name and then say static string full name and then say static string full name and then say this uh person model this uh person model this uh person model uh P and then we'd say, I don't know, uh P and then we'd say, I don't know, uh P and then we'd say, I don't know, return return return let's do string interpolation and say let's do string interpolation and say let's do string interpolation and say P.first name space P.first name space P.first name space uh uh uh P.last name. Okay, that'd be the full P.last name. Okay, that'd be the full P.last name. Okay, that'd be the full name. We're adding this on to the person name. We're adding this on to the person name. We're adding this on to the person model instance. model instance. model instance. So, now over here we could say um So, now over here we could say um So, now over here we could say um you know, P.full you know, P.full you know, P.full name instead. name instead. name instead. Okay? Um that would be Okay? Um that would be Okay? Um that would be that would be a way to extend the person that would be a way to extend the person that would be a way to extend the person model object, actually instances of it. model object, actually instances of it. model object, actually instances of it. So, that would allow us to add extra So, that would allow us to add extra So, that would allow us to add extra things. So, if you don't control the things. So, if you don't control the things. So, if you don't control the person model, you could still add on to person model, you could still add on to person model, you could still add on to it this way. And this is the extension it this way. And this is the extension it this way. And this is the extension method.

  4. method. method. Now, I'm going to comment that out Now, I'm going to comment that out Now, I'm going to comment that out because there's a newer way of doing because there's a newer way of doing because there's a newer way of doing things and this allows us to have more things and this allows us to have more things and this allows us to have more freedom of how we do things. And that's freedom of how we do things. And that's freedom of how we do things. And that's extension members. extension members. extension members. So, we say extension. So, we say extension. So, we say extension. What we're going to extend, we're going What we're going to extend, we're going What we're going to extend, we're going to extend the uh person model and we'll to extend the uh person model and we'll to extend the uh person model and we'll call it P. call it P. call it P. You don't have to give it a name if You don't have to give it a name if You don't have to give it a name if you're not going to use the instance of you're not going to use the instance of you're not going to use the instance of it, um but we're going to use the it, um but we're going to use the it, um but we're going to use the instance of it here. So, I'm going to instance of it here. So, I'm going to instance of it here. So, I'm going to say say say uh public uh public uh public int uh family size. And what I'm going to do is this code And what I'm going to do is this code right here, which actually, you know, right here, which actually, you know, right here, which actually, you know, figures out how big the family is. I'm figures out how big the family is. I'm figures out how big the family is. I'm going to take that. I'm going to create going to take that. I'm going to create going to take that. I'm going to create this family size as a property. this family size as a property. this family size as a property. I'm going to use the the arrow function I'm going to use the the arrow function I'm going to use the the arrow function here and just paste in this code. So, here and just paste in this code. So, here and just paste in this code. So, it's essentially the same as doing a get it's essentially the same as doing a get it's essentially the same as doing a get and then inside of the get curly braces and then inside of the get curly braces and then inside of the get curly braces returning this. returning this. returning this. So, it just makes it a little simpler. So, it just makes it a little simpler. So, it just makes it a little simpler. So, this is a read-only property So, this is a read-only property So, this is a read-only property um that's going to give us back the um that's going to give us back the um that's going to give us back the number of the family size for each number of the family size for each number of the family size for each person.

  5. person. person. So, this is a property. This is not So, this is a property. This is not So, this is a property. This is not something we could do before. We only something we could do before. We only something we could do before. We only had methods. Now, we have properties as had methods. Now, we have properties as had methods. Now, we have properties as well. well. well. The difference in a property and a The difference in a property and a The difference in a property and a method can be subtle in some ways, but method can be subtle in some ways, but method can be subtle in some ways, but properties are for things where you're properties are for things where you're properties are for things where you're not going to see changes inside of the not going to see changes inside of the not going to see changes inside of the instance. So, family size, it's just instance. So, family size, it's just instance. So, family size, it's just counting. Now, if you had a setter as counting. Now, if you had a setter as counting. Now, if you had a setter as well, sure, you could you could change a well, sure, you could you could change a well, sure, you could you could change a value. But, we're reading a property, we value. But, we're reading a property, we value. But, we're reading a property, we expect it to be very quick. We expect it expect it to be very quick. We expect it expect it to be very quick. We expect it to be not changing things. So, when you to be not changing things. So, when you to be not changing things. So, when you read uh datetime. you know, now, you read uh datetime. you know, now, you read uh datetime. you know, now, you don't expect it to be making changes don't expect it to be making changes don't expect it to be making changes inside of datetime. You expect it to be inside of datetime. You expect it to be inside of datetime. You expect it to be very quick. These are things you want very quick. These are things you want very quick. These are things you want from properties as opposed to methods from properties as opposed to methods from properties as opposed to methods where things might be changing where things might be changing where things might be changing internally. It might be calling an API internally. It might be calling an API internally. It might be calling an API or whatever else. So, this kind of clues or whatever else. So, this kind of clues or whatever else. So, this kind of clues you in this is just a quick value you you in this is just a quick value you you in this is just a quick value you can grab. It also allows for um some can grab. It also allows for um some can grab. It also allows for um some pattern matching other things that pattern matching other things that pattern matching other things that methods can't do.

  6. methods can't do. methods can't do. Okay. So, we have our um our property Okay. So, we have our um our property Okay. So, we have our um our property here, which means back over here in our here, which means back over here in our here, which means back over here in our uh program.cs, we can simplify things a uh program.cs, we can simplify things a uh program.cs, we can simplify things a bit. Now, bit. Now, bit. Now, I could just copy and paste this. Um in I could just copy and paste this. Um in I could just copy and paste this. Um in fact, let's do that for now. And get rid fact, let's do that for now. And get rid fact, let's do that for now. And get rid of of this line right here. of of this line right here. of of this line right here. And say if family size, well, we're just And say if family size, well, we're just And say if family size, well, we're just going to say p. family size family size if I can if I can if I can find it, right? find it, right? find it, right? There we go. p.family size There we go. p.family size There we go. p.family size That's it. That's it. That's it. So, now we have a property we can read So, now we have a property we can read So, now we have a property we can read from. I can even simplify it a little from. I can even simplify it a little from. I can even simplify it a little further by saying, you know, dot where further by saying, you know, dot where further by saying, you know, dot where family size greater equal three, then family size greater equal three, then family size greater equal three, then now you don't have a if statement here. now you don't have a if statement here. now you don't have a if statement here. Um but that's really not about extension Um but that's really not about extension Um but that's really not about extension methods or members. Um it's about, you methods or members. Um it's about, you methods or members. Um it's about, you know, just making it a simpler know, just making it a simpler know, just making it a simpler for each. for each. for each. Okay. So, we've seen how to do members Okay. So, we've seen how to do members Okay. So, we've seen how to do members with properties, but we still have the with properties, but we still have the with properties, but we still have the ability to use methods in here as well.

  7. ability to use methods in here as well. ability to use methods in here as well. So, public So, public So, public void display That's a weird name, but that's okay. Um That's a weird name, but that's okay. Um I'm going to do this console.writeline. I'm going to do this console.writeline. I'm going to do this console.writeline. This one right here. This one right here. This one right here. I'm going to grab this. I'm going to grab this. I'm going to grab this. And it still uses p because it's pulling And it still uses p because it's pulling And it still uses p because it's pulling from here. Um so, now down here, I could from here. Um so, now down here, I could from here. Um so, now down here, I could say say say p.display p.display p.display family method So, it's a method now, and family method So, it's a method now, and family method So, it's a method now, and that's going to print out that's going to print out that's going to print out console.writeline for that. So, now console.writeline for that. So, now console.writeline for that. So, now you've seen properties, and you've seen you've seen properties, and you've seen you've seen properties, and you've seen methods. And the cool thing is that we methods. And the cool thing is that we methods. And the cool thing is that we didn't have to say, you know, this and didn't have to say, you know, this and didn't have to say, you know, this and then give it a name every time. We can then give it a name every time. We can then give it a name every time. We can simplify each of these methods and simplify each of these methods and simplify each of these methods and properties because we're just properties because we're just properties because we're just referencing this up here. referencing this up here. referencing this up here. And we're putting everything inside of And we're putting everything inside of And we're putting everything inside of extension so that you know that all extension so that you know that all extension so that you know that all these things are extension properties these things are extension properties these things are extension properties and methods that go on to the person and methods that go on to the person and methods that go on to the person model model model object instance.

  8. object instance. object instance. And I'm saying object instance very And I'm saying object instance very And I'm saying object instance very clearly and and specifically because the clearly and and specifically because the clearly and and specifically because the fact, let's create one more. We're going fact, let's create one more. We're going fact, let's create one more. We're going to going to call this to going to call this to going to call this uh string extensions. uh string extensions. uh string extensions. And this is something we couldn't do And this is something we couldn't do And this is something we couldn't do before as well. before as well. before as well. So we're going to mark this as public So we're going to mark this as public So we're going to mark this as public static. static. static. And if I spell it right. There we go. There we go. And I'm going to create an extension And I'm going to create an extension And I'm going to create an extension for string. Now I'm not going to give it for string. Now I'm not going to give it for string. Now I'm not going to give it a a a um a variable name. um a variable name. um a variable name. I'm just going to say string and just I'm just going to say string and just I'm just going to say string and just say public say public say public static string static string static string create GUID. create GUID. create GUID. This is a method, but it's going to This is a method, but it's going to This is a method, but it's going to attach directly to the string attach directly to the string attach directly to the string the string itself, not an instance of the string itself, not an instance of the string itself, not an instance of string. string. string. So I can say return So I can say return So I can say return GUID .NewGuid GUID .NewGuid GUID .NewGuid .ToString. .ToString. .ToString. So this creates a new GUID on the So this creates a new GUID on the So this creates a new GUID on the string.

  9. string. string. So why is this valuable? Well, if I come So why is this valuable? Well, if I come So why is this valuable? Well, if I come down here and say Console.WriteLine, down here and say Console.WriteLine, down here and say Console.WriteLine, I can say string. I can say string. I can say string. Um what did I call that uh create GUID? Um what did I call that uh create GUID? Um what did I call that uh create GUID? So create GUID. Like so. I didn't have to create an Like so. I didn't have to create an empty string. empty string. empty string. Like I didn't have to say like, you Like I didn't have to say like, you Like I didn't have to say like, you know, know, know, empty string.CreateGUID. empty string.CreateGUID. empty string.CreateGUID. Because I don't have to create the Because I don't have to create the Because I don't have to create the string first in order to call it. I've string first in order to call it. I've string first in order to call it. I've put this extension member directly on put this extension member directly on put this extension member directly on string itself. string itself. string itself. So this this is again something else I So this this is again something else I So this this is again something else I couldn't do directly. We always worked couldn't do directly. We always worked couldn't do directly. We always worked off of the instance of the object. So off of the instance of the object. So off of the instance of the object. So like here we're working off the instance like here we're working off the instance like here we're working off the instance P to say family size and display family, P to say family size and display family, P to say family size and display family, etc. per instance. Here, I am putting etc. per instance. Here, I am putting etc. per instance. Here, I am putting this method directly on the object, so this method directly on the object, so this method directly on the object, so you don't have to instantiate it in you don't have to instantiate it in you don't have to instantiate it in order to do work with it.

  10. order to do work with it. order to do work with it. Okay? So, that's that's extension Okay? So, that's that's extension Okay? So, that's that's extension members, where it kind of cleans things members, where it kind of cleans things members, where it kind of cleans things up by putting everything inside of up by putting everything inside of up by putting everything inside of extensions. We can do properties now, extensions. We can do properties now, extensions. We can do properties now, not just methods, and we can also attach not just methods, and we can also attach not just methods, and we can also attach directly to an item instead of attaching directly to an item instead of attaching directly to an item instead of attaching to an instance of an item. All right? to an instance of an item. All right? to an instance of an item. All right? So, that's it. Thanks for watching. As So, that's it. Thanks for watching. As So, that's it. Thanks for watching. As always, I am Tim Corey. always, I am Tim Corey. always, I am Tim Corey. >> [music]

Summary

This transcript introduces C# 14 extension members through a practical demonstration of calculating family size. It contrasts extension members with extension methods and suggests that understanding their purpose, not just syntax, is key for effective use. The takeaway is to leverage extension members to simplify and organize code by adding functionality to existing types.

View original episode ↗