Home > DeveloperSection > Forums > Using this in parameters of static methods in static class?
Ankit Singh

Total Post:341

Posted on    January-27-2014 11:38 PM

 C# C# 

 1 Reply(s)
 806  View(s)
Rate this:

Why should I use 'this' in static functions of static classes to objects in parameter? I mean is the real difference between using those 2 methods?

public static class Extensions


    public static string AsJson(this object obj)

    public static string AsJson2(object obj)


Pravesh Singh

Total Post:412

Posted on    January-28-2014 12:48 AM

Hi Ankit,

The first one is an extension method, whereas the second one is just a static method.

The difference is in how you can call them:

object myObj = new object();

var result = myObj.AsJson();

var result2 = Extensions.AsJson2(myobj);

Note that you can also use the first one as a simple static method:

var result3 = Extensions.AsJson(myObj);

Essentially, it's just syntactical sugar. Upon compilation, the first statement will be transformed into the last one.

Don't want to miss updates? Please click the below button!

Follow MindStick