public static string AsJson(this object obj)
Its is an Extension Method on type object
Extension methods are defined as static methods but are called by
using instance method syntax. Their first parameter specifies which
type the method operates on, and the parameter is preceded by the
this modifier.
Your other method is just a simple static method.
public static string AsJson2(object obj)
Both of their calls would be like:
Object obj = new object();
string str = obj.AsJson(); //extension method called
string str2 = Extensions.AsJson2(obj); //Normal static method called
string str3 = Extensions.AsJson(obj); //extension method called like a normal static method
Extension methods are called like instance method but the compiler actually translates into a call to Static method
However, the intermediate language (IL) generated by the compiler
translates your code into a call on the static method.
So
string str = obj.AsJson();
Translates into
string str = Extensions.AsJson(obj);
That is why you can actually do:
object obj = null;
obj.AsJosn(); //Would not result in NRE