Solved

"this" question in C#

Posted on 2011-09-15
11
222 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
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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Apps blocked by Java 9 63
Error in page 3 46
Full Screen problem when auto scale IFRAME 2 64
C# Single Form 8 27
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now