What does the extension method do in c and why do you need it

0 votes
I'm new to c# and learned that extension methods allow you to "add" methods to existing types without having to create a new derived type, recompile, or otherwise modify the original type. What instances can you give to illustrate this?
Jun 11, 2022 in C# by jyoti
• 1,240 points
661 views

1 answer to this question.

0 votes

Extension Methods allow you to add additional methods to an existing type without affecting the original's specification. It enables you to extend the functionality of a type that you may not be able to inherit (because it is sealed) or for which you do not have the source code.

Here's an illustration. To DateTime, we'll add an Age() method:

public class Program
{
    public static void Main()
    {
        DateTime birthdate = new DateTime(1980, 10, 7);

        Console.WriteLine("Your age is: " + DateTime.Now.Age(birthdate));
    }   
}


public static class Extensions
{
    public static int Age(this DateTime date, DateTime birthDate)
    {
        int birthYear = birthDate.Year;
        int currentYear = DateTime.Now.Year;

        return currentYear - birthYear - 1;
    }
}

We added a new Age() function to C#'s DateTime  type, as you can see in the example. If you need more examples, reply back to me.

answered Jun 21, 2022 by krishna
• 2,820 points

Related Questions In C#

0 votes
1 answer

What are the differences between C, C# and C++ in terms of real-world applications?

C is a bare-bones, straightforward, and clean ...READ MORE

answered May 30, 2022 in C# by rajiv
• 1,620 points
728 views
0 votes
1 answer

What is LINQ and what does it do

LINQ or Language Integrated Query may refer to: a ...READ MORE

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
490 views
0 votes
0 answers

What are the differences between C, C# and C++ in terms of real-world applications?

As the title states, what are the ...READ MORE

Sep 26, 2022 in C# by krishna
• 2,820 points
311 views
0 votes
1 answer

What is the difference between C# and .NET?

C# is a programming language, and.NET refers ...READ MORE

answered Jun 7, 2022 in C# by pranav
• 2,590 points
700 views
0 votes
3 answers

Trying to upload files using Selenium(C#)

You can try using Javascript Executor to ...READ MORE

answered Aug 23, 2019 in Selenium by Abha
• 28,140 points
5,759 views
0 votes
1 answer

Deploy my Windows 10 IOT core application locally!

Of course, you, can! That is, in ...READ MORE

answered Jul 17, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,134 views
+6 votes
16 answers

How do backend of these really cool games work?

Most of the games these days don't ...READ MORE

answered Jul 19, 2018 in Career Counselling by Kalgi
• 52,350 points
11,403 views
0 votes
1 answer

SQLite.Net not being able to create text file in Win IoT Library!

That exception comes when access limited to ...READ MORE

answered Aug 3, 2018 in IoT (Internet of Things) by nirvana
• 3,130 points
1,042 views
0 votes
1 answer

What does the '=>' syntax in C# mean

Here's a simple MSDN example. delegate int del(int ...READ MORE

answered Jun 14, 2022 in C# by krishna
• 2,820 points
436 views
0 votes
1 answer

How do I turn a C# object into a JSON string in .NET

Newtonsoft.json can be used to accomplish this. ...READ MORE

answered Jun 13, 2022 in C# by krishna
• 2,820 points
737 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP