Solved

"this" question in C#

Posted on 2011-09-15
11
227 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
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!

 
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Introduction to Processes

691 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