Solved

"this" question in C#

Posted on 2011-09-15
11
220 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

760 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

18 Experts available now in Live!

Get 1:1 Help Now