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 in C# by jyoti
• 1,240 points
9 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 by krishna
• 1,340 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 in C# by rajiv
• 1,620 points
21 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 in C# by jyoti
• 1,240 points
7 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 in C# by pranav
• 2,320 points
10 views
0 votes
1 answer

How to find the extension of a file in C#

Go to here https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getextension?redirectedfrom=MSDN&view=net-6.0#System_IO_Path_GetExtension_System_String_ string myFilePath = @"C:\MyFile.txt"; string ext ...READ MORE

answered Jun 21 in C# by jyoti
• 1,240 points
16 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,100 points
4,468 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
499 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
7,252 views
0 votes
1 answer
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 in C# by krishna
• 1,340 points
7 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 in C# by krishna
• 1,340 points
9 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP