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
28-Jan-2014The first one is an extension method, whereas the second one is just a static method.
The difference is in how you can call them:
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.