Solved

"this" question in C#

Posted on 2011-09-15
11
226 Views
Last Modified: 2013-12-16
Hi there;

I have seen a function declaration as follows in C#.

     public static somereturnObject functionName(this className someotherobject)
        {

What is that "this" as input parameter? and what is the difference between:
     public static somereturnObject functionName(className someotherobject)
        {

Kind regards.
0
Comment
Question by:jazzIIIlove
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 250 total points
ID: 36541705
Well. The function that you refered called Extension method. Means that you are extending (adding additional method) for existing class. this means that you are Extending the class name that you are passed in.

Say For example:

public static ExtendStringMethod(this string)
{

//Do stuff here

}


After implementing this you can call ExtendStringMethod as one of the string method.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36541713
Hi;

Thanks for the interest in the question.

So can this be considered as inheritance in method level?

Kind regards.
0
 
LVL 16

Assisted Solution

by:Easwaran Paramasivam
Easwaran Paramasivam earned 250 total points
ID: 36541718
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 16

Accepted Solution

by:
Easwaran Paramasivam earned 250 total points
ID: 36541723
It is like normal method. There is nothing special except it is for syntatic sugar. Calling the method will be part of existing class. It would be easy for us to use. Thats it!
0
 
LVL 10

Expert Comment

by:John Claes
ID: 36541755
it is almost the same as you write a inheriting class and use it Every where

Only now you can stay using the base class and have the extended method available.

0
 
LVL 15

Assisted Solution

by:navneethegde
navneethegde earned 83 total points
ID: 36541876
Hi!

it's the way to add extension method to the type

public static somereturnObject functionName(this className someotherobject)
        {
}

So in this example an Extension Method funcionName will be added to the Type className

Say you want to add Extension to String Type as MyName

then You write like this

public static somereturnObject MyName(this String  someotherobject)
        {
}

simple example as :

http://en.wikipedia.org/wiki/Extension_method

Thanks!



0
 
LVL 1

Assisted Solution

by:ManojJainMca2004
ManojJainMca2004 earned 83 total points
ID: 36548091
First one is Extension Method and through out the existing assembly it will be available with that Type object.

The only thing to remember its signature is Static and must written into a non generic class ie static class.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36717486
So. for example, this is a adding a method to a String class and calling it from an instance of that String class.

Right?

Kind regards.
0
 
LVL 37

Assisted Solution

by:TommySzalapski
TommySzalapski earned 84 total points
ID: 36938744
Yes. It is adding that method to the String class and allowing it to be called from instances of that class. It's just a way to add methods.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question